From 8158996101dc7d71f9705b9d1b7b3794f9923fc4 Mon Sep 17 00:00:00 2001 From: jacoobes Date: Sun, 30 Jan 2022 21:02:16 -0600 Subject: [PATCH] working on slash commands --- .gitignore | 4 ++- src/handler/sern/sern.ts | 56 +++++++++++++++++++++++++---------- src/handler/utils/readFile.ts | 8 +++-- 3 files changed, 48 insertions(+), 20 deletions(-) diff --git a/.gitignore b/.gitignore index f75efae..81ce2e3 100644 --- a/.gitignore +++ b/.gitignore @@ -75,4 +75,6 @@ typings/ # FuseBox cache .fusebox/ -dist \ No newline at end of file +dist + +.vs \ No newline at end of file diff --git a/src/handler/sern/sern.ts b/src/handler/sern/sern.ts index e562919..c102e97 100644 --- a/src/handler/sern/sern.ts +++ b/src/handler/sern/sern.ts @@ -1,9 +1,9 @@ import type { MessagePackage, Visibility } from "../../types/handler/handler"; import { CommandType } from "../../types/handler/handler"; import { Files } from "../utils/readFile" -import type { Awaitable, Client, Message, Util } from "discord.js"; +import type { Awaitable, Client, CommandInteraction, Message, Util } from "discord.js"; import type { possibleOutput } from "../../types/handler/handler" -import { Err, Ok, Result } from "ts-results"; +import { Err, Ok, Result, Option, None, Some } from "ts-results"; import type { Utils } from "../utils/preprocessors/args"; @@ -20,10 +20,7 @@ export namespace Sern { this.wrapper.client .on("ready", async () => { if (this.wrapper.init !== undefined) this.wrapper.init(); - Files.registerModules(this.wrapper.commands) - .then( (_ : void)=> { - /// register slash commands - }); + Files.registerModules(this) }) .on("messageCreate", async message => { @@ -36,6 +33,11 @@ export namespace Sern { message.channel.send(cmdResult) }) + + .on("interactionCreate", async interaction => { + if (!interaction.isCommand()) return; + const module = Files.Commands.get(interaction.commandName); + }) } private async commandResult(module: Sern.Module | undefined, message: Message) : Promise { @@ -47,35 +49,57 @@ export namespace Sern { let args = this.msgHandler.fmtMsg.join(" "); let parsedArgs = module.parse === undefined ? Ok("") : module.parse(message, args); if(parsedArgs.err) return parsedArgs.val; - let fn = await module.delegate(message, parsedArgs) + let fn = await module.delegate({interaction : None, message: Some(message)}, parsedArgs) return fn instanceof Object ? fn.val : undefined } - get prefix() { + get prefix() : string { return this.wrapper.prefix; } - - private get privateServerId() { + get commandDir() : string { + return this.wrapper.commands; + } + get client() : Client { + return this.wrapper.client + } + get privateServerId() { return this.wrapper.privateServerId; } } - + /** + * An object to be passed into Sern.Handler constructor. + * ```ts + * new Sern.Handler({ + * client, // Discord.js client instance + * prefix : "!", // an example prefix + * commands: "", //commands directory + * init : () => console.log("Bot is ready") // function called on ready + * privateServerId : "" // a server id that can be used for private or test server + * }) + * ``` + */ export interface Wrapper { - client : Client, - prefix: string, - commands : string + readonly client : Client, + readonly prefix: string, + readonly commands : string init? : () => void, - privateServerId : string + readonly privateServerId : string } + type Context = { + message : Option, + interaction : Option + } + + export interface Module { alias: string[], desc : string, visibility : Visibility, type: CommandType, - delegate : (message: Message, args: Ok ) => Awaitable | void> + delegate : ( eventParams : Context , args: Ok ) => Awaitable | void> parse? : (message: Message, args: string) => Utils.ArgType } diff --git a/src/handler/utils/readFile.ts b/src/handler/utils/readFile.ts index 939ee88..cc3488a 100644 --- a/src/handler/utils/readFile.ts +++ b/src/handler/utils/readFile.ts @@ -24,12 +24,14 @@ export namespace Files { return arrayOfFiles; } - export async function registerModules(dir: string) : Promise { - Promise.all((await getCommands(dir)).map(async absPath => { + export async function registerModules(handler : Sern.Handler) : Promise { + const commandDir = handler.commandDir, + client = handler.client; + Promise.all((await getCommands(commandDir)).map(async absPath => { return { name : basename(absPath), mod: ( await import(absPath)).default as Sern.Module } })).then( modArr => { for ( const { name, mod } of modArr) { - Commands.set(name.substring(0, name.length-3), mod) + Commands.set(name.substring(0, name.length-3), mod); if(mod.alias.length > 0) { for( const alias of mod.alias) { Alias.set(alias, mod)