From abae8defbc83def5d62eddfcd201261d652f8605 Mon Sep 17 00:00:00 2001 From: Neo <80315475+NeoYaBoi@users.noreply.github.com> Date: Mon, 1 Aug 2022 10:30:59 +1000 Subject: [PATCH] feat: Adds nsfwOnly plugin (#12) * Add files via upload * Update nsfwOnly.ts * feat(TS): Adds Channel Only Plugin * Delete channelOnly.ts * fix: fixed discussed changes. --- TypeScript/nsfwOnly.ts | 61 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 TypeScript/nsfwOnly.ts diff --git a/TypeScript/nsfwOnly.ts b/TypeScript/nsfwOnly.ts new file mode 100644 index 0000000..af8bb3c --- /dev/null +++ b/TypeScript/nsfwOnly.ts @@ -0,0 +1,61 @@ +/** + * @author: @NeoYaBoi + * @version: 1.0.0 + * @description: This plugin checks if the channel is nsfw and responds to user with a specified response if not nsfw + * @license: Null + * @example: + * ```ts + * import { nsfwOnly } from "../plugins/nsfwOnly"; //(change if need be) + * import { sernModule, CommandType } from "@sern/handler"; + * export default commandModule({ + * plugins: [ nsfwOnly('response', true/false) ], + * execute: (ctx) => { + * //your code here + * } + * }) + * ``` + */ +import { + ChannelType, + GuildTextBasedChannel, + TextBasedChannel, + TextChannel, +} from "discord.js"; +import { CommandType, EventPlugin, Nullish, PluginType } from "@sern/handler"; +function isGuildText( + channel: Nullish +): channel is GuildTextBasedChannel { + return ( + channel?.type == ChannelType.GuildPublicThread || + channel?.type == ChannelType.GuildPrivateThread + ); +} +export function nsfwOnly( + onFail: string, + ephemeral: boolean +): EventPlugin { + return { + type: PluginType.Event, + description: "Checks if the channel is nsfw or not.", + async execute(event, controller) { + const [ctx] = event; + //checking if command was executed in dms + if (ctx.guild === null) { + await ctx.reply({ content: onFail, ephemeral }); + return controller.stop(); + } + //channel is thread (not supported by nsfw) + if (isGuildText(ctx.channel) == true) { + await ctx.reply({ content: onFail, ephemeral }); + return controller.stop(); + } + if (!(ctx.channel! as TextChannel).nsfw) { + //channel is not nsfw + await ctx.reply({ content: onFail, ephemeral }); + return controller.stop(); + } + //continues to command if nsfw + return controller.next(); + }, + }; +}