From 1cf3fcfd5b2907462084e14cf0feb0ef60c990b2 Mon Sep 17 00:00:00 2001 From: Sqaaakoi Date: Thu, 17 Oct 2024 14:23:17 +1300 Subject: [PATCH] refactor: flag registry system, save as map directly, readable command args --- index.tsx | 105 ++++++++++++++++++++++++++++++++---------------------- 1 file changed, 63 insertions(+), 42 deletions(-) diff --git a/index.tsx b/index.tsx index b3f081a..92685c5 100644 --- a/index.tsx +++ b/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(); +let userFlags = new Map(); + +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; }) {
- {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} -
+ ); } @@ -39,13 +71,14 @@ export default definePlugin({ async start() { const savedFlags = await DataStore.get("USERFLAGS"); if (savedFlags) { - const parsedFlags = new Map(JSON.parse(savedFlags)); - parsedFlags.forEach((value, key) => { - flags.set(key, value); - }); + if (typeof savedFlags === "string") { + userFlags = new Map(JSON.parse(savedFlags)); + } else { + userFlags = new Map(savedFlags); + } } addAccessory("flag", (props: Record) => { - if (flags.has((props.message as Message).author.id)) { + if (userFlags.has((props.message as Message).author.id)) { return ; } 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; } }