diff --git a/src/handler/sern/sern.ts b/src/handler/sern/sern.ts index 943104d..0a9b07c 100644 --- a/src/handler/sern/sern.ts +++ b/src/handler/sern/sern.ts @@ -1,7 +1,7 @@ import type { MessagePackage, Nullable, Visibility } from "../../types/handler/handler"; import { CommandType } from "../../types/handler/handler"; import { Files } from "../utils/readFile" -import type { ApplicationCommand, Awaitable, Client, CommandInteraction, Message, MessageInteraction, Util } from "discord.js"; +import type { ApplicationCommand, ApplicationCommandOption, ApplicationCommandOptionData, Awaitable, Client, CommandInteraction, CommandInteractionOption, Message, MessageInteraction, Util } from "discord.js"; import type { possibleOutput } from "../../types/handler/handler" import { Err, Ok, Result, Option, None, Some } from "ts-results"; import type { Utils } from "../utils/preprocessors/args"; @@ -31,7 +31,7 @@ export namespace Sern { if (tryFmt.err) return; const commandName = this.CtxHandler.fmtMsg!.shift()!; const module = Files.Commands.get(commandName) ?? Files.Alias.get(commandName) - let cmdResult = (await this.commandResult(module, message)) + let cmdResult = (await this.commandResult(module?.mod, message)) if (cmdResult === undefined) return; message.channel.send(cmdResult) @@ -46,13 +46,23 @@ export namespace Sern { }) } - private async interactionResult(module: Sern.Module | undefined, interaction: CommandInteraction) : Promise { + private async interactionResult( + module: { mod: Sern.Module, options: ApplicationCommandOptionData[]} | undefined, + interaction: CommandInteraction) : Promise { + if (module === undefined) return "Unknown slash command!"; - module.delegate( - this.CtxHandler.messagePack as Context, - Ok("") - ) - throw Error ("unimplemented"); + const name = Array.from(Files.Commands.keys()).find(it => it === interaction.commandName)!; + + (await this.client.guilds.fetch(this.privateServerId)) + .commands + .create({ + name, + description : module.mod.desc, + options: module.options + }); + + module.mod.delegate({message : None, interaction: Some(interaction)}, Ok(interaction.options) ); + throw Error("unimpl") } private async commandResult(module: Sern.Module | undefined, message: Message) : Promise { @@ -62,7 +72,7 @@ export namespace Sern { } if (module.type === CommandType.SLASH) return `This may be a slash command and not a legacy command` let args = this.CtxHandler.fmtMsg.join(" "); - let parsedArgs = module.parse === undefined ? Ok("") : module.parse(message, args); + let parsedArgs = module.parse === undefined ? Ok("") : module.parse( { message : Some(message), interaction : None }, args); if(parsedArgs.err) return parsedArgs.val; let fn = await module.delegate({interaction : None, message: Some(message)}, parsedArgs) return fn instanceof Object ? fn.val : undefined @@ -115,7 +125,7 @@ export namespace Sern { visibility : Visibility, type: CommandType, delegate : ( eventParams : Context , args: Ok ) => Awaitable | void> - parse? : (message: Message, args: string) => Utils.ArgType + parse? : (message: Context, args: string) => Utils.ArgType } diff --git a/src/handler/utils/readFile.ts b/src/handler/utils/readFile.ts index 9264668..3988044 100644 --- a/src/handler/utils/readFile.ts +++ b/src/handler/utils/readFile.ts @@ -1,12 +1,11 @@ -import type { CommandInteractionOption } from "discord.js"; +import type { ApplicationCommandOptionData, CommandInteractionOption } from "discord.js"; import { readdirSync, statSync } from "fs"; import { basename, join } from "path"; import type { Sern } from "../sern/sern"; export namespace Files { - export const Slash = new Map, options: readonly CommandInteractionOption[] }>(); - export const Commands = new Map>(); - export const Alias = new Map>(); + export const Commands = new Map, options: ApplicationCommandOptionData[] }>(); + export const Alias = new Map, options: ApplicationCommandOptionData[] }>(); //courtesy of Townsy#0001 on Discord async function readPath(dir: string, arrayOfFiles: string[] = []): Promise { @@ -34,22 +33,21 @@ export namespace Files { })).then( async modArr => { for ( const { name, mod, absPath } of modArr) { switch (mod.type) { - case 2 : Commands.set(name.substring(0, name.length-3), mod); break; + case 2 : Commands.set(name.substring(0, name.length-3), { mod, options: [] }); break; case 4 : { - const options = ((await import(absPath)).options as readonly CommandInteractionOption[]) - Slash.set(name.substring(0, name.length - 3), { mod, options }); + const options = ((await import(absPath)).options as ApplicationCommandOptionData[]) + Commands.set(name.substring(0, name.length - 3), { mod, options : options ?? [] }); } break; case 6 : { - Commands.set(name.substring(0, name.length-3), mod); - const options = ((await import(absPath)).options as readonly CommandInteractionOption[]) - Slash.set(name.substring(0, name.length - 3), {mod, options}); + const options = ((await import(absPath)).options as ApplicationCommandOptionData[]) + Commands.set(name.substring(0, name.length-3),{mod, options : options ?? [] } ); } 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) + for ( const alias of mod.alias) { + Alias.set(alias, {mod, options : []}) } } }