refactor: flag registry system, save as map directly, readable command args
This commit is contained in:
parent
2f74ce8a3c
commit
1cf3fcfd5b
1 changed files with 63 additions and 42 deletions
105
index.tsx
105
index.tsx
|
@ -4,17 +4,49 @@
|
|||
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*/
|
||||
|
||||
import { ApplicationCommandInputType, ApplicationCommandOptionType, Argument, CommandContext, sendBotMessage } from "@api/Commands";
|
||||
import { ApplicationCommandInputType, ApplicationCommandOptionType, Argument, CommandContext, findOption, sendBotMessage } from "@api/Commands";
|
||||
import { DataStore } from "@api/index";
|
||||
import { addAccessory } from "@api/MessageAccessories";
|
||||
import { Devs } from "@utils/constants";
|
||||
import definePlugin from "@utils/types";
|
||||
import { React, Text } from "@webpack/common";
|
||||
import { Parser, React, Text } from "@webpack/common";
|
||||
import { Message } from "discord-types/general";
|
||||
|
||||
const flags = new Map<string, Flag>();
|
||||
let userFlags = new Map<string, Flag>();
|
||||
|
||||
type FlagRegistryEntry = {
|
||||
label: string;
|
||||
color: string;
|
||||
emoji: string;
|
||||
};
|
||||
|
||||
// worst typing ever?
|
||||
const flagRegistry = {
|
||||
danger: {
|
||||
label: "Danger",
|
||||
color: "#ff7473",
|
||||
emoji: "🛑"
|
||||
} as FlagRegistryEntry,
|
||||
warning: {
|
||||
label: "Warning",
|
||||
color: "#ffb02e",
|
||||
emoji: "⚠️"
|
||||
} as FlagRegistryEntry,
|
||||
info: {
|
||||
label: "Info",
|
||||
color: "#62a8ff",
|
||||
emoji: "🔵"
|
||||
} as FlagRegistryEntry,
|
||||
positive: {
|
||||
label: "Positive",
|
||||
color: "#62ff74",
|
||||
emoji: "✅"
|
||||
} as FlagRegistryEntry
|
||||
} as const;
|
||||
|
||||
|
||||
type Flag = {
|
||||
type: "danger" | "warning" | "info" | "positive";
|
||||
type: keyof typeof flagRegistry;
|
||||
text: string;
|
||||
};
|
||||
|
||||
|
@ -23,11 +55,11 @@ function Flag({ id }: { id: string; }) {
|
|||
<div>
|
||||
<Text
|
||||
variant="text-md/bold"
|
||||
style={{ color: flags.get(id)?.type === "danger" ? "#FF7473" : flags.get(id)?.type === "warning" ? "#ffb02e" : flags.get(id)?.type === "info" ? "#62A8FF" : "#62FF74" }}
|
||||
style={{ color: flagRegistry[userFlags.get(id)?.type || ""]?.color }}
|
||||
>
|
||||
{flags.get(id)?.type === "danger" ? "🛑" : flags.get(id)?.type === "warning" ? "⚠️" : flags.get(id)?.type === "info" ? "🔵" : "✅"} {flags.get(id)?.text}
|
||||
{Parser.parse(flagRegistry[userFlags.get(id)?.type || ""]?.emoji)} {userFlags.get(id)?.text}
|
||||
</Text>
|
||||
</div>
|
||||
</div >
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -39,13 +71,14 @@ export default definePlugin({
|
|||
async start() {
|
||||
const savedFlags = await DataStore.get("USERFLAGS");
|
||||
if (savedFlags) {
|
||||
const parsedFlags = new Map<string, Flag>(JSON.parse(savedFlags));
|
||||
parsedFlags.forEach((value, key) => {
|
||||
flags.set(key, value);
|
||||
});
|
||||
if (typeof savedFlags === "string") {
|
||||
userFlags = new Map<string, Flag>(JSON.parse(savedFlags));
|
||||
} else {
|
||||
userFlags = new Map(savedFlags);
|
||||
}
|
||||
}
|
||||
addAccessory("flag", (props: Record<string, any>) => {
|
||||
if (flags.has((props.message as Message).author.id)) {
|
||||
if (userFlags.has((props.message as Message).author.id)) {
|
||||
return <Flag id={props.message.author.id} />;
|
||||
}
|
||||
return null;
|
||||
|
@ -67,28 +100,12 @@ export default definePlugin({
|
|||
"name": "type",
|
||||
"type": ApplicationCommandOptionType.STRING,
|
||||
"description": "The type of flag to add",
|
||||
choices: [
|
||||
{
|
||||
label: "Danger",
|
||||
value: "danger",
|
||||
name: "Danger"
|
||||
},
|
||||
{
|
||||
label: "Warning",
|
||||
value: "warning",
|
||||
name: "Warning"
|
||||
},
|
||||
{
|
||||
label: "Info",
|
||||
value: "info",
|
||||
name: "Info"
|
||||
},
|
||||
{
|
||||
label: "Positive",
|
||||
value: "positive",
|
||||
name: "Positive"
|
||||
}
|
||||
],
|
||||
choices: Object.entries(flagRegistry).map(([key, flag]) => ({
|
||||
name: key,
|
||||
label: flag.label,
|
||||
displayName: flag.label,
|
||||
value: key,
|
||||
})),
|
||||
required: true
|
||||
},
|
||||
{
|
||||
|
@ -99,14 +116,17 @@ export default definePlugin({
|
|||
},
|
||||
],
|
||||
execute: async (args: Argument[], ctx: CommandContext) => {
|
||||
flags.set(args[0].value, {
|
||||
type: args[1].value as "danger" | "warning" | "info" | "positive",
|
||||
text: args[2].value
|
||||
const user = findOption(args, "user", "");
|
||||
const type: Flag["type"] = findOption(args, "type", "info");
|
||||
const text = findOption(args, "message", "");
|
||||
userFlags.set(user, {
|
||||
type,
|
||||
text
|
||||
});
|
||||
sendBotMessage(ctx.channel.id, {
|
||||
content: `Flag set on <@${args[0].value}> with content \`${args[2].value}\`!`
|
||||
content: `Flag set on <@${user}> with content \`${text}\`!`
|
||||
});
|
||||
await DataStore.set("USERFLAGS", JSON.stringify(Array.from(flags.entries())));
|
||||
await DataStore.set("USERFLAGS", userFlags);
|
||||
return;
|
||||
}
|
||||
},
|
||||
|
@ -123,11 +143,12 @@ export default definePlugin({
|
|||
}
|
||||
],
|
||||
execute: async (args: Argument[], ctx: CommandContext) => {
|
||||
flags.delete(args[0].value);
|
||||
const user = findOption(args, "user", "");
|
||||
userFlags.delete(user);
|
||||
sendBotMessage(ctx.channel.id, {
|
||||
content: `Flag removed from <@${args[0].value}>`
|
||||
content: `Flag removed from <@${user}>`
|
||||
});
|
||||
await DataStore.set("USERFLAGS", JSON.stringify(Array.from(flags.entries())));
|
||||
await DataStore.set("USERFLAGS", userFlags);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue