From 2c8bcd33e1ad596249ee2b7e297a0ff107bb4f06 Mon Sep 17 00:00:00 2001 From: GRZ4NA Date: Wed, 8 Sep 2021 22:04:53 +0200 Subject: [PATCH] rewritten CommandManager --- src/commands/CommandManager.ts | 62 ++++++++++++++++++++++++++++++++-- 1 file changed, 59 insertions(+), 3 deletions(-) diff --git a/src/commands/CommandManager.ts b/src/commands/CommandManager.ts index 74fdca3..6f18454 100644 --- a/src/commands/CommandManager.ts +++ b/src/commands/CommandManager.ts @@ -1,5 +1,7 @@ import { Interaction, Message } from "discord.js"; -import { applicationState } from "state.js"; +import { TargetID } from "structures/Parameter.js"; +import { CommandNotFound } from "../errors.js"; +import { applicationState } from "../state.js"; import { BaseCommand } from "./BaseCommand.js"; import { ChatCommand } from "./ChatCommand.js"; import { MessageCommand } from "./MessageCommand.js"; @@ -92,7 +94,61 @@ export class CommandManager { } } - public fetch(interaction: T): CommandInteractionData | null { - return null; + public fetch(i: Interaction | Message): CommandInteractionData | null { + if (i instanceof Interaction) { + if (i.isCommand()) { + const cmd = this.get(i.commandName, "CHAT"); + if (cmd) { + const args = cmd.processArguments(i.options.data.map((d) => d.value || null)); + return { + command: cmd, + parameters: args, + }; + } else { + throw new CommandNotFound(i.commandName); + } + } else if (i.isContextMenu()) { + const cmd = this.get(i.commandName, "MESSAGE"); + if (cmd) { + const target = new TargetID(i.targetId, i.targetType); + return { + command: cmd, + parameters: target, + }; + } else { + throw new CommandNotFound(i.commandName); + } + } else { + return null; + } + } else if (this.prefix && i instanceof Message) { + if (i.content.startsWith(this.prefix)) { + const cmdName = i.content.replace(this.prefix, "").split(" ")[0]; + const cmd = this.get(cmdName, "CHAT"); + if (cmd) { + const argsRaw = i.content + .replace(`${this.prefix}${cmdName} `, "") + .split(this.argumentSeparator) + .map((a) => { + if (a.startsWith(" ")) { + return a.replace(" ", ""); + } else { + return a; + } + }); + const args = cmd.processArguments(argsRaw); + return { + command: cmd, + parameters: args, + }; + } else { + throw new CommandNotFound(cmdName); + } + } else { + return null; + } + } else { + return null; + } } }