From 01508914f732da3141989e6e733b41e0ba20989c Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Sun, 31 Jul 2022 11:56:10 +0530 Subject: [PATCH] chore: Update JavaScript plugins (#17) chore: update JavaScript plugins Co-authored-by: EvolutionX-10 --- JavaScript/dmOnly.js | 37 +++++++++++++ JavaScript/ownerOnly.js | 33 ++++++++++++ JavaScript/permCheck.js | 46 ++++++++++++++++ JavaScript/publish.js | 116 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 232 insertions(+) create mode 100644 JavaScript/dmOnly.js create mode 100644 JavaScript/ownerOnly.js create mode 100644 JavaScript/permCheck.js create mode 100644 JavaScript/publish.js diff --git a/JavaScript/dmOnly.js b/JavaScript/dmOnly.js new file mode 100644 index 0000000..535308a --- /dev/null +++ b/JavaScript/dmOnly.js @@ -0,0 +1,37 @@ +// @ts-nocheck + +/** + * @author: @EvolutionX-10 + * @version: 1.1.0-beta + * @description: This is dmOnly plugin, it allows commands to be run only in DMs. + * @requires `partials: [Partials.Channel], intents: [GatewayIntentBits.DirectMessages, GatewayIntentBits.MessageContent] + * @license: MIT + * @example: + * ```ts + * import { dmOnly } from "../path/to/your/plugin/folder"; + * import { commandModule } from "@sern/handler"; + * export default commandModule({ + * plugins: [dmOnly()], + * execute: // your code + * }) + * ``` + */ +import { PluginType } from "@sern/handler"; +export function dmOnly(content, ephemeral) { + return { + type: PluginType.Event, + description: "Allows commands to be run in DM only", + + async execute(event, controller) { + const [ctx] = event; + if (ctx.channel?.isDMBased()) return controller.next(); + if (content) + await ctx.reply({ + content, + ephemeral, + }); // Change this if you want or remove it for silent deny + + return controller.stop(); + }, + }; +} diff --git a/JavaScript/ownerOnly.js b/JavaScript/ownerOnly.js new file mode 100644 index 0000000..4b9c49e --- /dev/null +++ b/JavaScript/ownerOnly.js @@ -0,0 +1,33 @@ +// @ts-nocheck + +/** + * @author: @EvolutionX-10 + * @version: 1.0.0 + * @description: This is OwnerOnly plugin, it allows only bot owners to run the command, like eval. + * @license: MIT + * @example: + * ```ts + * import { ownerOnly } from "../path/to/your/plugin/folder"; + * import { sernModule, CommandType } from "@sern/handler"; + * export default sernModule([OwnerOnly()], { + * // your code + * }) + * ``` + */ +import { PluginType } from "@sern/handler"; +const ownerIDs = ["697795666373640213"]; //! Fill your ID + +export function ownerOnly() { + return { + type: PluginType.Event, + description: "Allows only bot owner to run command", + + async execute(event, controller) { + const [ctx] = event; + if (ownerIDs.includes(ctx.user.id)) return controller.next(); //* If you want to reply when the command fails due to user not being owner, you can use following + // await ctx.reply("Only owner can run it!!!"); + + return controller.stop(); //! Important: It stops the execution of command! + }, + }; +} diff --git a/JavaScript/permCheck.js b/JavaScript/permCheck.js new file mode 100644 index 0000000..5183d0e --- /dev/null +++ b/JavaScript/permCheck.js @@ -0,0 +1,46 @@ +// @ts-nocheck + +/** + * @author: @NeoYaBoi + * @version: 1.0.1 + * @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', 'No permission response') ], + * execute: (ctx) => { + * //your code here + * } + * }) + * ``` + */ +import { PluginType } from "@sern/handler"; +export function permCheck(perm, response) { + 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( + "PermCheck > A command stopped because we couldn't check a users permissions (was used in dms)" + ); //delete this line if you dont want to be notified when a command is used outside of a guild/server + + return controller.stop(); + } + + if (!ctx.member.permissions.has(perm)) { + await ctx.reply(response); + return controller.stop(); + } + + return controller.next(); + }, + }; +} diff --git a/JavaScript/publish.js b/JavaScript/publish.js new file mode 100644 index 0000000..d535018 --- /dev/null +++ b/JavaScript/publish.js @@ -0,0 +1,116 @@ +// @ts-nocheck + +/** + * @author: @EvolutionX-10 + * @version: 1.1.0-beta + * @description: This is publish plugin, it allows you to publish your slash commands with ease. + * @license: MIT + * @example: + * ```ts + * import { publish } from "../path/to/your/plugin/folder"; + * import { sernModule, CommandType } from "@sern/handler"; + * export default sernModule([publish()], { // put guild id in array for guild commands + * // your code + * }) + * ``` + */ +import { CommandType, PluginType } from "@sern/handler"; +import { ApplicationCommandType } from "discord.js"; +export function publish(guildIds = []) { + return { + type: PluginType.Command, + description: "Manage Slash Commands", + name: "slash-auto-publish", + + async execute({ client }, { absPath, mod: module }, controller) { + function c(e) { + console.error("publish command didnt work for", module.name); + console.error(e); + } + + try { + const commandData = { + type: CommandTypeRaw[module.type], + name: module.name, + description: module.description, + options: optionsTransformer(module.options ?? []), + }; + if (!Array.isArray(guildIds)) guildIds = [guildIds]; + + if (!guildIds.length) { + const cmd = ( + await client.application.commands.fetch() + ).find((c) => c.name === module.name); + + if (cmd) { + if (!cmd.equals(commandData, true)) { + console.log( + `Found differences in global command ${module.name}` + ); + await cmd.edit(commandData).then((c) => { + console.log( + `${module.name} updated with new data successfully!` + ); + }); + } + + return controller.next(); + } + + await client.application.commands + .create(commandData) + .catch(c); + console.log("Command created", module.name); + return controller.next(); + } + + for (const id of guildIds) { + const guild = await client.guilds.fetch(id).catch(c); + if (!guild) continue; + const guildcmd = (await guild.commands.fetch()).find( + (c) => c.name === module.name + ); + + if (guildcmd) { + if (!guildcmd.equals(commandData, true)) { + console.log( + `Found differences in command ${module.name}` + ); + await guildcmd.edit(commandData).catch(c); + console.log( + `${module.name} updated with new data successfully!` + ); + continue; + } + + continue; + } + + await guild.commands.create(commandData).catch(c); + console.log( + "Guild Command created", + module.name, + guild.name + ); + } + + return controller.next(); + } catch (e) { + console.log("Command did not register" + module.name); + console.log(e); + return controller.stop(); + } + }, + }; +} +export function optionsTransformer(ops) { + return ops.map((el) => + el.autocomplete ? (({ command, ...el }) => el)(el) : el + ); +} +export const CommandTypeRaw = { + [CommandType.Both]: ApplicationCommandType.ChatInput, + [CommandType.MenuMsg]: ApplicationCommandType.Message, + [CommandType.MenuUser]: ApplicationCommandType.User, + [CommandType.Slash]: ApplicationCommandType.ChatInput, +};