diff --git a/src/handler/sern/sern.ts b/src/handler/sern/sern.ts index f44409f..943104d 100644 --- a/src/handler/sern/sern.ts +++ b/src/handler/sern/sern.ts @@ -1,18 +1,18 @@ -import type { MessagePackage, Visibility } from "../../types/handler/handler"; +import type { MessagePackage, Nullable, Visibility } from "../../types/handler/handler"; import { CommandType } from "../../types/handler/handler"; import { Files } from "../utils/readFile" -import type { Awaitable, Client, CommandInteraction, Message, Util } from "discord.js"; +import type { ApplicationCommand, Awaitable, Client, CommandInteraction, 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"; + export namespace Sern { - export class Handler { private wrapper: Sern.Wrapper; - private msgHandler : MsgHandler = new MsgHandler(); + private CtxHandler : CtxHandler = new CtxHandler(); constructor( wrapper : Sern.Wrapper, ) { @@ -25,36 +25,43 @@ export namespace Sern { }) - .on("messageCreate", async message => { - let tryFmt = this.msgHandler.listen({message, prefix: this.prefix}).fmt(); + .on("messageCreate", async message => { + + let tryFmt = this.CtxHandler.listen({ message: Some(message), interaction : None, prefix: this.prefix}).fmt(); if (tryFmt.err) return; - const commandName = this.msgHandler.fmtMsg!.shift()!; + const commandName = this.CtxHandler.fmtMsg!.shift()!; const module = Files.Commands.get(commandName) ?? Files.Alias.get(commandName) let cmdResult = (await this.commandResult(module, message)) if (cmdResult === undefined) return; message.channel.send(cmdResult) + this.CtxHandler.clear(); }) .on("interactionCreate", async interaction => { - if (!interaction.isCommand()) return; - const module = Files.Slash.get(interaction.commandName); - await this.interactionResult(module); - + if(!interaction.isCommand()) return; + const module = Files.Commands.get(interaction.commandName); + await this.interactionResult(module, interaction); + }) } - private async interactionResult(module: Sern.Module | undefined) { - + private async interactionResult(module: Sern.Module | undefined, interaction: CommandInteraction) : Promise { + if (module === undefined) return "Unknown slash command!"; + module.delegate( + this.CtxHandler.messagePack as Context, + Ok("") + ) + throw Error ("unimplemented"); } private async commandResult(module: Sern.Module | undefined, message: Message) : Promise { - if (module === undefined) return "Unknown Command"; + if (module === undefined) return "Unknown legacy command"; if (module.visibility === "private" && message.guildId !== this.privateServerId) { return "This command is not availible in this guild!" } if (module.type === CommandType.SLASH) return `This may be a slash command and not a legacy command` - let args = this.msgHandler.fmtMsg.join(" "); + let args = this.CtxHandler.fmtMsg.join(" "); let parsedArgs = module.parse === undefined ? Ok("") : module.parse(message, args); if(parsedArgs.err) return parsedArgs.val; let fn = await module.delegate({interaction : None, message: Some(message)}, parsedArgs) @@ -114,26 +121,33 @@ export namespace Sern { } -class MsgHandler { +class CtxHandler { - private msg : MessagePackage | null = null; - private resMsg : string[] | null = null; + private msg : Nullable = null; + private resMsg : Nullable = null; - listen (msg : MessagePackage): MsgHandler { + listen (msg : MessagePackage): CtxHandler { this.msg = msg return this; } isCommand() : boolean { - const msg = this.msg!.message.content.trim() - return this.message.author.bot || msg.slice(0, this.prefix.length).toLowerCase() !== this.prefix; - } + const msg = this.msg!.message; + if(msg.some) { + const someMsg = msg.val.content.trim(); + return msg.unwrap().author.bot || someMsg.slice(this.prefix.length).toLowerCase() !== this.prefix + } + return false; + } fmt() : Result { if (this.isCommand()) return Err(void 0); - this.resMsg = this.message.content.slice(this.prefix.length).trim().split(/\s+/g); + this.resMsg = this.message.unwrap().content.slice(this.prefix.length).trim().split(/\s+/g); return Ok(void 0); } + clear() { + this.msg = null; + } get fmtMsg() { return this.resMsg!; diff --git a/src/handler/utils/readFile.ts b/src/handler/utils/readFile.ts index eb6a410..9264668 100644 --- a/src/handler/utils/readFile.ts +++ b/src/handler/utils/readFile.ts @@ -1,9 +1,10 @@ +import type { 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>(); + export const Slash = new Map, options: readonly CommandInteractionOption[] }>(); export const Commands = new Map>(); export const Alias = new Map>(); @@ -29,15 +30,19 @@ export namespace Files { 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) { + return { name : basename(absPath), mod: ( await import(absPath)).default as Sern.Module, absPath } + })).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 4 : Slash.set(name.substring(0, name.length - 3), mod); break; + case 4 : { + const options = ((await import(absPath)).options as readonly CommandInteractionOption[]) + Slash.set(name.substring(0, name.length - 3), { mod, options }); + } break; case 6 : { Commands.set(name.substring(0, name.length-3), mod); - Slash.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}); } break; default : throw Error(`${name}.js is not a valid module type.`); } diff --git a/src/types/handler/handler.ts b/src/types/handler/handler.ts index d710eb2..4a2ba2b 100644 --- a/src/types/handler/handler.ts +++ b/src/types/handler/handler.ts @@ -1,15 +1,18 @@ -import type { Ok, Result } from 'ts-results'; -import type { Awaitable, Client, Message, MessagePayload} from 'discord.js'; +import type { Ok, Result, Option } from 'ts-results'; +import type { Awaitable, Client, CommandInteraction, Message, MessagePayload} from 'discord.js'; import type { MessageOptions } from 'child_process'; import type { Sern } from '../../handler/sern/sern'; export type Visibility = "private" | "public" export type possibleOutput = string | MessagePayload & MessageOptions +export type Nullable = T | null; export type MessagePackage = { - message : Message, + message : Option, + interaction : Option, prefix : string } + export type delegate = Sern.Module["delegate"] export enum CommandType {