SlashCmd Map, export options

This commit is contained in:
jacoobes
2022-02-01 11:43:14 -06:00
parent 2e5942cb11
commit 4606ade698
3 changed files with 54 additions and 32 deletions

View File

@@ -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<unknown> | undefined) {
private async interactionResult(module: Sern.Module<unknown> | undefined, interaction: CommandInteraction) : Promise<possibleOutput | undefined> {
if (module === undefined) return "Unknown slash command!";
module.delegate(
this.CtxHandler.messagePack as Context,
Ok("")
)
throw Error ("unimplemented");
}
private async commandResult(module: Sern.Module<unknown> | undefined, message: Message) : Promise<possibleOutput| undefined> {
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<MessagePackage> = null;
private resMsg : Nullable<string[]> = 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<void, void> {
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!;

View File

@@ -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<string, Sern.Module<unknown>>();
export const Slash = new Map<string, { mod: Sern.Module<unknown>, options: readonly CommandInteractionOption[] }>();
export const Commands = new Map<string, Sern.Module<unknown>>();
export const Alias = new Map<string, Sern.Module<unknown>>();
@@ -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<unknown> }
})).then( modArr => {
for ( const { name, mod } of modArr) {
return { name : basename(absPath), mod: ( await import(absPath)).default as Sern.Module<unknown>, 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.`);
}

View File

@@ -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> = T | null;
export type MessagePackage = {
message : Message,
message : Option<Message>,
interaction : Option<CommandInteraction>,
prefix : string
}
export type delegate = Sern.Module<unknown>["delegate"]
export enum CommandType {