From 07c7942e8f2cdb8ce9dc9e5312e412c70c303c27 Mon Sep 17 00:00:00 2001 From: Neo <80315475+NeoYaBoi@users.noreply.github.com> Date: Tue, 26 Jul 2022 10:48:31 +1000 Subject: [PATCH] Perm Checker (#8) * Perm Checker Checks for specified perms and responds with specified response. * fix: changes * fix: final * fix: small syntax error Co-authored-by: Jacob Nguyen <76754747+jacoobes@users.noreply.github.com> --- TypeScript/permCheck.ts | 45 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 TypeScript/permCheck.ts diff --git a/TypeScript/permCheck.ts b/TypeScript/permCheck.ts new file mode 100644 index 0000000..c0a2692 --- /dev/null +++ b/TypeScript/permCheck.ts @@ -0,0 +1,45 @@ +// @ts-nocheck +/** + * @author: @NeoYaBoi + * @version: 1.0.0 + * @description: 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). + * @license: Null + * @example: + * ```ts + * import { permCheck } from "../plugins/permCheck"; //(change if need be) + * import { sernModule, CommandType } from "@sern/handler"; + * export default commandModule({ + * plugins: [ permCheck('permission', 'Response Here') ], + * execute: (ctx) => { + * //your code here + * } + * }) + * ``` + */ + +import { PermissionResolvable } from "discord.js"; +import { CommandType, EventPlugin, PluginType } from "@sern/handler"; +export function permCheck(perm: PermissionResolvable, response: string): EventPlugin { + return { + type: PluginType.Event, + description: "Checks for specified perm", + async execute(event, controller) { + const [ctx] = event; + if(ctx.guild == null) { + ctx.reply('This command cannot be used here') + console.warn('A command stopped because we couldn\'t check there permissions (was used in dms)') //delete this line if you dont was to be notified when a command is used outside of a guild/server + return controller.stop() + } + if(!(ctx.member! as GuildMember).permissions.has(perm)) { + try { + await ctx.reply(response) + return controller.stop() + } catch { + ctx.reply("You do not have the required permissions") + return controller.stop() + } + } + return controller.next() + }, + }; +}