userflags/index.tsx

158 lines
5 KiB
TypeScript
Raw Normal View History

2024-10-12 12:39:41 -04:00
/*
* Vencord, a Discord client mod
* Copyright (c) 2024 Vendicated and contributors
* SPDX-License-Identifier: GPL-3.0-or-later
*/
import { ApplicationCommandInputType, ApplicationCommandOptionType, Argument, CommandContext, findOption, sendBotMessage } from "@api/Commands";
2024-10-12 13:19:48 -04:00
import { DataStore } from "@api/index";
2024-10-12 12:39:41 -04:00
import { addAccessory } from "@api/MessageAccessories";
2024-10-06 15:01:19 -04:00
import { Devs } from "@utils/constants";
import definePlugin from "@utils/types";
import { Parser, React, Text } from "@webpack/common";
2024-10-12 12:39:41 -04:00
import { Message } from "discord-types/general";
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;
2024-10-12 12:39:41 -04:00
type Flag = {
type: keyof typeof flagRegistry;
2024-10-12 12:39:41 -04:00
text: string;
};
2024-10-12 12:57:36 -04:00
function Flag({ id }: { id: string; }) {
return (
2024-10-12 12:39:41 -04:00
<div>
2024-10-12 12:57:36 -04:00
<Text
2024-10-12 13:23:10 -04:00
variant="text-md/bold"
style={{ color: flagRegistry[userFlags.get(id)?.type || ""]?.color }}
2024-10-12 12:57:36 -04:00
>
{Parser.parse(flagRegistry[userFlags.get(id)?.type || ""]?.emoji)} {userFlags.get(id)?.text}
2024-10-12 12:39:41 -04:00
</Text>
</div >
2024-10-12 12:39:41 -04:00
);
}
2024-10-06 15:01:19 -04:00
export default definePlugin({
name: "UserFlags",
2024-10-12 12:39:41 -04:00
description: `Add "flags" to users that will always show under their messages`,
2024-10-06 15:01:19 -04:00
authors: [Devs.nin0dev],
2024-10-12 12:39:41 -04:00
dependencies: ["MessageAccessoriesAPI"],
2024-10-12 13:19:48 -04:00
async start() {
const savedFlags = await DataStore.get("USERFLAGS");
if (savedFlags) {
if (typeof savedFlags === "string") {
userFlags = new Map<string, Flag>(JSON.parse(savedFlags));
} else {
userFlags = new Map(savedFlags);
}
2024-10-12 13:19:48 -04:00
}
2024-10-12 12:39:41 -04:00
addAccessory("flag", (props: Record<string, any>) => {
if (userFlags.has((props.message as Message).author.id)) {
2024-10-12 12:57:36 -04:00
return <Flag id={props.message.author.id} />;
}
return null;
2024-10-12 12:39:41 -04:00
}, 4);
},
commands: [
{
name: "flag set",
description: "Set a flag on a user",
inputType: ApplicationCommandInputType.BOT,
options: [
{
name: "user",
type: ApplicationCommandOptionType.USER,
description: "The user to set a flag to",
required: true
},
{
"name": "type",
"type": ApplicationCommandOptionType.STRING,
"description": "The type of flag to add",
choices: Object.entries(flagRegistry).map(([key, flag]) => ({
name: key,
label: flag.label,
displayName: flag.label,
value: key,
})),
2024-10-12 12:39:41 -04:00
required: true
},
{
name: "message",
type: ApplicationCommandOptionType.STRING,
description: "The flag content",
required: true
},
],
2024-10-12 13:19:48 -04:00
execute: async (args: Argument[], ctx: CommandContext) => {
const user = findOption(args, "user", "");
const type: Flag["type"] = findOption(args, "type", "info");
const text = findOption(args, "message", "");
userFlags.set(user, {
type,
text
2024-10-12 12:39:41 -04:00
});
sendBotMessage(ctx.channel.id, {
content: `Flag set on <@${user}> with content \`${text}\`!`
2024-10-12 12:39:41 -04:00
});
await DataStore.set("USERFLAGS", userFlags);
2024-10-12 12:39:41 -04:00
return;
}
},
{
name: "flag delete",
description: "Delete the flag from a user",
inputType: ApplicationCommandInputType.BOT,
options: [
{
name: "user",
type: ApplicationCommandOptionType.USER,
description: "The user to delete the flag from",
required: true
}
],
2024-10-12 13:19:48 -04:00
execute: async (args: Argument[], ctx: CommandContext) => {
const user = findOption(args, "user", "");
userFlags.delete(user);
2024-10-12 12:39:41 -04:00
sendBotMessage(ctx.channel.id, {
content: `Flag removed from <@${user}>`
2024-10-12 12:39:41 -04:00
});
await DataStore.set("USERFLAGS", userFlags);
2024-10-12 12:39:41 -04:00
return;
}
}
]
2024-10-06 15:01:19 -04:00
});