From f1ed375cc25ab6dec4c0fed5facd0b90762b7213 Mon Sep 17 00:00:00 2001 From: SrIzan10 <66965250+SrIzan10@users.noreply.github.com> Date: Sat, 19 Nov 2022 13:54:03 +0100 Subject: [PATCH] feat: update publish plugin --- commands/fun/cursivify.ts | 2 +- src/plugins/publish.ts | 36 ++++-- src/plugins/publish_contextmenu.ts | 174 ----------------------------- 3 files changed, 30 insertions(+), 182 deletions(-) delete mode 100644 src/plugins/publish_contextmenu.ts diff --git a/commands/fun/cursivify.ts b/commands/fun/cursivify.ts index d91a30c..f70ca70 100644 --- a/commands/fun/cursivify.ts +++ b/commands/fun/cursivify.ts @@ -1,5 +1,5 @@ import { commandModule, CommandType } from '@sern/handler'; -import { publish } from '../../src/plugins/publish_contextmenu.js'; +import { publish } from '../../src/plugins/publish.js'; /* import { publish } from "../../src/plugins/publish.js"; import { ownerOnly } from "../../src/plugins/ownerOnly.js" diff --git a/src/plugins/publish.ts b/src/plugins/publish.ts index ec63167..da282cd 100644 --- a/src/plugins/publish.ts +++ b/src/plugins/publish.ts @@ -1,9 +1,9 @@ // @ts-nocheck /** - * This is publish plugin, it allows you to publish your slash commands with ease. + * This is publish plugin, it allows you to publish your application commands with ease. * * @author @EvolutionX-10 [<@697795666373640213>] - * @version 1.2.3 + * @version 1.3.0 * @example * ```ts * import { publish } from "../plugins/publish"; @@ -22,6 +22,7 @@ import { CommandType, PluginType, SernOptionsData, + SlashCommand, } from "@sern/handler"; import { ApplicationCommandData, @@ -31,7 +32,12 @@ import { export function publish( options?: PublishOptions -): CommandPlugin { +): CommandPlugin< + | CommandType.Slash + | CommandType.Both + | CommandType.MenuMsg + | CommandType.MenuUser +> { return { type: PluginType.Command, description: "Manage Slash Commands", @@ -56,8 +62,18 @@ export function publish( const commandData = { type: CommandTypeRaw[module.type], name: module.name!, - description: module.description, - options: optionsTransformer(module.options ?? []), + description: [CommandType.Slash, CommandType.Both].includes( + module.type + ) + ? module.description + : undefined, + options: [CommandType.Slash, CommandType.Both].includes( + module.type + ) + ? optionsTransformer( + (module as SlashCommand).options ?? [] + ) + : [], defaultMemberPermissions, dmPermission, } as ApplicationCommandData; @@ -65,7 +81,11 @@ export function publish( if (!guildIds.length) { const cmd = ( await client.application!.commands.fetch() - ).find((c) => c.name === module.name); + ).find( + (c) => + c.name === module.name && + c.type === CommandTypeRaw[module.type] + ); if (cmd) { if (!cmd.equals(commandData, true)) { console.log( @@ -92,7 +112,9 @@ export function publish( const guild = await client.guilds.fetch(id).catch(c); if (!guild) continue; const guildcmd = (await guild.commands.fetch()).find( - (c) => c.name === module.name + (c) => + c.name === module.name && + c.type === CommandTypeRaw[module.type] ); if (guildcmd) { if (!guildcmd.equals(commandData, true)) { diff --git a/src/plugins/publish_contextmenu.ts b/src/plugins/publish_contextmenu.ts deleted file mode 100644 index b4c057c..0000000 --- a/src/plugins/publish_contextmenu.ts +++ /dev/null @@ -1,174 +0,0 @@ -// @ts-nocheck -/** - * This is publish plugin, it allows you to publish your slash commands with ease. - * - * @author @EvolutionX-10 [<@697795666373640213>] - * @version 1.2.3 - * @example - * ```ts - * import { publish } from "../plugins/publish"; - * import { commandModule } from "@sern/handler"; - * export default commandModule({ - * plugins: [ publish() ], // put an object containing permissions, ids for guild commands, boolean for dmPermission - * // plugins: [ publish({ guildIds: ['guildId'], defaultMemberPermissions: 'Administrator'})] - * execute: (ctx) => { - * //your code here - * } - * }) - * ``` - */ -import { - CommandPlugin, - CommandType, - PluginType, - SernOptionsData, -} from "@sern/handler"; -import { - ApplicationCommandData, - ApplicationCommandType, - PermissionResolvable, -} from "discord.js"; - -export function publish( - options?: PublishOptions -): CommandPlugin { - return { - type: PluginType.Command, - description: "Manage Slash Commands", - name: "slash-auto-publish", - async execute({ client }, { mod: module }, controller) { - const defaultOptions = { - guildIds: [], - dmPermission: undefined, - defaultMemberPermissions: null, - }; - - options = { ...defaultOptions, ...options } as PublishOptions & - ValidPublishOptions; - let { defaultMemberPermissions, dmPermission, guildIds } = - options as unknown as ValidPublishOptions; - - function c(e: unknown) { - console.error("publish command didnt work for", module.name!); - console.error(e); - } - try { - const commandData = { - type: CommandTypeRaw[module.type], - name: module.name!, - options: optionsTransformer(module.options ?? []), - defaultMemberPermissions, - dmPermission, - } as ApplicationCommandData; - - 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}` - ); - cmd.edit(commandData).then(() => { - console.log( - `${module.name} updated with new data successfully!` - ); - }); - } - return controller.next(); - } - client - .application!.commands.create(commandData) - .then(() => { - console.log("Command created", module.name!); - }) - .catch(c); - 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}` - ); - guildcmd - .edit(commandData) - .then(() => - console.log( - `${module.name} updated with new data successfully!` - ) - ) - .catch(c); - continue; - } - continue; - } - guild.commands - .create(commandData) - .then(() => - console.log( - "Guild Command created", - module.name!, - guild.name - ) - ) - .catch(c); - } - return controller.next(); - } catch (e) { - console.log("Command did not register" + module.name!); - console.log(e); - return controller.stop(); - } - }, - }; -} - -export function optionsTransformer(ops: Array) { - 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, -} as const; - -export type NonEmptyArray = [T, ...T[]]; - -export interface ValidPublishOptions { - guildIds: string[]; - dmPermission: boolean; - defaultMemberPermissions: PermissionResolvable; -} -interface GuildPublishOptions { - guildIds?: NonEmptyArray; - defaultMemberPermissions?: PermissionResolvable; - dmPermission?: never; -} -interface GlobalPublishOptions { - defaultMemberPermissions?: PermissionResolvable; - dmPermission?: false; - guildIds?: never; -} - -type BasePublishOptions = GuildPublishOptions | GlobalPublishOptions; - -export type PublishOptions = BasePublishOptions & - ( - | Required> - | ( - | Required> - | Required> - ) - );