adding slash command map

This commit is contained in:
jacoobes
2022-01-31 23:06:02 -06:00
parent 8158996101
commit 2e5942cb11
2 changed files with 23 additions and 5 deletions

View File

@@ -20,7 +20,9 @@ export namespace Sern {
this.wrapper.client
.on("ready", async () => {
if (this.wrapper.init !== undefined) this.wrapper.init();
Files.registerModules(this)
await Files.registerModules(this);
})
.on("messageCreate", async message => {
@@ -36,10 +38,16 @@ export namespace Sern {
.on("interactionCreate", async interaction => {
if (!interaction.isCommand()) return;
const module = Files.Commands.get(interaction.commandName);
const module = Files.Slash.get(interaction.commandName);
await this.interactionResult(module);
})
}
private async interactionResult(module: Sern.Module<unknown> | undefined) {
}
private async commandResult(module: Sern.Module<unknown> | undefined, message: Message) : Promise<possibleOutput| undefined> {
if (module === undefined) return "Unknown Command";
if (module.visibility === "private" && message.guildId !== this.privateServerId) {

View File

@@ -3,8 +3,9 @@ import { basename, join } from "path";
import type { Sern } from "../sern/sern";
export namespace Files {
export const Commands = new Map<string, Sern.Module<unknown> >()
export const Alias = new Map<string, Sern.Module<unknown>>()
export const Slash = new Map<string, Sern.Module<unknown>>();
export const Commands = new Map<string, Sern.Module<unknown>>();
export const Alias = new Map<string, Sern.Module<unknown>>();
//courtesy of Townsy#0001 on Discord
async function readPath(dir: string, arrayOfFiles: string[] = []): Promise<string[]> {
@@ -31,7 +32,16 @@ export namespace Files {
return { name : basename(absPath), mod: ( await import(absPath)).default as Sern.Module<unknown> }
})).then( modArr => {
for ( const { name, mod } of modArr) {
Commands.set(name.substring(0, name.length-3), mod);
switch (mod.type) {
case 2 : Commands.set(name.substring(0, name.length-3), mod); break;
case 4 : Slash.set(name.substring(0, name.length - 3), mod); break;
case 6 : {
Commands.set(name.substring(0, name.length-3), mod);
Slash.set(name.substring(0, name.length - 3), mod);
} 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)