reloading of slash commands (not tested)

This commit is contained in:
jacoobes
2022-02-10 10:40:37 -06:00
parent 7ca83141d8
commit 35d6c27893
2 changed files with 64 additions and 40 deletions

View File

@@ -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<unknown>, 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

View File

@@ -24,51 +24,26 @@ async function readPath(dir: string, arrayOfFiles: string[] = []): Promise<strin
return arrayOfFiles;
}
const fmtFileName = (n : string) => {
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<void> {
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<unknown>;
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<unknown>, 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<string[]> {
return readPath(join(process.cwd(), dir))
}
}