From 337dbfff09a55921687008e41c765458aba1c5fb Mon Sep 17 00:00:00 2001 From: Peter-MJ-Parker <34216187+Peter-MJ-Parker@users.noreply.github.com> Date: Sun, 7 Jul 2024 12:35:24 -0500 Subject: [PATCH 1/9] chore: Update permCheck.ts Allows users to check member perms before using specific sub command groups, sub commands, and options! --- plugins/permCheck.ts | 274 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 254 insertions(+), 20 deletions(-) diff --git a/plugins/permCheck.ts b/plugins/permCheck.ts index 7884997..04258ce 100644 --- a/plugins/permCheck.ts +++ b/plugins/permCheck.ts @@ -1,16 +1,24 @@ -// @ts-nocheck +//@ts-nocheck /** * @plugin * This is perm check, it allows users to parse the permission you want and let the plugin do the rest. (check user for that perm). + * Each function (other than "command") allows multiple options! [ { ... }, { ... }, { ... } ] See examples! * * @author @Benzo-Fury [<@762918086349029386>] - * @version 1.0.1 + * @author @Peter-MJ-Parker [<@371759410009341952>] + * @version 2.0.0 * @example * ```ts * import { permCheck } from "../plugins/permCheck"; * import { commandModule } from "@sern/handler"; + * import { PermissionFlagsBits } from "discord.js"; * export default commandModule({ - * plugins: [ permCheck('permission', 'No permission response') ], + * plugins: [ + * //permCheck.command("permission", "response") + * //permCheck.subGroups([{ name: "test", perms: , needAllPerms: boolean, response?: "set me for custom response"}, { ...other_group_setup }]) + * //permCheck.subcommands([ { name: "testAgain", ...same options }, { ... } ]) + * //permCheck.options([ {}, {} ]) WIP! + * ], * execute: (ctx) => { * //your code here * } @@ -19,21 +27,247 @@ * @end */ -import type { GuildMember, PermissionResolvable } from "discord.js"; -import { CommandControlPlugin, CommandType, controller } from "@sern/handler"; -export function permCheck(perm: PermissionResolvable, response: string) { - return CommandControlPlugin(async (ctx, args) => { - if (ctx.guild === null) { - await ctx.reply("This command cannot be used here"); - console.warn( - "PermCheck > A command stopped because we couldn't check a users permissions (was used in dms)", - ); //delete this line if you dont want to be notified when a command is used outside of a guild/server - return controller.stop(); - } - if (!(ctx.member! as GuildMember).permissions.has(perm)) { - await ctx.reply(response); - return controller.stop(); - } - return controller.next(); - }); +import { + Colors, + EmbedBuilder, + PermissionsBitField, + type GuildMember, + type PermissionResolvable, + type TextChannel +} from "discord.js"; +import { + CommandControlPlugin, + CommandType, + controller, + Service +} from "@sern/handler"; + +function command(perm: PermissionResolvable, response?: string) { + return CommandControlPlugin(async (ctx) => { + if (ctx.guild === null) { + await ctx.reply("This command cannot be used in DM's!"); + return controller.stop(); + } + if ( + !(ctx.member! as GuildMember) + .permissionsIn(ctx.channel as TextChannel) + .has(perm) + ) { + await ctx.reply({ + embeds: [ + sendEmbed( + response ?? + `You are missing required permissions to run this command:\n${permsToString(perm)}` + ) + ], + ephemeral: ctx.isMessage() ? false : true + }); + return controller.stop(); + } + if ( + !(ctx.member! as GuildMember) + .permissionsIn(ctx.channel as TextChannel) + .any(perm) + ) { + await ctx.reply({ + embeds: [ + sendEmbed( + response ?? + `You need at least one of the following permissions to run this command:\n${permsToString(perm)}` + ) + ], + ephemeral: ctx.isMessage() ? false : true + }); + return controller.stop(); + } + return controller.next(); + }); +} +function subGroups(opts: BaseOptions[]) { + return CommandControlPlugin(async ({ interaction }) => { + await no_guild(interaction); + const member = interaction.member as GuildMember; + const group = interaction.options.getSubcommandGroup(); + for (const opt of opts) { + if (group !== opt.name) { + await interaction.reply({ + embeds: [ + sendEmbed( + `[PLUGIN_permCheck.subGroups]: Failed to find specified subcommandGroup \`${opt.name}\`!` + ) + ], + ephemeral: true + }); + return controller.stop(); + } + const _perms = member.permissionsIn(interaction.channel as TextChannel); + if (opt.needAllPerms && !_perms.has(opt.perms)) { + await interaction.reply({ + embeds: [ + sendEmbed( + opt.response ?? + `You cannot use this group due to missing permissions: ${permsToString(opt.perms)}` + ) + ], + ephemeral: true + }); + return controller.stop(); + } + if (!opt.needAllPerms && !_perms.any(opt.perms)) { + await interaction.reply({ + embeds: [ + sendEmbed( + opt.response ?? + `You cannot use this group because you need at least one of the following permissions: ${permsToString(opt.perms)}` + ) + ], + ephemeral: true + }); + return controller.stop(); + } + } + return controller.next(); + }); +} + +function subcommands(opts: BaseOptions[]) { + return CommandControlPlugin(async ({ interaction }) => { + await no_guild(interaction); + const member = interaction.member as GuildMember; + const sub = interaction.options.getSubcommand(); + for (const opt of opts) { + if (sub !== opt.name) { + await interaction.reply({ + embeds: [ + sendEmbed( + `[PLUGIN_permCheck.subcommands]: Failed to find specified subcommand \`${opt.name}\`!` + ) + ], + ephemeral: true + }); + return controller.stop(); + } + const _perms = member.permissionsIn(interaction.channel as TextChannel); + if (opt.needAllPerms && !_perms.has(opt.perms)) { + await interaction.reply({ + embeds: [ + sendEmbed( + opt.response ?? + `You cannot use this subcommand due to missing permissions: ${permsToString(opt.perms)}` + ) + ], + ephemeral: true + }); + return controller.stop(); + } + if (!opt.needAllPerms && !_perms.any(opt.perms)) { + await interaction.reply({ + embeds: [ + sendEmbed( + opt.response ?? + `You cannot use this subcommand because you need at least one of the following permissions: ${permsToString(opt.perms)}` + ) + ], + ephemeral: true + }); + return controller.stop(); + } + } + return controller.next(); + }); } +function options(opts: BaseOptions[]) { + return CommandControlPlugin(async ({ interaction }) => { + await no_guild(interaction); + const member = interaction.member as GuildMember; + const channel = interaction.channel as TextChannel; + + for (const opt of opts) { + const option = interaction.options.get(opt.name); + console.log(option); + + if (option && option.name !== opt.name) { + await interaction.reply({ + embeds: [ + sendEmbed( + `[PLUGIN_permCheck.options]: Could not find supplied option: \`${opt.name}\`` + ) + ], + ephemeral: true + }); + return controller.stop(); + } + + const permissions = member.permissionsIn(channel); + + console.log(permsToString(permissions)); + if (opt.needAllPerms) { + if (!permissions.has(opt.perms)) { + await interaction.reply({ + embeds: [ + sendEmbed( + opt.response ?? + `You need all the following permissions for option \`${opt.name}\`:\n ${permsToString(...opt.perms)}` + ) + ], + ephemeral: true + }); + return controller.stop(); + } + } else { + if (!permissions.any(opt.perms)) { + await interaction.reply({ + embeds: [ + sendEmbed( + opt.response ?? + `You need at least one of the following permissions for option \`${opt.name}\`: \n${permsToString(...opt.perms)}` + ) + ], + ephemeral: true + }); + return controller.stop(); + } + } + } + + return controller.next(); + }); +} +interface BaseOptions { + name: string; + perms: PermissionResolvable[]; + needAllPerms: boolean; + response?: string; +} + +const no_guild = async (interaction: any) => { + if (interaction.guild === null) { + await interaction.reply({ + content: "This command cannot be used in DM's!", + ephemeral: true + }); + return controller.stop(); + } +}; + +const sendEmbed = (description: string) => { + const client = Service("@sern/client"); + return new EmbedBuilder({ + title: ":x: Permission Error! :x:", + description, + color: Colors.Red, + footer: { + text: client.user?.username!, + icon_url: client.user?.displayAvatarURL()! + } + }); +}; + +export const permsToString = (...perms: PermissionResolvable[]) => { + return new PermissionsBitField(perms) + .toArray() + .map((perm) => `\`${perm}\``) + .join(", "); +}; + +export const permCheck = { command, options, subcommands, subGroups }; From ab30550b53287dc81b40e8734c303dc60f461a0d Mon Sep 17 00:00:00 2001 From: Peter-MJ-Parker <34216187+Peter-MJ-Parker@users.noreply.github.com> Date: Sun, 7 Jul 2024 16:15:25 -0500 Subject: [PATCH 2/9] edit: permCheck - revert back to a function Reverted permCheck back to a function to avoid breaking changes. --- plugins/permCheck.ts | 488 ++++++++++++++++++++++++------------------- 1 file changed, 269 insertions(+), 219 deletions(-) diff --git a/plugins/permCheck.ts b/plugins/permCheck.ts index 04258ce..d7f692a 100644 --- a/plugins/permCheck.ts +++ b/plugins/permCheck.ts @@ -11,13 +11,12 @@ * ```ts * import { permCheck } from "../plugins/permCheck"; * import { commandModule } from "@sern/handler"; - * import { PermissionFlagsBits } from "discord.js"; * export default commandModule({ * plugins: [ - * //permCheck.command("permission", "response") - * //permCheck.subGroups([{ name: "test", perms: , needAllPerms: boolean, response?: "set me for custom response"}, { ...other_group_setup }]) - * //permCheck.subcommands([ { name: "testAgain", ...same options }, { ... } ]) - * //permCheck.options([ {}, {} ]) WIP! + * permCheck("command", ["Administrator", "AddReactions"], "I am a custom response!"), + * permCheck("options", [{ name: "user", needAllPerms: true, perms: ["AttachFiles", "CreateEvents"]}]), + * permCheck("subcommands", [{ name: "list", needAllPerms: false, perms: ["Connect"]}]), + * permCheck("subGroups", [{ name: "list", needAllPerms: false, perms: ["Connect"], response: "I am also a custom response!"}]) * ], * execute: (ctx) => { * //your code here @@ -28,246 +27,297 @@ */ import { - Colors, - EmbedBuilder, - PermissionsBitField, - type GuildMember, - type PermissionResolvable, - type TextChannel + ChatInputCommandInteraction, + Colors, + EmbedBuilder, + PermissionsBitField, + type GuildMember, + type PermissionResolvable, + type TextChannel, } from "discord.js"; import { - CommandControlPlugin, - CommandType, - controller, - Service + CommandControlPlugin, + CommandType, + controller, + Service, } from "@sern/handler"; function command(perm: PermissionResolvable, response?: string) { - return CommandControlPlugin(async (ctx) => { - if (ctx.guild === null) { - await ctx.reply("This command cannot be used in DM's!"); - return controller.stop(); - } - if ( - !(ctx.member! as GuildMember) - .permissionsIn(ctx.channel as TextChannel) - .has(perm) - ) { - await ctx.reply({ - embeds: [ - sendEmbed( - response ?? - `You are missing required permissions to run this command:\n${permsToString(perm)}` - ) - ], - ephemeral: ctx.isMessage() ? false : true - }); - return controller.stop(); - } - if ( - !(ctx.member! as GuildMember) - .permissionsIn(ctx.channel as TextChannel) - .any(perm) - ) { - await ctx.reply({ - embeds: [ - sendEmbed( - response ?? - `You need at least one of the following permissions to run this command:\n${permsToString(perm)}` - ) - ], - ephemeral: ctx.isMessage() ? false : true - }); - return controller.stop(); - } - return controller.next(); - }); + return CommandControlPlugin(async (ctx) => { + if (ctx.guild === null) { + await ctx.reply("This command cannot be used in DM's!"); + return controller.stop(); + } + if ( + !(ctx.member! as GuildMember) + .permissionsIn(ctx.channel as TextChannel) + .has(perm) + ) { + await ctx.reply({ + embeds: [ + sendEmbed( + response ?? + `You are missing required permissions to run this command:\n${permsToString( + perm + )}` + ), + ], + ephemeral: !ctx.isMessage() ? true : false, + }); + return controller.stop(); + } + if ( + !(ctx.member! as GuildMember) + .permissionsIn(ctx.channel as TextChannel) + .any(perm) + ) { + await ctx.reply({ + embeds: [ + sendEmbed( + response ?? + `You need at least one of the following permissions to run this command:\n${permsToString( + perm + )}` + ), + ], + ephemeral: ctx.isMessage() ? false : true, + }); + return controller.stop(); + } + return controller.next(); + }); } function subGroups(opts: BaseOptions[]) { - return CommandControlPlugin(async ({ interaction }) => { - await no_guild(interaction); - const member = interaction.member as GuildMember; - const group = interaction.options.getSubcommandGroup(); - for (const opt of opts) { - if (group !== opt.name) { - await interaction.reply({ - embeds: [ - sendEmbed( - `[PLUGIN_permCheck.subGroups]: Failed to find specified subcommandGroup \`${opt.name}\`!` - ) - ], - ephemeral: true - }); - return controller.stop(); - } - const _perms = member.permissionsIn(interaction.channel as TextChannel); - if (opt.needAllPerms && !_perms.has(opt.perms)) { - await interaction.reply({ - embeds: [ - sendEmbed( - opt.response ?? - `You cannot use this group due to missing permissions: ${permsToString(opt.perms)}` - ) - ], - ephemeral: true - }); - return controller.stop(); - } - if (!opt.needAllPerms && !_perms.any(opt.perms)) { - await interaction.reply({ - embeds: [ - sendEmbed( - opt.response ?? - `You cannot use this group because you need at least one of the following permissions: ${permsToString(opt.perms)}` - ) - ], - ephemeral: true - }); - return controller.stop(); - } - } - return controller.next(); - }); + return CommandControlPlugin(async ({ interaction }) => { + await no_guild(interaction); + const member = interaction.member as GuildMember; + const group = interaction.options.getSubcommandGroup(); + for (const opt of opts) { + if (group !== opt.name) { + await interaction.reply({ + embeds: [ + sendEmbed( + `[PLUGIN_permCheck.subGroups]: Failed to find specified subcommandGroup \`${opt.name}\`!` + ), + ], + ephemeral: true, + }); + return controller.stop(); + } + const _perms = member.permissionsIn( + interaction.channel as TextChannel + ); + if (opt.needAllPerms && !_perms.has(opt.perms)) { + await interaction.reply({ + embeds: [ + sendEmbed( + opt.response ?? + `You cannot use this group due to missing permissions: ${permsToString( + opt.perms + )}` + ), + ], + ephemeral: true, + }); + return controller.stop(); + } + if (!opt.needAllPerms && !_perms.any(opt.perms)) { + await interaction.reply({ + embeds: [ + sendEmbed( + opt.response ?? + `You cannot use this group because you need at least one of the following permissions: ${permsToString( + opt.perms + )}` + ), + ], + ephemeral: true, + }); + return controller.stop(); + } + } + return controller.next(); + }); } function subcommands(opts: BaseOptions[]) { - return CommandControlPlugin(async ({ interaction }) => { - await no_guild(interaction); - const member = interaction.member as GuildMember; - const sub = interaction.options.getSubcommand(); - for (const opt of opts) { - if (sub !== opt.name) { - await interaction.reply({ - embeds: [ - sendEmbed( - `[PLUGIN_permCheck.subcommands]: Failed to find specified subcommand \`${opt.name}\`!` - ) - ], - ephemeral: true - }); - return controller.stop(); - } - const _perms = member.permissionsIn(interaction.channel as TextChannel); - if (opt.needAllPerms && !_perms.has(opt.perms)) { - await interaction.reply({ - embeds: [ - sendEmbed( - opt.response ?? - `You cannot use this subcommand due to missing permissions: ${permsToString(opt.perms)}` - ) - ], - ephemeral: true - }); - return controller.stop(); - } - if (!opt.needAllPerms && !_perms.any(opt.perms)) { - await interaction.reply({ - embeds: [ - sendEmbed( - opt.response ?? - `You cannot use this subcommand because you need at least one of the following permissions: ${permsToString(opt.perms)}` - ) - ], - ephemeral: true - }); - return controller.stop(); - } - } - return controller.next(); - }); + return CommandControlPlugin(async ({ interaction }) => { + await no_guild(interaction); + const member = interaction.member as GuildMember; + const sub = interaction.options.getSubcommand(); + for (const opt of opts) { + if (sub !== opt.name) { + await interaction.reply({ + embeds: [ + sendEmbed( + `[PLUGIN_permCheck.subcommands]: Failed to find specified subcommand \`${opt.name}\`!` + ), + ], + ephemeral: true, + }); + return controller.stop(); + } + const _perms = member.permissionsIn( + interaction.channel as TextChannel + ); + if (opt.needAllPerms && !_perms.has(opt.perms)) { + await interaction.reply({ + embeds: [ + sendEmbed( + opt.response ?? + `You cannot use this subcommand due to missing permissions: ${permsToString( + opt.perms + )}` + ), + ], + ephemeral: true, + }); + return controller.stop(); + } + if (!opt.needAllPerms && !_perms.any(opt.perms)) { + await interaction.reply({ + embeds: [ + sendEmbed( + opt.response ?? + `You cannot use this subcommand because you need at least one of the following permissions: ${permsToString( + opt.perms + )}` + ), + ], + ephemeral: true, + }); + return controller.stop(); + } + } + return controller.next(); + }); } function options(opts: BaseOptions[]) { - return CommandControlPlugin(async ({ interaction }) => { - await no_guild(interaction); - const member = interaction.member as GuildMember; - const channel = interaction.channel as TextChannel; + return CommandControlPlugin(async ({ interaction }) => { + await no_guild(interaction); + const member = interaction.member as GuildMember; + const channel = interaction.channel as TextChannel; - for (const opt of opts) { - const option = interaction.options.get(opt.name); - console.log(option); + for (const opt of opts) { + const option = interaction.options.get(opt.name); + console.log(option); - if (option && option.name !== opt.name) { - await interaction.reply({ - embeds: [ - sendEmbed( - `[PLUGIN_permCheck.options]: Could not find supplied option: \`${opt.name}\`` - ) - ], - ephemeral: true - }); - return controller.stop(); - } + if (option && option.name !== opt.name) { + await interaction.reply({ + embeds: [ + sendEmbed( + `[PLUGIN_permCheck.options]: Could not find supplied option: \`${opt.name}\`` + ), + ], + ephemeral: true, + }); + return controller.stop(); + } - const permissions = member.permissionsIn(channel); + const permissions = member.permissionsIn(channel); - console.log(permsToString(permissions)); - if (opt.needAllPerms) { - if (!permissions.has(opt.perms)) { - await interaction.reply({ - embeds: [ - sendEmbed( - opt.response ?? - `You need all the following permissions for option \`${opt.name}\`:\n ${permsToString(...opt.perms)}` - ) - ], - ephemeral: true - }); - return controller.stop(); - } - } else { - if (!permissions.any(opt.perms)) { - await interaction.reply({ - embeds: [ - sendEmbed( - opt.response ?? - `You need at least one of the following permissions for option \`${opt.name}\`: \n${permsToString(...opt.perms)}` - ) - ], - ephemeral: true - }); - return controller.stop(); - } - } - } + console.log(permsToString(permissions)); + if (opt.needAllPerms) { + if (!permissions.has(opt.perms)) { + await interaction.reply({ + embeds: [ + sendEmbed( + opt.response ?? + `You need all the following permissions for option \`${ + opt.name + }\`:\n ${permsToString(...opt.perms)}` + ), + ], + ephemeral: true, + }); + return controller.stop(); + } + } else { + if (!permissions.any(opt.perms)) { + await interaction.reply({ + embeds: [ + sendEmbed( + opt.response ?? + `You need at least one of the following permissions for option \`${ + opt.name + }\`: \n${permsToString(...opt.perms)}` + ), + ], + ephemeral: true, + }); + return controller.stop(); + } + } + } - return controller.next(); - }); + return controller.next(); + }); } interface BaseOptions { - name: string; - perms: PermissionResolvable[]; - needAllPerms: boolean; - response?: string; + name: string; + perms: PermissionResolvable[]; + needAllPerms: boolean; + response?: string; } -const no_guild = async (interaction: any) => { - if (interaction.guild === null) { - await interaction.reply({ - content: "This command cannot be used in DM's!", - ephemeral: true - }); - return controller.stop(); - } +const no_guild = async (interaction: ChatInputCommandInteraction) => { + if (interaction.guild === null) { + await interaction.reply({ + content: "This command cannot be used in DM's!", + ephemeral: true, + }); + return controller.stop(); + } }; const sendEmbed = (description: string) => { - const client = Service("@sern/client"); - return new EmbedBuilder({ - title: ":x: Permission Error! :x:", - description, - color: Colors.Red, - footer: { - text: client.user?.username!, - icon_url: client.user?.displayAvatarURL()! - } - }); + const client = Service("@sern/client"); + return new EmbedBuilder({ + title: ":x: Permission Error! :x:", + description, + color: Colors.Red, + footer: { + text: client.user?.username!, + icon_url: client.user?.displayAvatarURL()!, + }, + }); }; export const permsToString = (...perms: PermissionResolvable[]) => { - return new PermissionsBitField(perms) - .toArray() - .map((perm) => `\`${perm}\``) - .join(", "); + return new PermissionsBitField(perms) + .toArray() + .map((perm) => `\`${perm}\``) + .join(", "); }; -export const permCheck = { command, options, subcommands, subGroups }; +export function permCheck( + type: "command", + perm: PermissionResolvable, + response?: string +): ReturnType; +export function permCheck( + type: "subGroups" | "subcommands" | "options", + opts: BaseOptions[] +): + | ReturnType + | ReturnType + | ReturnType; + +export function permCheck( + type: "command" | "subGroups" | "subcommands" | "options", + permissionsOrOpts: PermissionResolvable | BaseOptions[], + response?: string +) { + switch (type) { + case "command": + return command(permissionsOrOpts as PermissionResolvable, response); + case "subGroups": + return subGroups(permissionsOrOpts as BaseOptions[]); + case "subcommands": + return subcommands(permissionsOrOpts as BaseOptions[]); + case "options": + return options(permissionsOrOpts as BaseOptions[]); + default: + throw new Error(`Unknown type: ${type}`); + } +} From ee631f471e0ad685f1bcaa762627eb4a29d0fa12 Mon Sep 17 00:00:00 2001 From: Peter-MJ-Parker <34216187+Peter-MJ-Parker@users.noreply.github.com> Date: Sun, 7 Jul 2024 16:49:36 -0500 Subject: [PATCH 3/9] edit: permCheck.ts Remove console logs --- plugins/permCheck.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/plugins/permCheck.ts b/plugins/permCheck.ts index d7f692a..3599383 100644 --- a/plugins/permCheck.ts +++ b/plugins/permCheck.ts @@ -200,7 +200,6 @@ function options(opts: BaseOptions[]) { for (const opt of opts) { const option = interaction.options.get(opt.name); - console.log(option); if (option && option.name !== opt.name) { await interaction.reply({ @@ -216,7 +215,6 @@ function options(opts: BaseOptions[]) { const permissions = member.permissionsIn(channel); - console.log(permsToString(permissions)); if (opt.needAllPerms) { if (!permissions.has(opt.perms)) { await interaction.reply({ From e1cd29dfdbcd2722b19aad58653eaeca355ea1e8 Mon Sep 17 00:00:00 2001 From: Jacob Nguyen <76754747+jacoobes@users.noreply.github.com> Date: Sun, 7 Jul 2024 18:48:43 -0500 Subject: [PATCH 4/9] formatting permCheck.ts --- plugins/permCheck.ts | 73 +++++++++++++------------------------------- 1 file changed, 22 insertions(+), 51 deletions(-) diff --git a/plugins/permCheck.ts b/plugins/permCheck.ts index 3599383..9b33500 100644 --- a/plugins/permCheck.ts +++ b/plugins/permCheck.ts @@ -66,19 +66,10 @@ function command(perm: PermissionResolvable, response?: string) { }); return controller.stop(); } - if ( - !(ctx.member! as GuildMember) - .permissionsIn(ctx.channel as TextChannel) - .any(perm) - ) { + if (!(ctx.member! as GuildMember).permissionsIn(ctx.channel as TextChannel).any(perm)) { await ctx.reply({ embeds: [ - sendEmbed( - response ?? - `You need at least one of the following permissions to run this command:\n${permsToString( - perm - )}` - ), + sendEmbed(response ?? `You need at least one of the following permissions to run this command:\n${permsToString(perm)}`), ], ephemeral: ctx.isMessage() ? false : true, }); @@ -96,9 +87,7 @@ function subGroups(opts: BaseOptions[]) { if (group !== opt.name) { await interaction.reply({ embeds: [ - sendEmbed( - `[PLUGIN_permCheck.subGroups]: Failed to find specified subcommandGroup \`${opt.name}\`!` - ), + sendEmbed(`[PLUGIN_permCheck.subGroups]: Failed to find specified subcommandGroup \`${opt.name}\`!`), ], ephemeral: true, }); @@ -145,44 +134,30 @@ function subcommands(opts: BaseOptions[]) { await no_guild(interaction); const member = interaction.member as GuildMember; const sub = interaction.options.getSubcommand(); - for (const opt of opts) { - if (sub !== opt.name) { + for (const { name, needAllPerms, perms, response } of opts) { + if (sub !== name) { await interaction.reply({ embeds: [ - sendEmbed( - `[PLUGIN_permCheck.subcommands]: Failed to find specified subcommand \`${opt.name}\`!` - ), + sendEmbed(`[PLUGIN_permCheck.subcommands]: Failed to find specified subcommand \`${name}\`!`), ], ephemeral: true, }); return controller.stop(); } - const _perms = member.permissionsIn( - interaction.channel as TextChannel - ); - if (opt.needAllPerms && !_perms.has(opt.perms)) { + const _perms = member.permissionsIn(interaction.channel as TextChannel); + if (needAllPerms && !_perms.has(perms)) { await interaction.reply({ embeds: [ - sendEmbed( - opt.response ?? - `You cannot use this subcommand due to missing permissions: ${permsToString( - opt.perms - )}` - ), + sendEmbed(response ?? `You cannot use this subcommand due to missing permissions: ${permsToString(perms)}`), ], ephemeral: true, }); return controller.stop(); } - if (!opt.needAllPerms && !_perms.any(opt.perms)) { + if (!needAllPerms && !_perms.any(perms)) { await interaction.reply({ embeds: [ - sendEmbed( - opt.response ?? - `You cannot use this subcommand because you need at least one of the following permissions: ${permsToString( - opt.perms - )}` - ), + sendEmbed(response ?? `You cannot use this subcommand because you need at least the following permissions: ${permsToString(perms)}`), ], ephemeral: true, }); @@ -198,14 +173,14 @@ function options(opts: BaseOptions[]) { const member = interaction.member as GuildMember; const channel = interaction.channel as TextChannel; - for (const opt of opts) { - const option = interaction.options.get(opt.name); + for (const { name, needAllPerms, perms, response } of opts) { + const option = interaction.options.get(name); - if (option && option.name !== opt.name) { + if (option && option.name !== name) { await interaction.reply({ embeds: [ sendEmbed( - `[PLUGIN_permCheck.options]: Could not find supplied option: \`${opt.name}\`` + `[PLUGIN_permCheck.options]: Could not find supplied option: \`${name}\`` ), ], ephemeral: true, @@ -215,15 +190,13 @@ function options(opts: BaseOptions[]) { const permissions = member.permissionsIn(channel); - if (opt.needAllPerms) { - if (!permissions.has(opt.perms)) { + if (needAllPerms) { + if (!permissions.has(perms)) { await interaction.reply({ embeds: [ sendEmbed( - opt.response ?? - `You need all the following permissions for option \`${ - opt.name - }\`:\n ${permsToString(...opt.perms)}` + response ?? + `You need all the following permissions for option \`${name}\`:\n ${permsToString(...perms)}` ), ], ephemeral: true, @@ -231,14 +204,12 @@ function options(opts: BaseOptions[]) { return controller.stop(); } } else { - if (!permissions.any(opt.perms)) { + if (!permissions.any(perms)) { await interaction.reply({ embeds: [ sendEmbed( - opt.response ?? - `You need at least one of the following permissions for option \`${ - opt.name - }\`: \n${permsToString(...opt.perms)}` + response ?? + `You need at least one of the following permissions for option \`${name}\`: \n${permsToString(...perms)}` ), ], ephemeral: true, From 17205d7e5ad2ab7f842081e1af76ddb6dbc66e5a Mon Sep 17 00:00:00 2001 From: Peter-MJ-Parker <34216187+Peter-MJ-Parker@users.noreply.github.com> Date: Sun, 7 Jul 2024 20:19:53 -0500 Subject: [PATCH 5/9] edit: Update permCheck.ts Fixed overall formatting simplified ephemeral booleans No longer destructuring interaction from ctx fixed in guild from not fully returning controller.stop() remove `no_guild` function in favor of hard coding imported type CommandType --- plugins/permCheck.ts | 161 +++++++++++++++++++------------------------ 1 file changed, 72 insertions(+), 89 deletions(-) diff --git a/plugins/permCheck.ts b/plugins/permCheck.ts index 9b33500..49d1a51 100644 --- a/plugins/permCheck.ts +++ b/plugins/permCheck.ts @@ -37,7 +37,7 @@ import { } from "discord.js"; import { CommandControlPlugin, - CommandType, + type CommandType, controller, Service, } from "@sern/handler"; @@ -45,33 +45,28 @@ import { function command(perm: PermissionResolvable, response?: string) { return CommandControlPlugin(async (ctx) => { if (ctx.guild === null) { - await ctx.reply("This command cannot be used in DM's!"); + await ctx.reply({ + content: "This command cannot be used in DM's!", + ephemeral: !ctx.isMessage() + }); return controller.stop(); } - if ( - !(ctx.member! as GuildMember) - .permissionsIn(ctx.channel as TextChannel) - .has(perm) - ) { + const _perms = (ctx.member as GuildMember).permissionsIn(ctx.channel as TextChannel); + if (!_perms.has(perm)) { await ctx.reply({ embeds: [ - sendEmbed( - response ?? - `You are missing required permissions to run this command:\n${permsToString( - perm - )}` - ), + sendEmbed(response ?? `You are missing required permissions to run this command:\n${permsToString(perm)}`), ], - ephemeral: !ctx.isMessage() ? true : false, + ephemeral: !ctx.isMessage(), }); return controller.stop(); } - if (!(ctx.member! as GuildMember).permissionsIn(ctx.channel as TextChannel).any(perm)) { + if (!_perms.any(perm)) { await ctx.reply({ embeds: [ sendEmbed(response ?? `You need at least one of the following permissions to run this command:\n${permsToString(perm)}`), ], - ephemeral: ctx.isMessage() ? false : true, + ephemeral: !ctx.isMessage(), }); return controller.stop(); } @@ -79,48 +74,42 @@ function command(perm: PermissionResolvable, response?: string) { }); } function subGroups(opts: BaseOptions[]) { - return CommandControlPlugin(async ({ interaction }) => { - await no_guild(interaction); - const member = interaction.member as GuildMember; - const group = interaction.options.getSubcommandGroup(); + return CommandControlPlugin(async (ctx) => { + if (ctx.guild === null) { + await ctx.reply({ + content: "This command cannot be used in DM's!", + ephemeral: !ctx.isMessage(), + }); + return controller.stop(); + } + const member = ctx.member as GuildMember; + const group = ctx.options.getSubcommandGroup(); for (const opt of opts) { if (group !== opt.name) { - await interaction.reply({ + await ctx.reply({ embeds: [ sendEmbed(`[PLUGIN_permCheck.subGroups]: Failed to find specified subcommandGroup \`${opt.name}\`!`), ], - ephemeral: true, + ephemeral: !ctx.isMessage(), }); return controller.stop(); } - const _perms = member.permissionsIn( - interaction.channel as TextChannel - ); + const _perms = member.permissionsIn(ctx.channel as TextChannel); if (opt.needAllPerms && !_perms.has(opt.perms)) { - await interaction.reply({ + await ctx.reply({ embeds: [ - sendEmbed( - opt.response ?? - `You cannot use this group due to missing permissions: ${permsToString( - opt.perms - )}` - ), + sendEmbed(opt.response ?? `You cannot use this group due to missing permissions: ${permsToString(opt.perms)}`), ], - ephemeral: true, + ephemeral: !ctx.isMessage(), }); return controller.stop(); } if (!opt.needAllPerms && !_perms.any(opt.perms)) { - await interaction.reply({ + await ctx.reply({ embeds: [ - sendEmbed( - opt.response ?? - `You cannot use this group because you need at least one of the following permissions: ${permsToString( - opt.perms - )}` - ), + sendEmbed(opt.response ?? `You cannot use this group because you need at least one of the following permissions: ${permsToString(opt.perms)}`), ], - ephemeral: true, + ephemeral: !ctx.isMessage(), }); return controller.stop(); } @@ -130,36 +119,42 @@ function subGroups(opts: BaseOptions[]) { } function subcommands(opts: BaseOptions[]) { - return CommandControlPlugin(async ({ interaction }) => { - await no_guild(interaction); - const member = interaction.member as GuildMember; - const sub = interaction.options.getSubcommand(); + return CommandControlPlugin(async ({ ctx }) => { + if (ctx.guild === null) { + await ctx.reply({ + content: "This command cannot be used in DM's!", + ephemeral: !ctx.isMessage(), + }); + return controller.stop(); + } + const member = ctx.member as GuildMember; + const sub = ctx.options.getSubcommand(); for (const { name, needAllPerms, perms, response } of opts) { if (sub !== name) { - await interaction.reply({ + await ctx.reply({ embeds: [ sendEmbed(`[PLUGIN_permCheck.subcommands]: Failed to find specified subcommand \`${name}\`!`), ], - ephemeral: true, + ephemeral: !ctx.isMessage(), }); return controller.stop(); } - const _perms = member.permissionsIn(interaction.channel as TextChannel); + const _perms = member.permissionsIn(ctx.channel as TextChannel); if (needAllPerms && !_perms.has(perms)) { - await interaction.reply({ + await ctx.reply({ embeds: [ sendEmbed(response ?? `You cannot use this subcommand due to missing permissions: ${permsToString(perms)}`), ], - ephemeral: true, + ephemeral: !ctx.isMessage(), }); return controller.stop(); } if (!needAllPerms && !_perms.any(perms)) { - await interaction.reply({ + await ctx.reply({ embeds: [ sendEmbed(response ?? `You cannot use this subcommand because you need at least the following permissions: ${permsToString(perms)}`), ], - ephemeral: true, + ephemeral: !ctx.isMessage(), }); return controller.stop(); } @@ -168,22 +163,26 @@ function subcommands(opts: BaseOptions[]) { }); } function options(opts: BaseOptions[]) { - return CommandControlPlugin(async ({ interaction }) => { - await no_guild(interaction); - const member = interaction.member as GuildMember; - const channel = interaction.channel as TextChannel; + return CommandControlPlugin(async ({ ctx }) => { + if (ctx.guild === null) { + await ctx.reply({ + content: "This command cannot be used in DM's!", + ephemeral: !ctx.isMessage(), + }); + return controller.stop(); + } + const member = ctx.member as GuildMember; + const channel = ctx.channel as TextChannel; for (const { name, needAllPerms, perms, response } of opts) { - const option = interaction.options.get(name); + const option = ctx.options.get(name); if (option && option.name !== name) { - await interaction.reply({ - embeds: [ - sendEmbed( - `[PLUGIN_permCheck.options]: Could not find supplied option: \`${name}\`` - ), + await ctx.reply({ + embeds: [ + sendEmbed(`[PLUGIN_permCheck.options]: Could not find supplied option: \`${name}\``), ], - ephemeral: true, + ephemeral: !ctx.isMessage(), }); return controller.stop(); } @@ -192,27 +191,21 @@ function options(opts: BaseOptions[]) { if (needAllPerms) { if (!permissions.has(perms)) { - await interaction.reply({ + await ctx.reply({ embeds: [ - sendEmbed( - response ?? - `You need all the following permissions for option \`${name}\`:\n ${permsToString(...perms)}` - ), + sendEmbed(response ?? `You need all the following permissions for option \`${name}\`:\n ${permsToString(...perms)}`), ], - ephemeral: true, + ephemeral: !ctx.isMessage(), }); return controller.stop(); } } else { if (!permissions.any(perms)) { - await interaction.reply({ + await ctx.reply({ embeds: [ - sendEmbed( - response ?? - `You need at least one of the following permissions for option \`${name}\`: \n${permsToString(...perms)}` - ), + sendEmbed(response ?? `You need at least one of the following permissions for option \`${name}\`: \n${permsToString(...perms)}`), ], - ephemeral: true, + ephemeral: !ctx.isMessage(), }); return controller.stop(); } @@ -229,16 +222,6 @@ interface BaseOptions { response?: string; } -const no_guild = async (interaction: ChatInputCommandInteraction) => { - if (interaction.guild === null) { - await interaction.reply({ - content: "This command cannot be used in DM's!", - ephemeral: true, - }); - return controller.stop(); - } -}; - const sendEmbed = (description: string) => { const client = Service("@sern/client"); return new EmbedBuilder({ @@ -264,13 +247,13 @@ export function permCheck( perm: PermissionResolvable, response?: string ): ReturnType; + export function permCheck( type: "subGroups" | "subcommands" | "options", opts: BaseOptions[] -): - | ReturnType - | ReturnType - | ReturnType; +): | ReturnType + | ReturnType + | ReturnType; export function permCheck( type: "command" | "subGroups" | "subcommands" | "options", From 1d99cbf41e9ca8b764151208bd9c668b60b5e271 Mon Sep 17 00:00:00 2001 From: Peter-MJ-Parker <34216187+Peter-MJ-Parker@users.noreply.github.com> Date: Sun, 7 Jul 2024 20:33:39 -0500 Subject: [PATCH 6/9] edit: Update permCheck.ts Remove ALL destructures `({ ctx })` --> `(ctx)` --- plugins/permCheck.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/permCheck.ts b/plugins/permCheck.ts index 49d1a51..cdaa2a0 100644 --- a/plugins/permCheck.ts +++ b/plugins/permCheck.ts @@ -119,7 +119,7 @@ function subGroups(opts: BaseOptions[]) { } function subcommands(opts: BaseOptions[]) { - return CommandControlPlugin(async ({ ctx }) => { + return CommandControlPlugin(async (ctx) => { if (ctx.guild === null) { await ctx.reply({ content: "This command cannot be used in DM's!", @@ -163,7 +163,7 @@ function subcommands(opts: BaseOptions[]) { }); } function options(opts: BaseOptions[]) { - return CommandControlPlugin(async ({ ctx }) => { + return CommandControlPlugin(async (ctx) => { if (ctx.guild === null) { await ctx.reply({ content: "This command cannot be used in DM's!", From 4def0070a2b46aface2b72ef970e379693de6b52 Mon Sep 17 00:00:00 2001 From: Peter-MJ-Parker <34216187+Peter-MJ-Parker@users.noreply.github.com> Date: Sun, 7 Jul 2024 20:35:44 -0500 Subject: [PATCH 7/9] edit: Update permCheck.ts edit response for not being in guild --- plugins/permCheck.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/plugins/permCheck.ts b/plugins/permCheck.ts index cdaa2a0..cf7a132 100644 --- a/plugins/permCheck.ts +++ b/plugins/permCheck.ts @@ -77,7 +77,7 @@ function subGroups(opts: BaseOptions[]) { return CommandControlPlugin(async (ctx) => { if (ctx.guild === null) { await ctx.reply({ - content: "This command cannot be used in DM's!", + content: "This sub command group cannot be used in DM's!", ephemeral: !ctx.isMessage(), }); return controller.stop(); @@ -122,7 +122,7 @@ function subcommands(opts: BaseOptions[]) { return CommandControlPlugin(async (ctx) => { if (ctx.guild === null) { await ctx.reply({ - content: "This command cannot be used in DM's!", + content: "This sub command cannot be used in DM's!", ephemeral: !ctx.isMessage(), }); return controller.stop(); @@ -166,7 +166,7 @@ function options(opts: BaseOptions[]) { return CommandControlPlugin(async (ctx) => { if (ctx.guild === null) { await ctx.reply({ - content: "This command cannot be used in DM's!", + content: "This specific option cannot be used in DM's!", ephemeral: !ctx.isMessage(), }); return controller.stop(); From d9b01eadff3c43bc88037a4fc1321bc8ccb75d07 Mon Sep 17 00:00:00 2001 From: Peter-MJ-Parker <34216187+Peter-MJ-Parker@users.noreply.github.com> Date: Tue, 9 Jul 2024 00:20:07 -0500 Subject: [PATCH 8/9] edit: permCheck - assigned to object Remove all breaking changes while maintaining integrity of new features as well as original! --- plugins/permCheck.ts | 445 +++++++++++++++++++++---------------------- 1 file changed, 218 insertions(+), 227 deletions(-) diff --git a/plugins/permCheck.ts b/plugins/permCheck.ts index cf7a132..c9f4c79 100644 --- a/plugins/permCheck.ts +++ b/plugins/permCheck.ts @@ -12,12 +12,10 @@ * import { permCheck } from "../plugins/permCheck"; * import { commandModule } from "@sern/handler"; * export default commandModule({ - * plugins: [ - * permCheck("command", ["Administrator", "AddReactions"], "I am a custom response!"), - * permCheck("options", [{ name: "user", needAllPerms: true, perms: ["AttachFiles", "CreateEvents"]}]), - * permCheck("subcommands", [{ name: "list", needAllPerms: false, perms: ["Connect"]}]), - * permCheck("subGroups", [{ name: "list", needAllPerms: false, perms: ["Connect"], response: "I am also a custom response!"}]) - * ], + * plugins: [ permCheck(["Administrator", "AddReactions"], "I am a custom response!"), + * permCheck.options([{ name: "user", needAllPerms: true, perms: ["AttachFiles", "CreateEvents"]}]), + * permCheck.subcommands([{ name: "list", needAllPerms: false, perms: ["Connect"]}]), + * permCheck.subGroups([{ name: "list", needAllPerms: false, perms: ["Connect"], response: "I am also a custom response!"}])], * execute: (ctx) => { * //your code here * } @@ -27,249 +25,242 @@ */ import { - ChatInputCommandInteraction, - Colors, - EmbedBuilder, - PermissionsBitField, - type GuildMember, - type PermissionResolvable, - type TextChannel, -} from "discord.js"; -import { - CommandControlPlugin, - type CommandType, - controller, - Service, -} from "@sern/handler"; + Colors, + EmbedBuilder, + PermissionsBitField, + type GuildMember, + type PermissionResolvable, + type TextChannel +} from 'discord.js'; +import { CommandControlPlugin, type CommandType, controller, Service } from '@sern/handler'; function command(perm: PermissionResolvable, response?: string) { - return CommandControlPlugin(async (ctx) => { - if (ctx.guild === null) { - await ctx.reply({ - content: "This command cannot be used in DM's!", - ephemeral: !ctx.isMessage() - }); - return controller.stop(); - } - const _perms = (ctx.member as GuildMember).permissionsIn(ctx.channel as TextChannel); - if (!_perms.has(perm)) { - await ctx.reply({ - embeds: [ - sendEmbed(response ?? `You are missing required permissions to run this command:\n${permsToString(perm)}`), - ], - ephemeral: !ctx.isMessage(), - }); - return controller.stop(); - } - if (!_perms.any(perm)) { - await ctx.reply({ - embeds: [ - sendEmbed(response ?? `You need at least one of the following permissions to run this command:\n${permsToString(perm)}`), - ], - ephemeral: !ctx.isMessage(), - }); - return controller.stop(); - } - return controller.next(); - }); + return CommandControlPlugin(async (ctx) => { + if (ctx.guild === null) { + await ctx.reply({ + content: "This command cannot be used in DM's!", + ephemeral: !ctx.isMessage() + }); + return controller.stop(); + } + const _perms = (ctx.member as GuildMember).permissionsIn(ctx.channel as TextChannel); + if (!_perms.has(perm)) { + await ctx.reply({ + embeds: [ + sendEmbed(response ?? `You are missing required permissions to run this command:\n${permsToString(perm)}`) + ], + ephemeral: !ctx.isMessage() + }); + return controller.stop(); + } + if (!_perms.any(perm)) { + await ctx.reply({ + embeds: [ + sendEmbed( + response ?? + `You need at least one of the following permissions to run this command:\n${permsToString(perm)}` + ) + ], + ephemeral: !ctx.isMessage() + }); + return controller.stop(); + } + return controller.next(); + }); } function subGroups(opts: BaseOptions[]) { - return CommandControlPlugin(async (ctx) => { - if (ctx.guild === null) { - await ctx.reply({ - content: "This sub command group cannot be used in DM's!", - ephemeral: !ctx.isMessage(), - }); - return controller.stop(); - } - const member = ctx.member as GuildMember; - const group = ctx.options.getSubcommandGroup(); - for (const opt of opts) { - if (group !== opt.name) { - await ctx.reply({ - embeds: [ - sendEmbed(`[PLUGIN_permCheck.subGroups]: Failed to find specified subcommandGroup \`${opt.name}\`!`), - ], - ephemeral: !ctx.isMessage(), - }); - return controller.stop(); - } - const _perms = member.permissionsIn(ctx.channel as TextChannel); - if (opt.needAllPerms && !_perms.has(opt.perms)) { - await ctx.reply({ - embeds: [ - sendEmbed(opt.response ?? `You cannot use this group due to missing permissions: ${permsToString(opt.perms)}`), - ], - ephemeral: !ctx.isMessage(), - }); - return controller.stop(); - } - if (!opt.needAllPerms && !_perms.any(opt.perms)) { - await ctx.reply({ - embeds: [ - sendEmbed(opt.response ?? `You cannot use this group because you need at least one of the following permissions: ${permsToString(opt.perms)}`), - ], - ephemeral: !ctx.isMessage(), - }); - return controller.stop(); - } - } - return controller.next(); - }); + return CommandControlPlugin(async (ctx) => { + if (ctx.guild === null) { + await ctx.reply({ + content: "This sub command group cannot be used in DM's!", + ephemeral: !ctx.isMessage() + }); + return controller.stop(); + } + const member = ctx.member as GuildMember; + const group = ctx.options.getSubcommandGroup(); + for (const opt of opts) { + if (group !== opt.name) { + await ctx.reply({ + embeds: [ + sendEmbed(`[PLUGIN_permCheck.subGroups]: Failed to find specified subcommandGroup \`${opt.name}\`!`) + ], + ephemeral: !ctx.isMessage() + }); + return controller.stop(); + } + const _perms = member.permissionsIn(ctx.channel as TextChannel); + if (opt.needAllPerms && !_perms.has(opt.perms)) { + await ctx.reply({ + embeds: [ + sendEmbed( + opt.response ?? `You cannot use this group due to missing permissions: ${permsToString(opt.perms)}` + ) + ], + ephemeral: !ctx.isMessage() + }); + return controller.stop(); + } + if (!opt.needAllPerms && !_perms.any(opt.perms)) { + await ctx.reply({ + embeds: [ + sendEmbed( + opt.response ?? + `You cannot use this group because you need at least one of the following permissions: ${permsToString( + opt.perms + )}` + ) + ], + ephemeral: !ctx.isMessage() + }); + return controller.stop(); + } + } + return controller.next(); + }); } function subcommands(opts: BaseOptions[]) { - return CommandControlPlugin(async (ctx) => { - if (ctx.guild === null) { - await ctx.reply({ - content: "This sub command cannot be used in DM's!", - ephemeral: !ctx.isMessage(), - }); - return controller.stop(); - } - const member = ctx.member as GuildMember; - const sub = ctx.options.getSubcommand(); - for (const { name, needAllPerms, perms, response } of opts) { - if (sub !== name) { - await ctx.reply({ - embeds: [ - sendEmbed(`[PLUGIN_permCheck.subcommands]: Failed to find specified subcommand \`${name}\`!`), - ], - ephemeral: !ctx.isMessage(), - }); - return controller.stop(); - } - const _perms = member.permissionsIn(ctx.channel as TextChannel); - if (needAllPerms && !_perms.has(perms)) { - await ctx.reply({ - embeds: [ - sendEmbed(response ?? `You cannot use this subcommand due to missing permissions: ${permsToString(perms)}`), - ], - ephemeral: !ctx.isMessage(), - }); - return controller.stop(); - } - if (!needAllPerms && !_perms.any(perms)) { - await ctx.reply({ - embeds: [ - sendEmbed(response ?? `You cannot use this subcommand because you need at least the following permissions: ${permsToString(perms)}`), - ], - ephemeral: !ctx.isMessage(), - }); - return controller.stop(); - } - } - return controller.next(); - }); + return CommandControlPlugin(async (ctx) => { + if (ctx.guild === null) { + await ctx.reply({ + content: "This sub command cannot be used in DM's!", + ephemeral: !ctx.isMessage() + }); + return controller.stop(); + } + const member = ctx.member as GuildMember; + const sub = ctx.options.getSubcommand(); + for (const { name, needAllPerms, perms, response } of opts) { + if (sub !== name) { + await ctx.reply({ + embeds: [sendEmbed(`[PLUGIN_permCheck.subcommands]: Failed to find specified subcommand \`${name}\`!`)], + ephemeral: !ctx.isMessage() + }); + return controller.stop(); + } + const _perms = member.permissionsIn(ctx.channel as TextChannel); + if (needAllPerms && !_perms.has(perms)) { + await ctx.reply({ + embeds: [ + sendEmbed(response ?? `You cannot use this subcommand due to missing permissions: ${permsToString(perms)}`) + ], + ephemeral: !ctx.isMessage() + }); + return controller.stop(); + } + if (!needAllPerms && !_perms.any(perms)) { + await ctx.reply({ + embeds: [ + sendEmbed( + response ?? + `You cannot use this subcommand because you need at least the following permissions: ${permsToString( + perms + )}` + ) + ], + ephemeral: !ctx.isMessage() + }); + return controller.stop(); + } + } + return controller.next(); + }); } function options(opts: BaseOptions[]) { - return CommandControlPlugin(async (ctx) => { - if (ctx.guild === null) { - await ctx.reply({ - content: "This specific option cannot be used in DM's!", - ephemeral: !ctx.isMessage(), - }); - return controller.stop(); - } - const member = ctx.member as GuildMember; - const channel = ctx.channel as TextChannel; + return CommandControlPlugin(async (ctx) => { + if (ctx.guild === null) { + await ctx.reply({ + content: "This specific option cannot be used in DM's!", + ephemeral: !ctx.isMessage() + }); + return controller.stop(); + } + const member = ctx.member as GuildMember; + const channel = ctx.channel as TextChannel; - for (const { name, needAllPerms, perms, response } of opts) { - const option = ctx.options.get(name); + for (const { name, needAllPerms, perms, response } of opts) { + const option = ctx.options.get(name); - if (option && option.name !== name) { - await ctx.reply({ - embeds: [ - sendEmbed(`[PLUGIN_permCheck.options]: Could not find supplied option: \`${name}\``), - ], - ephemeral: !ctx.isMessage(), - }); - return controller.stop(); - } + if (option && option.name !== name) { + await ctx.reply({ + embeds: [sendEmbed(`[PLUGIN_permCheck.options]: Could not find supplied option: \`${name}\``)], + ephemeral: !ctx.isMessage() + }); + return controller.stop(); + } - const permissions = member.permissionsIn(channel); + const permissions = member.permissionsIn(channel); - if (needAllPerms) { - if (!permissions.has(perms)) { - await ctx.reply({ - embeds: [ - sendEmbed(response ?? `You need all the following permissions for option \`${name}\`:\n ${permsToString(...perms)}`), - ], - ephemeral: !ctx.isMessage(), - }); - return controller.stop(); - } - } else { - if (!permissions.any(perms)) { - await ctx.reply({ - embeds: [ - sendEmbed(response ?? `You need at least one of the following permissions for option \`${name}\`: \n${permsToString(...perms)}`), - ], - ephemeral: !ctx.isMessage(), - }); - return controller.stop(); - } - } - } + if (needAllPerms) { + if (!permissions.has(perms)) { + await ctx.reply({ + embeds: [ + sendEmbed( + response ?? + `You need all the following permissions for option \`${name}\`:\n ${permsToString(...perms)}` + ) + ], + ephemeral: !ctx.isMessage() + }); + return controller.stop(); + } + } else { + if (!permissions.any(perms)) { + await ctx.reply({ + embeds: [ + sendEmbed( + response ?? + `You need at least one of the following permissions for option \`${name}\`: \n${permsToString( + ...perms + )}` + ) + ], + ephemeral: !ctx.isMessage() + }); + return controller.stop(); + } + } + } - return controller.next(); - }); + return controller.next(); + }); } interface BaseOptions { - name: string; - perms: PermissionResolvable[]; - needAllPerms: boolean; - response?: string; + name: string; + perms: PermissionResolvable[]; + needAllPerms: boolean; + response?: string; } const sendEmbed = (description: string) => { - const client = Service("@sern/client"); - return new EmbedBuilder({ - title: ":x: Permission Error! :x:", - description, - color: Colors.Red, - footer: { - text: client.user?.username!, - icon_url: client.user?.displayAvatarURL()!, - }, - }); + const client = Service('@sern/client'); + return new EmbedBuilder({ + title: ':x: Permission Error! :x:', + description, + color: Colors.Red, + footer: { + text: client.user?.username!, + icon_url: client.user?.displayAvatarURL()! + } + }); }; export const permsToString = (...perms: PermissionResolvable[]) => { - return new PermissionsBitField(perms) - .toArray() - .map((perm) => `\`${perm}\``) - .join(", "); + return new PermissionsBitField(perms) + .toArray() + .map((perm) => `\`${perm}\``) + .join(', '); }; -export function permCheck( - type: "command", - perm: PermissionResolvable, - response?: string -): ReturnType; - -export function permCheck( - type: "subGroups" | "subcommands" | "options", - opts: BaseOptions[] -): | ReturnType - | ReturnType - | ReturnType; - -export function permCheck( - type: "command" | "subGroups" | "subcommands" | "options", - permissionsOrOpts: PermissionResolvable | BaseOptions[], - response?: string -) { - switch (type) { - case "command": - return command(permissionsOrOpts as PermissionResolvable, response); - case "subGroups": - return subGroups(permissionsOrOpts as BaseOptions[]); - case "subcommands": - return subcommands(permissionsOrOpts as BaseOptions[]); - case "options": - return options(permissionsOrOpts as BaseOptions[]); - default: - throw new Error(`Unknown type: ${type}`); - } +export function _permCheck(permissionsOrOpts: PermissionResolvable, response?: string) { + // permCheck function call is now a alias for command + return command(permissionsOrOpts as PermissionResolvable, response); } + +export const permCheck = Object.assign(_permCheck, { + command, + subGroups, + subcommands, + options +}); From c4b703289640350cfa278fac1bc4eea7e45754d3 Mon Sep 17 00:00:00 2001 From: Peter-MJ-Parker <34216187+Peter-MJ-Parker@users.noreply.github.com> Date: Tue, 9 Jul 2024 00:26:40 -0500 Subject: [PATCH 9/9] edit: permCheck - remove un-needed code. Thanks to Duro for simplifying the code into one function! --- plugins/permCheck.ts | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/plugins/permCheck.ts b/plugins/permCheck.ts index c9f4c79..0d62fdb 100644 --- a/plugins/permCheck.ts +++ b/plugins/permCheck.ts @@ -253,12 +253,7 @@ export const permsToString = (...perms: PermissionResolvable[]) => { .join(', '); }; -export function _permCheck(permissionsOrOpts: PermissionResolvable, response?: string) { - // permCheck function call is now a alias for command - return command(permissionsOrOpts as PermissionResolvable, response); -} - -export const permCheck = Object.assign(_permCheck, { +export const permCheck = Object.assign(command, { command, subGroups, subcommands,