yep @sern-handler (#114)

* fix error handling in subcommandPermCheck

This edit fixes the error "<subcommandName> not found on command: <commandName>." it was finding the incorrect subcommand from the list vs the command subcommands.

* fix error handling in permCheck

Errors were not being thrown in the correct manner. This edit will filter through the command subcommandgroups and subcommands to validate the listed names in the plugin. Originally, the plugin would only search for the first subcommandgroup or subcommand and try to match to an in the list.
This commit is contained in:
Max
2024-07-15 14:22:36 +12:00
committed by GitHub
parent 6be9e5473a
commit 30fb45fdc5
2 changed files with 34 additions and 38 deletions

View File

@@ -6,7 +6,8 @@
*
* @author @Benzo-Fury [<@762918086349029386>]
* @author @Peter-MJ-Parker [<@371759410009341952>]
* @version 2.0.0
* @author @MaxiIsSlayy [<@237210568791031809>]
* @version 2.0.1
* @example
* ```ts
* import { permCheck } from "../plugins/permCheck";
@@ -79,16 +80,18 @@ function subGroups(opts: BaseOptions[]) {
}
const member = ctx.member as GuildMember;
const group = ctx.options.getSubcommandGroup();
if (!opts.some(opt => opt.name === group)) {
await interaction.reply({
embeds: [
sendEmbed(
`[PLUGIN_permCheck.subGroups]: Failed to find specified subcommandGroup \`${group}\`!`
),
],
ephemeral: true,
});
return controller.stop();
}
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({
@@ -131,14 +134,18 @@ function subcommands(opts: BaseOptions[]) {
}
const member = ctx.member as GuildMember;
const sub = ctx.options.getSubcommand();
if (!opts.some(opt => opt.name === sub)) {
await interaction.reply({
embeds: [
sendEmbed(
`[PLUGIN_permCheck.subcommands]: Failed to find specified subcommand \`${sub}\`!`
),
],
ephemeral: true,
});
return controller.stop();
}
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({

View File

@@ -4,7 +4,8 @@
* Inspired by the plugin "requirePermission" created by Benzo-Fury & needhamgary, this plugin will set permissions for specific subcommands without manually writing it into the code.
*
* @author @Peter-MJ-Parker [<@371759410009341952>]
* @version 1.0
* @author @MaxiIsSlayy [<@237210568791031809>]
* @version 1.0.1
* @example
* ```ts
* import { subcommandPermCheck } from "../plugins/subcommandPerms.js";
@@ -29,8 +30,6 @@
* ```
* @end
*/
/** Marked TODO's will be reconfigured with release of sern v4! **/
import type { GuildMember, PermissionResolvable, TextChannel } from 'discord.js';
import { type CommandType, CommandControlPlugin, controller } from '@sern/handler';
@@ -45,30 +44,24 @@ export function subcommandPermCheck(opts: Options) {
return CommandControlPlugin<CommandType.Slash>(async (ctx) => {
if (!ctx.isSlash()) {
throw new Error('You did not provide a slash command.', { cause: "The plugin 'subcommandPermCheck' is meant for Slash commands only!" });
//TODO: return state to command rather than error
return controller.stop();
}
if (ctx.guild === null) {
await ctx.reply({
content: "This sub command cannot be used in DM's!",
ephemeral: true
});
ctx.reply("PermCheck > A command stopped because we couldn't check a users permissions (was used in dms)");
return controller.stop();
}
const member = ctx.member as GuildMember;
const subcommands = opts.list;
let sub = ctx.options.getSubcommand();
let subs = ctx.options.getSubcommand();
/** WILL BE REWRITTEN WHEN SERN V4 IS RELEASED!!! **/
if (!subcommands.some((opt) => opt.subcommand === subs)) {
throw new Error("You provided a subcommand name which doesn't exist in given command.", {
cause: `${subs} not found on command: ${ctx.interaction.commandName}.`
});
return controller.stop();
}
for (const { name, perms } of subcommands) {
if (name !== sub) {
throw new Error("You provided a subcommand name which doesn't exist in given command.", {
cause: `subcommand: \`${name}\` not found on command: ${ctx.interaction.commandName}.`
});
//TODO: return state to command rather than error
return controller.stop();
} else {
const each = permsToString(perms);
const { needAllPerms } = opts;
const memberPermissions = member.permissionsIn(ctx.channel as TextChannel);
@@ -80,7 +73,6 @@ export function subcommandPermCheck(opts: Options) {
`You are required to have all of the following permissions to run this subcommand in this channel:\n${each}`,
ephemeral: true
});
//TODO: return state to command
return controller.stop();
}
} else {
@@ -91,13 +83,10 @@ export function subcommandPermCheck(opts: Options) {
`You are required to have at least one of the following permissions to run this subcommand in this channel:\n${each}`,
ephemeral: true
});
//TODO: return state to command
return controller.stop();
}
}
}
}
//TODO: return state to command
return controller.next();
});
}