Merge pull request 'refactor: flag registry system, save as map directly, readable command args' (#1) from Sqaaakoi-VencordUserPlugins/userflags-fork:refactor into main

Reviewed-on: https://git.nin0.dev///userplugins/userflags/pulls/1
Reviewed-by: nin0dev <personal@nin0.dev>
This commit is contained in:
nin0dev 2024-10-16 21:44:36 -04:00
commit 8299c02ed4

105
index.tsx
View file

@ -4,17 +4,49 @@
* SPDX-License-Identifier: GPL-3.0-or-later * 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 { DataStore } from "@api/index";
import { addAccessory } from "@api/MessageAccessories"; import { addAccessory } from "@api/MessageAccessories";
import { Devs } from "@utils/constants"; import { Devs } from "@utils/constants";
import definePlugin from "@utils/types"; import definePlugin from "@utils/types";
import { React, Text } from "@webpack/common"; import { Parser, React, Text } from "@webpack/common";
import { Message } from "discord-types/general"; 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 Flag = {
type: "danger" | "warning" | "info" | "positive"; type: keyof typeof flagRegistry;
text: string; text: string;
}; };
@ -23,11 +55,11 @@ function Flag({ id }: { id: string; }) {
<div> <div>
<Text <Text
variant="text-md/bold" 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> </Text>
</div> </div >
); );
} }
@ -39,13 +71,14 @@ export default definePlugin({
async start() { async start() {
const savedFlags = await DataStore.get("USERFLAGS"); const savedFlags = await DataStore.get("USERFLAGS");
if (savedFlags) { if (savedFlags) {
const parsedFlags = new Map<string, Flag>(JSON.parse(savedFlags)); if (typeof savedFlags === "string") {
parsedFlags.forEach((value, key) => { userFlags = new Map<string, Flag>(JSON.parse(savedFlags));
flags.set(key, value); } else {
}); userFlags = new Map(savedFlags);
}
} }
addAccessory("flag", (props: Record<string, any>) => { 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 <Flag id={props.message.author.id} />;
} }
return null; return null;
@ -67,28 +100,12 @@ export default definePlugin({
"name": "type", "name": "type",
"type": ApplicationCommandOptionType.STRING, "type": ApplicationCommandOptionType.STRING,
"description": "The type of flag to add", "description": "The type of flag to add",
choices: [ choices: Object.entries(flagRegistry).map(([key, flag]) => ({
{ name: key,
label: "Danger", label: flag.label,
value: "danger", displayName: flag.label,
name: "Danger" value: key,
}, })),
{
label: "Warning",
value: "warning",
name: "Warning"
},
{
label: "Info",
value: "info",
name: "Info"
},
{
label: "Positive",
value: "positive",
name: "Positive"
}
],
required: true required: true
}, },
{ {
@ -99,14 +116,17 @@ export default definePlugin({
}, },
], ],
execute: async (args: Argument[], ctx: CommandContext) => { execute: async (args: Argument[], ctx: CommandContext) => {
flags.set(args[0].value, { const user = findOption(args, "user", "");
type: args[1].value as "danger" | "warning" | "info" | "positive", const type: Flag["type"] = findOption(args, "type", "info");
text: args[2].value const text = findOption(args, "message", "");
userFlags.set(user, {
type,
text
}); });
sendBotMessage(ctx.channel.id, { 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; return;
} }
}, },
@ -123,11 +143,12 @@ export default definePlugin({
} }
], ],
execute: async (args: Argument[], ctx: CommandContext) => { execute: async (args: Argument[], ctx: CommandContext) => {
flags.delete(args[0].value); const user = findOption(args, "user", "");
userFlags.delete(user);
sendBotMessage(ctx.channel.id, { 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; return;
} }
} }