zappyzep/presetup-configurator/src/Configurator.tsx

199 lines
5.6 KiB
TypeScript
Raw Normal View History

Update djs & knub (#395) * update pkgs Signed-off-by: GitHub <noreply@github.com> * new knub typings Signed-off-by: GitHub <noreply@github.com> * more pkg updates Signed-off-by: GitHub <noreply@github.com> * more fixes Signed-off-by: GitHub <noreply@github.com> * channel typings Signed-off-by: GitHub <noreply@github.com> * more message utils typings fixes Signed-off-by: GitHub <noreply@github.com> * migrate permissions Signed-off-by: GitHub <noreply@github.com> * fix: InternalPoster webhookables Signed-off-by: GitHub <noreply@github.com> * djs typings: Attachment & Util Signed-off-by: GitHub <noreply@github.com> * more typings Signed-off-by: GitHub <noreply@github.com> * fix: rename permissionNames Signed-off-by: GitHub <noreply@github.com> * more fixes Signed-off-by: GitHub <noreply@github.com> * half the number of errors * knub commands => messageCommands Signed-off-by: GitHub <noreply@github.com> * configPreprocessor => configParser Signed-off-by: GitHub <noreply@github.com> * fix channel.messages Signed-off-by: GitHub <noreply@github.com> * revert automod any typing Signed-off-by: GitHub <noreply@github.com> * more configParser typings Signed-off-by: GitHub <noreply@github.com> * revert Signed-off-by: GitHub <noreply@github.com> * remove knub type params Signed-off-by: GitHub <noreply@github.com> * fix more MessageEmbed / MessageOptions Signed-off-by: GitHub <noreply@github.com> * dumb commit for @almeidx to see why this is stupid Signed-off-by: GitHub <noreply@github.com> * temp disable custom_events Signed-off-by: GitHub <noreply@github.com> * more minor typings fixes - 23 err left Signed-off-by: GitHub <noreply@github.com> * update djs dep * +debug build method (revert this) Signed-off-by: GitHub <noreply@github.com> * Revert "+debug build method (revert this)" This reverts commit a80af1e729b742d1aad1097df538d224fbd32ce7. * Redo +debug build (Revert this) Signed-off-by: GitHub <noreply@github.com> * uniform before/after Load shorthands Signed-off-by: GitHub <noreply@github.com> * remove unused imports & add prettier plugin Signed-off-by: GitHub <noreply@github.com> * env fixes for web platform hosting Signed-off-by: GitHub <noreply@github.com> * feat: knub v32-next; related fixes * fix: allow legacy keys in change_perms action * fix: request Message Content intent * fix: use Knub's config validation logic in API * fix(dashboard): fix error when there are no message and/or slash commands in a plugin * fix(automod): start_thread action thread options * fix(CustomEvents): message command types * chore: remove unneeded type annotation * feat: add forum channel icon; use thread icon for news threads * chore: make tslint happy * chore: fix formatting --------- Signed-off-by: GitHub <noreply@github.com> Co-authored-by: almeidx <almeidx@pm.me> Co-authored-by: Dragory <2606411+Dragory@users.noreply.github.com>
2023-04-01 12:58:17 +01:00
import yaml from "js-yaml";
2021-01-18 22:44:54 +02:00
import React, { useEffect, useState } from "react";
Update djs & knub (#395) * update pkgs Signed-off-by: GitHub <noreply@github.com> * new knub typings Signed-off-by: GitHub <noreply@github.com> * more pkg updates Signed-off-by: GitHub <noreply@github.com> * more fixes Signed-off-by: GitHub <noreply@github.com> * channel typings Signed-off-by: GitHub <noreply@github.com> * more message utils typings fixes Signed-off-by: GitHub <noreply@github.com> * migrate permissions Signed-off-by: GitHub <noreply@github.com> * fix: InternalPoster webhookables Signed-off-by: GitHub <noreply@github.com> * djs typings: Attachment & Util Signed-off-by: GitHub <noreply@github.com> * more typings Signed-off-by: GitHub <noreply@github.com> * fix: rename permissionNames Signed-off-by: GitHub <noreply@github.com> * more fixes Signed-off-by: GitHub <noreply@github.com> * half the number of errors * knub commands => messageCommands Signed-off-by: GitHub <noreply@github.com> * configPreprocessor => configParser Signed-off-by: GitHub <noreply@github.com> * fix channel.messages Signed-off-by: GitHub <noreply@github.com> * revert automod any typing Signed-off-by: GitHub <noreply@github.com> * more configParser typings Signed-off-by: GitHub <noreply@github.com> * revert Signed-off-by: GitHub <noreply@github.com> * remove knub type params Signed-off-by: GitHub <noreply@github.com> * fix more MessageEmbed / MessageOptions Signed-off-by: GitHub <noreply@github.com> * dumb commit for @almeidx to see why this is stupid Signed-off-by: GitHub <noreply@github.com> * temp disable custom_events Signed-off-by: GitHub <noreply@github.com> * more minor typings fixes - 23 err left Signed-off-by: GitHub <noreply@github.com> * update djs dep * +debug build method (revert this) Signed-off-by: GitHub <noreply@github.com> * Revert "+debug build method (revert this)" This reverts commit a80af1e729b742d1aad1097df538d224fbd32ce7. * Redo +debug build (Revert this) Signed-off-by: GitHub <noreply@github.com> * uniform before/after Load shorthands Signed-off-by: GitHub <noreply@github.com> * remove unused imports & add prettier plugin Signed-off-by: GitHub <noreply@github.com> * env fixes for web platform hosting Signed-off-by: GitHub <noreply@github.com> * feat: knub v32-next; related fixes * fix: allow legacy keys in change_perms action * fix: request Message Content intent * fix: use Knub's config validation logic in API * fix(dashboard): fix error when there are no message and/or slash commands in a plugin * fix(automod): start_thread action thread options * fix(CustomEvents): message command types * chore: remove unneeded type annotation * feat: add forum channel icon; use thread icon for news threads * chore: make tslint happy * chore: fix formatting --------- Signed-off-by: GitHub <noreply@github.com> Co-authored-by: almeidx <almeidx@pm.me> Co-authored-by: Dragory <2606411+Dragory@users.noreply.github.com>
2023-04-01 12:58:17 +01:00
import "./Configurator.css";
2021-01-18 22:44:54 +02:00
import { LevelEntry, Levels } from "./Levels";
import { LogChannel, LogChannels } from "./LogChannels";
export function Configurator() {
2021-09-11 19:06:51 +03:00
const [prefix, setPrefix] = useState("!");
2021-01-18 22:44:54 +02:00
const [levels, setLevels] = useState<LevelEntry[]>([]);
const [withModCommands, setWithModCommands] = useState(false);
const [muteRoleId, setMuteRoleId] = useState("");
const [caseChannelId, setCaseChannelId] = useState("");
const [dmModActionReasons, setDmModActionReasons] = useState(false);
const [withLogs, setWithLogs] = useState(false);
const [logChannels, setLogChannels] = useState<LogChannel[]>([]);
const [result, setResult] = useState({});
useEffect(() => {
const resultObj: any = {
prefix,
levels: levels.reduce((obj, entry) => {
obj[entry[0]] = entry[1];
return obj;
}, {}),
plugins: {
utility: {},
},
};
if (withModCommands) {
resultObj.plugins.cases = {
config: {
case_log_channel: caseChannelId,
},
};
resultObj.plugins.mod_actions = {};
if (muteRoleId) {
resultObj.plugins.mutes = {
config: {
mute_role: muteRoleId,
2021-09-11 19:06:51 +03:00
},
2021-01-18 22:44:54 +02:00
};
if (dmModActionReasons) {
resultObj.plugins.mutes.config.dm_on_mute = true;
}
}
if (dmModActionReasons) {
resultObj.plugins.mod_actions = {
config: {
dm_on_warn: true,
dm_on_kick: true,
dm_on_ban: true,
},
};
}
}
if (withLogs) {
resultObj.plugins.logs = {
config: {
channels: logChannels.reduce((obj, logChannel) => {
if (logChannel.includeExclude === "include") {
obj[logChannel.id] = {
include: Array.from(logChannel.logTypes.values()),
};
} else {
obj[logChannel.id] = {
exclude: Array.from(logChannel.logTypes.values()),
};
}
return obj;
}, {}),
},
};
}
setResult(resultObj);
}, [prefix, levels, withModCommands, muteRoleId, caseChannelId, dmModActionReasons, withLogs, logChannels]);
const [formattedResult, setFormattedResult] = useState("");
useEffect(() => {
let _formattedResult = yaml.dump(result);
// Add line break before each unquoted top-level or second-level property
_formattedResult = _formattedResult.replace(/^ {0,2}[a-z_]+:/gm, "\n$&").trim();
// Add additional line break at the end
_formattedResult += "\n";
// Explain "exclude: []"
_formattedResult = _formattedResult.replace(/exclude: \[]/, "$& # Exclude nothing = include everything");
setFormattedResult(_formattedResult);
2021-01-18 22:44:54 +02:00
}, [result]);
const resultRows = formattedResult.split("\n").length || 1;
const [copied, setCopied] = useState(false);
function copyResultText(textarea: HTMLTextAreaElement) {
textarea.select();
document.execCommand("copy");
setCopied(true);
}
2021-09-11 19:06:51 +03:00
const [copyResetTimeout, setCopyResetTimeout] = useState<number | null>(null);
useEffect(() => {
if (!copied) {
return;
}
if (copyResetTimeout != null) {
window.clearTimeout(copyResetTimeout);
}
const timeout = window.setTimeout(() => setCopied(false), 3000);
setCopyResetTimeout(timeout);
}, [copied]);
2021-01-18 22:44:54 +02:00
return (
<div className="Configurator">
{/* Options */}
<div className="options">
<h2>Prefix</h2>
<div className="control">
<label>
2021-09-11 19:06:51 +03:00
Bot prefix
<br />
<input value={prefix} onChange={(e) => setPrefix(e.target.value)} />
2021-01-18 22:44:54 +02:00
</label>
</div>
<h2>Levels</h2>
<div className="control">
<Levels levels={levels} setLevels={setLevels} />
</div>
<h2>Mod commands</h2>
<div className="control">
<label>
2021-09-11 19:06:51 +03:00
<input type="checkbox" checked={withModCommands} onChange={(e) => setWithModCommands(e.target.checked)} />
2021-01-18 22:44:54 +02:00
Start with a basic mod command setup
</label>
{withModCommands && (
<div>
<label>
2021-09-11 19:06:51 +03:00
Mute role ID
<br />
<input value={muteRoleId} onChange={(e) => setMuteRoleId(e.target.value)} />
2021-01-18 22:44:54 +02:00
</label>
<label>
2021-09-11 19:06:51 +03:00
Case channel ID
<br />
<input value={caseChannelId} onChange={(e) => setCaseChannelId(e.target.value)} />
2021-01-18 22:44:54 +02:00
</label>
<label>
2021-09-11 19:06:51 +03:00
<input
type="checkbox"
checked={dmModActionReasons}
onChange={(e) => setDmModActionReasons(e.target.checked)}
/>
2021-01-18 22:44:54 +02:00
DM reason with mod actions
</label>
</div>
)}
</div>
<h2>Logs</h2>
<div className="control">
<label>
2021-09-11 19:06:51 +03:00
<input type="checkbox" checked={withLogs} onChange={(e) => setWithLogs(e.target.checked)} />
2021-01-18 22:44:54 +02:00
Start with a basic logging setup
</label>
2021-09-11 19:06:51 +03:00
{withLogs && <LogChannels logChannels={logChannels} setLogChannels={setLogChannels} />}
2021-01-18 22:44:54 +02:00
</div>
</div>
{/* Result */}
2021-09-11 19:06:51 +03:00
<textarea
className="result"
rows={resultRows}
readOnly={true}
value={formattedResult}
onClick={(e) => copyResultText(e.target as HTMLTextAreaElement)}
/>
{copied ? <em>Copied!</em> : <em>Click textarea to copy</em>}
2021-01-18 22:44:54 +02:00
</div>
);
}