diff --git a/index.tsx b/index.tsx index d2e4d84..ada4bc1 100644 --- a/index.tsx +++ b/index.tsx @@ -6,9 +6,11 @@ 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 { Parser, React, Text } from "@webpack/common"; +import { Message } from "discord-types/general"; let userFlags = new Map(); @@ -25,22 +27,23 @@ type FlagRegistryEntry = { emoji: string; }; -const flagRegistry: Record = { +// worst typing ever? +const flagRegistry: { [key in FlagType]: FlagRegistryEntry } = { [FlagType.DANGER]: { label: "Danger", color: "#ff7473", emoji: "🛑" - }, + } as FlagRegistryEntry, [FlagType.WARNING]: { label: "Warning", color: "#ffb02e", emoji: "âš ī¸" - }, + } as FlagRegistryEntry, [FlagType.INFO]: { label: "Info", color: "#62a8ff", - emoji: "â„šī¸" - }, + emoji: "đŸ”ĩ" + } as FlagRegistryEntry, [FlagType.POSITIVE]: { label: "Positive", color: "#62ff74", @@ -54,24 +57,16 @@ type Flag = { text: string; }; -const subscribers = new Set<() => void>(); -function subscribe(callback: () => void) { - subscribers.add(callback); - return () => subscribers.delete(callback); -}; - function Flag({ id }: { id: string; }) { - const flag = React.useSyncExternalStore(subscribe, () => userFlags.get(id)); - if (!flag) return null; return (
- {Parser.parse(flagRegistry[flag.type].emoji)} {flag.text} + {Parser.parse(flagRegistry[userFlags.get(id)?.type || ""]?.emoji)} {userFlags.get(id)?.text} -
+ ); } @@ -86,12 +81,15 @@ export default definePlugin({ if (typeof savedFlags === "string") { userFlags = new Map(JSON.parse(savedFlags)); } else { - userFlags = new Map(savedFlags); + userFlags = new Map(savedFlags); } } - }, - renderMessageAccessory: (props: Record) => { - return ; + addAccessory("flag", (props: Record) => { + if (userFlags.has((props.message as Message).author.id)) { + return ; + } + return null; + }, 4); }, commands: [ { @@ -106,9 +104,9 @@ export default definePlugin({ required: true }, { - name: "type", - type: ApplicationCommandOptionType.STRING, - description: "The type of flag to add", + "name": "type", + "type": ApplicationCommandOptionType.STRING, + "description": "The type of flag to add", choices: Object.entries(flagRegistry).map(([key, flag]) => ({ name: key, label: flag.label, @@ -132,7 +130,6 @@ export default definePlugin({ type, text }); - subscribers.forEach(cb => cb()); sendBotMessage(ctx.channel.id, { content: `Flag set on <@${user}> with content \`${text}\`!` }); @@ -155,7 +152,6 @@ export default definePlugin({ execute: async (args: Argument[], ctx: CommandContext) => { const user = findOption(args, "user", ""); userFlags.delete(user); - subscribers.forEach(cb => cb()); sendBotMessage(ctx.channel.id, { content: `Flag removed from <@${user}>` });