From ced05ef4062fd665e3de53ed667f978ea65e26ec Mon Sep 17 00:00:00 2001 From: EvolutionX Date: Fri, 19 Aug 2022 08:59:14 +0530 Subject: [PATCH] perf: improve publish --- src/commands/ping.ts | 10 ++++---- src/index.ts | 2 +- src/plugins/publish.ts | 57 +++++++++++++++++++++++------------------- 3 files changed, 37 insertions(+), 32 deletions(-) diff --git a/src/commands/ping.ts b/src/commands/ping.ts index a843d3f..5e28532 100644 --- a/src/commands/ping.ts +++ b/src/commands/ping.ts @@ -1,10 +1,10 @@ -import { commandModule, CommandType } from '@sern/handler'; -import { publish } from '../plugins/publish'; +import { commandModule, CommandType } from "@sern/handler"; +import { publish } from "../plugins/publish"; export default commandModule({ type: CommandType.Slash, - plugins: [ publish() ], - description: 'Pong!', + plugins: [publish()], + description: "Pong!", execute: async (context) => { - await context.reply('Pong 🏓'); + await context.reply(`Pong 🏓 \`${context.client.ws.ping}ms\``); }, }); diff --git a/src/index.ts b/src/index.ts index 05358de..dec3679 100644 --- a/src/index.ts +++ b/src/index.ts @@ -16,7 +16,7 @@ const client = new Client({ Sern.init({ client, sernEmitter: new SernEmitter(), - defaultPrefix: "!sern", + defaultPrefix: "sern", commands: "dist/src/commands", events: "dist/src/events", }); diff --git a/src/plugins/publish.ts b/src/plugins/publish.ts index 091d65e..94f553f 100644 --- a/src/plugins/publish.ts +++ b/src/plugins/publish.ts @@ -4,16 +4,25 @@ import { PluginType, SernOptionsData, } from "@sern/handler"; -import { ApplicationCommandData, ApplicationCommandType } from "discord.js"; +import { + ApplicationCommandData, + ApplicationCommandType, + PermissionResolvable, +} from "discord.js"; export function publish( - guildIds: string | Array = [] + options: PublishOptions = { + guildIds: [], + dmPermission: true, + defaultMemberPermissions: null, + } ): CommandPlugin { return { type: PluginType.Command, description: "Manage Slash Commands", name: "slash-auto-publish", - async execute({client}, {mod: module}, controller) { + async execute({ client }, { mod: module }, controller) { + let { defaultMemberPermissions, dmPermission, guildIds } = options; function c(e: unknown) { console.error("publish command didnt work for", module.name!); console.error(e); @@ -24,19 +33,19 @@ export function publish( name: module.name!, description: module.description, options: optionsTransformer(module.options ?? []), + defaultMemberPermissions, + dmPermission, }; if (!Array.isArray(guildIds)) guildIds = [guildIds]; if (!guildIds.length) { - const cmd = ( - await client.application!.commands.fetch() - ).find((c) => c.name === module.name); + 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(`Found differences in global command ${module.name}`); + cmd.edit(commandData).then(() => { console.log( `${module.name} updated with new data successfully!` ); @@ -45,10 +54,8 @@ export function publish( return controller.next(); } - await client - .application!.commands.create(commandData) - .catch(c); console.log("Command created", module.name!); + client.application!.commands.create(commandData).catch(c); return controller.next(); } @@ -60,23 +67,15 @@ export function publish( ); 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!` - ); + console.log(`Found differences in command ${module.name}`); + console.log(`${module.name} updated with new data successfully!`); + guildcmd.edit(commandData).catch(c); continue; } continue; } - await guild.commands.create(commandData).catch(c); - console.log( - "Guild Command created", - module.name!, - guild.name - ); + console.log("Guild Command created", module.name!, guild.name); + guild.commands.create(commandData).catch(c); } return controller.next(); } catch (e) { @@ -100,3 +99,9 @@ export const CommandTypeRaw = { [CommandType.MenuUser]: ApplicationCommandType.User, [CommandType.Slash]: ApplicationCommandType.ChatInput, } as const; + +interface PublishOptions { + guildIds: string[]; + defaultMemberPermissions: PermissionResolvable | null; + dmPermission: boolean; +}