diff --git a/.prettierrc b/.prettierrc index 8dc2e72..72dca21 100644 --- a/.prettierrc +++ b/.prettierrc @@ -1,4 +1,5 @@ { "tabWidth": 4, - "useTabs": true + "useTabs": true, + "singleQuote": true } diff --git a/JavaScript/publish.js b/JavaScript/publish.js new file mode 100644 index 0000000..eedc6d6 --- /dev/null +++ b/JavaScript/publish.js @@ -0,0 +1,117 @@ +/** + * @author: Murtatrxx & EvolotionX-10 + * @version: 1.0.0 + * @description: This is the publish plugin, it allows you to publish your slash commands with ease. + * @license: MIT + * @example: + * ```js + * 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 goes here + * }); + * ``` + */ + + 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-js', + async execute(client, 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, +}; + diff --git a/TypeScript/publish.ts b/TypeScript/publish.ts index 12a995a..2de72f5 100644 --- a/TypeScript/publish.ts +++ b/TypeScript/publish.ts @@ -7,31 +7,54 @@ * @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 { 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 goes here + * }); * ``` */ + import { CommandPlugin, CommandType, PluginType, SernOptionsData, -} from "@sern/handler"; -import { ApplicationCommandData, ApplicationCommandType } from "discord.js"; +} from '@sern/handler'; + +import { + ApplicationCommandData, + ApplicationCommandType +} from 'discord.js'; export function publish( guildIds: string | Array = [] ): CommandPlugin { return { type: PluginType.Command, - description: "Manage Slash Commands", - name: "slash-auto-publish", - async execute(client, module, controller) { + description: 'Manage Slash Commands', + name: 'slash-auto-publish-ts', + async execute( + client: { + application: any; + guilds: { + fetch: (arg0: string) => Promise + }; + }, + module: { + name: any; + type: string | number; + description: any; + options: any; + }, + controller: { + next: () => any; + stop: () => any + } + ) { function c(e: unknown) { - console.error("publish command didnt work for", module.name!); + console.error('publish command didnt work for', module.name!); console.error(e); } try { @@ -64,7 +87,7 @@ export function publish( await client .application!.commands.create(commandData) .catch(c); - console.log("Command created", module.name!); + console.log('Command created', module.name!); return controller.next(); } @@ -89,14 +112,14 @@ export function publish( } await guild.commands.create(commandData).catch(c); console.log( - "Guild Command created", + 'Guild Command created', module.name!, guild.name ); } return controller.next(); } catch (e) { - console.log("Command did not register" + module.name!); + console.log('Command did not register' + module.name!); console.log(e); return controller.stop(); }