From 35d6c278931cee73f1f4d97753f46409fe0b1b09 Mon Sep 17 00:00:00 2001 From: jacoobes Date: Thu, 10 Feb 2022 10:40:37 -0600 Subject: [PATCH] reloading of slash commands (not tested) --- src/handler/sern.ts | 53 +++++++++++++++++++++++++++++++++-- src/handler/utils/readFile.ts | 51 +++++++++------------------------ 2 files changed, 64 insertions(+), 40 deletions(-) diff --git a/src/handler/sern.ts b/src/handler/sern.ts index c6a651f..584e2d5 100644 --- a/src/handler/sern.ts +++ b/src/handler/sern.ts @@ -1,6 +1,6 @@ import type { Arg, Context, Visibility } from "../types/handler"; import * as Files from "./utils/readFile" -import type { Awaitable, Client, CommandInteraction, Message } from "discord.js"; +import type { ApplicationCommandOptionData, Awaitable, Client, CommandInteraction, Message } from "discord.js"; import type { possibleOutput } from "../types/handler" import { Ok, Result, None, Some } from "ts-results"; import type * as Utils from "./utils/preprocessors/args"; @@ -24,7 +24,8 @@ export class Handler { this.wrapper.client .on("ready", async () => { - await Files.registerModules(this); + Files.buildData(this) + .then(this.registerModules); if (this.wrapper.init !== undefined) this.wrapper.init(this); }) @@ -98,6 +99,54 @@ export class Handler { const fn = await module.mod.delegate(context, parsedArgs) return fn?.val } + private async registerModules(modArr : {name: string, mod: Module, absPath: string}[]) { + for (const { name, mod, absPath } of modArr) { + const { cmdName, testOnly } = Files.fmtFileName(name); + switch (mod.type) { + case 1: Files.Commands.set(cmdName, { mod, options: [], testOnly }); break; + case 2: + case (1 | 2): { + const options = ((await import(absPath)).options as ApplicationCommandOptionData[]) + Files.Commands.set(cmdName, { mod, options: options ?? [], testOnly }); + switch(mod.visibility) { + case "private" : { + await this.reloadSlash(cmdName, mod.desc, options) + } + case "public" : { + this.client.application!.commands + .create({ + name: cmdName, + description: mod.desc, + options + }) + } + } + } break; + default: throw Error(`${name}.js is not a valid module type.`); + } + + if (mod.alias.length > 0) { + for (const alias of mod.alias) { + Files.Alias.set(alias, { mod, options: [], testOnly }) + } + } + } + } + private async reloadSlash( + cmdName: string, + description : string, + options: ApplicationCommandOptionData[] + ) { + for(const {id} of this.privateServers) { + const guild = (await this.client.guilds.fetch(id)); + + guild.commands.create({ + name: cmdName, + description, + options + }) + } + } /** * @readonly * @returns {string} prefix used for legacy commands diff --git a/src/handler/utils/readFile.ts b/src/handler/utils/readFile.ts index c8afc7a..53e3dc5 100644 --- a/src/handler/utils/readFile.ts +++ b/src/handler/utils/readFile.ts @@ -24,51 +24,26 @@ async function readPath(dir: string, arrayOfFiles: string[] = []): Promise { +export const fmtFileName = (n : string) => { const endsW = n.toLowerCase().endsWith("-test.js") || n.toLowerCase().endsWith("-test.ts"); return endsW ? { cmdName : n.substring(0, n.length - 8), testOnly : true } : { cmdName: n.substring(0, n.length - 3), testOnly: false}; }; -export async function registerModules(handler: Sern.Handler): Promise { - const commandDir = handler.commandDir; - Promise.all((await getCommands(commandDir)).map(async absPath => { +export async function buildData(handler: Sern.Handler) + : Promise<{ + name: string; + mod: Sern.Module; + absPath: string; + }[]> { + const commandDir = handler.commandDir; + return Promise.all((await getCommands(commandDir)) + .map(async absPath => { return { name: basename(absPath), mod: (await import(absPath)).default as Sern.Module, absPath } - })).then(async modArr => { - for (const { name, mod, absPath } of modArr) { - const { cmdName, testOnly } = fmtFileName(name); - switch (mod.type) { - case 1: Commands.set(cmdName, { mod, options: [], testOnly }); break; - case 2: - case (1 | 2): { - const options = ((await import(absPath)).options as ApplicationCommandOptionData[]) - Commands.set(cmdName, { mod, options: options ?? [], testOnly }); - switch(mod.visibility) { - case "private" : { - //unimplemented - } - case "public" : { - //unimplemented - } - } - } break; - default: throw Error(`${name}.js is not a valid module type.`); - } - - if (mod.alias.length > 0) { - for (const alias of mod.alias) { - Alias.set(alias, { mod, options: [], testOnly }) - } - } - } - - }) - + })) } + export async function getCommands(dir: string): Promise { return readPath(join(process.cwd(), dir)) -} - - - +} \ No newline at end of file