From 6d0e581512a4276fe40e0cbb2fff345180b0a244 Mon Sep 17 00:00:00 2001 From: Gary Date: Sun, 1 Jan 2023 14:07:00 -0600 Subject: [PATCH] feat: add both option to check bot and user perms (#65) Co-authored-by: loveisglitchy --- TypeScript/requirePermission.ts | 59 +++++++++++++++++++++++---------- 1 file changed, 41 insertions(+), 18 deletions(-) diff --git a/TypeScript/requirePermission.ts b/TypeScript/requirePermission.ts index 3c60dc4..958b3d3 100644 --- a/TypeScript/requirePermission.ts +++ b/TypeScript/requirePermission.ts @@ -3,7 +3,7 @@ * * @author @Benzo-Fury [<@762918086349029386>] * @author @needhamgary [<@342314924804014081>] - * @version 1.1.0 + * @version 1.2.0 * @example * ```ts * import { requirePermission } from "../plugins/myPermCheck"; @@ -26,12 +26,16 @@ import { } from "@sern/handler"; function payload(resp?: string) { - return { fetchReply: true, content: resp } as const; + return { + fetchReply: true, + content: resp, + allowedMentions: { repliedUser: false }, + } as const; } export function requirePermission( - target: "user" | "bot", - perm: PermissionResolvable, + target: "user" | "bot" | "both", + perm: PermissionResolvable[], response?: string ): EventPlugin { return { @@ -46,29 +50,48 @@ export function requirePermission( ); //delete this line if you dont want to be notified when a command is used outside of a guild/server return controller.stop(); } + const bot = (await ctx.guild.members.fetchMe({ + cache: false, + })!) as GuildMember; + const memm = ctx.member! as GuildMember; switch (target) { + //*********************************************************************************************************************// case "bot": + if (!bot.permissions.has(perm)) { + if (!response) + response = `I cannot use this command, please give me \`${perm.join( + ", " + )}\` permission(s).`; + await ctx.reply(payload(response)); + return controller.stop(); + } + return controller.next(); + //*********************************************************************************************************************// + case "user": + if (!memm.permissions.has(perm)) { + if (!response) + response = `You cannot use this command because you are missing \`${perm.join( + ", " + )}\` permission(s).`; + await ctx.reply(payload(response)); + return controller.stop(); + } + return controller.next(); + //*********************************************************************************************************************// + case "both": if ( - !( - (await ctx.guild.members.fetchMe({ - cache: false, - })!) as GuildMember - ).permissions.has(perm) + !bot.permissions.has(perm) || + !memm.permissions.has(perm) ) { if (!response) - response = `I cannot use this command, please give me \`${perm}\` permission.`; - await ctx.reply(payload(response)); - return controller.stop(); - } - return controller.next(); - case "user": - if (!(ctx.member! as GuildMember).permissions.has(perm)) { - if (!response) - response = `You cannot use this command because you are missing \`${perm}\` permission.`; + response = `Please ensure <@${bot.user.id}> and <@${ + memm.user.id + }> both have \`${perm.join(", ")}\` permission(s).`; await ctx.reply(payload(response)); return controller.stop(); } return controller.next(); + //*********************************************************************************************************************// default: console.warn( "Perm Check >>> You didn't specify user or bot."