From b5946645e86e8869feb57881de18e1958be122b0 Mon Sep 17 00:00:00 2001 From: nin0dev Date: Fri, 26 Jul 2024 12:37:37 -0400 Subject: [PATCH] Added role context menu --- index.tsx | 101 ++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 91 insertions(+), 10 deletions(-) diff --git a/index.tsx b/index.tsx index c229aa9..083241e 100644 --- a/index.tsx +++ b/index.tsx @@ -7,17 +7,81 @@ import "./style.css"; import { ApplicationCommandInputType, ApplicationCommandOptionType, sendBotMessage } from "@api/Commands"; +import { getUserSettingLazy } from "@api/UserSettings"; +import { InfoIcon } from "@components/Icons"; import { Devs } from "@utils/constants"; +import { getCurrentChannel, getCurrentGuild } from "@utils/discord"; +import { classes } from "@utils/misc"; import definePlugin from "@utils/types"; -import { Forms, GuildMemberStore, Parser } from "@webpack/common"; +import { Forms, GuildMemberStore, GuildStore, Menu, Parser } from "@webpack/common"; import { GuildMember } from "discord-types/general"; +import { PropsWithChildren, SVGProps } from "react"; import { showInRoleModal } from "./RoleMembersModal"; +const DeveloperMode = getUserSettingLazy("appearance", "developerMode")!; + +interface BaseIconProps extends IconProps { + viewBox: string; +} + +interface IconProps extends SVGProps { + className?: string; + height?: string | number; + width?: string | number; +} + +function Icon({ height = 24, width = 24, className, children, viewBox, ...svgProps }: PropsWithChildren) { + return ( + + {children} + + ); +} + + +function EyeIcon(props: IconProps) { + return ( + + + + ); +} + +function getMembersInRole(roleId: string, guildId: string) { + const members = GuildMemberStore.getMembers(guildId); + const membersInRole: GuildMember[] = []; + members.forEach(member => { + if (member.roles.includes(roleId)) { + membersInRole.push(member); + } + }); + return membersInRole; +} + export default definePlugin({ name: "InRole", description: "Know who is in a role (read plugin info!)", authors: [Devs.nin0dev], + dependencies: ["UserSettingsAPI"], + start() { + // DeveloperMode needs to be enabled for the context menu to be shown + DeveloperMode.updateSetting(true); + }, settingsAboutComponent: () => { return ( <> @@ -28,6 +92,7 @@ export default definePlugin({ ); }, + commands: [ { name: "inrole", @@ -47,15 +112,31 @@ export default definePlugin({ return sendBotMessage(ctx.channel.id, { content: "Make sure that you are in a server." }); } const role = args[0].value; - const members = GuildMemberStore.getMembers(ctx.guild!.id); - const membersInRole: GuildMember[] = []; - members.forEach(member => { - if (member.roles.includes(role)) { - membersInRole.push(member); - } - }); - showInRoleModal(membersInRole, role, ctx.channel.id); + showInRoleModal(getMembersInRole(role, ctx.guild.id), role, ctx.channel.id); } } - ] + ], + contextMenus: { + "dev-context"(children, { id }: { id: string; }) { + const guild = getCurrentGuild(); + if (!guild) return; + + const channel = getCurrentChannel(); + if (!channel) return; + + const role = GuildStore.getRole(guild.id, id); + if (!role) return; + + children.push( + { + showInRoleModal(getMembersInRole(role.id, guild.id), role.id, channel.id); + }} + icon={InfoIcon} + /> + ); + } + } });