From 27d3d37048655c806b5dd1c5ceeec906daf79e35 Mon Sep 17 00:00:00 2001 From: Neo <80315475+NeoYaBoi@users.noreply.github.com> Date: Sat, 20 Aug 2022 03:16:44 +1000 Subject: [PATCH] feat: channelType Plugin (#29) * feat: channelType Plugin * fix: Forgot to format with prettier * fix: fixed requested changes regarding optional onFail: I wasn't sure if it would throw an error due to the plugin attempting to send a empty message so I put it in a check. * fix: fix * chore: fix jsdoc * feat: Ability to have multiple channel types * fix: fix * feat: Role Only * Delete roleOnly.ts * fix: fixed changes * fix: uses typeof to specify the type * fix: fix Co-authored-by: Evo <85353424+EvolutionX-10@users.noreply.github.com> --- TypeScript/channelType.ts | 44 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 TypeScript/channelType.ts diff --git a/TypeScript/channelType.ts b/TypeScript/channelType.ts new file mode 100644 index 0000000..752c789 --- /dev/null +++ b/TypeScript/channelType.ts @@ -0,0 +1,44 @@ +/** + * This plugin checks if a channel is the specified type + * + * @author @NeoYaBoi [<@762918086349029386>] + * @version 1.0.0 + * @example + * ```ts + * import { channelType } from "../plugins/channelType"; + * import { ChannelType } from "discord.js" + * import { commandModule } from "@sern/handler"; + * export default commandModule({ + * plugins: [ channelType([ChannelType.GuildText], 'This cannot be used here') ], + * execute: (ctx) => { + * //your code here + * } + * }) + * ``` + */ +import { ChannelType } from "discord.js"; +import { CommandType, EventPlugin, PluginType } from "@sern/handler"; +export function channelType( + channelType: ChannelType[], + onFail?: string +): EventPlugin { + return { + type: PluginType.Event, + description: "Checks the channel type.", + async execute(event, controller) { + const [ctx] = event; + let channel = ctx.channel?.type; + //for some reason the dm channel type was returning undefined at some points + if (channel === undefined) { + channel = ChannelType.DM; + } + if (channelType.includes(channel)) { + return controller.next(); + } + if (onFail) { + await ctx.reply(onFail); + } + return controller.stop(); + }, + }; +}