MessageCommandManager

This commit is contained in:
mmacierzanka committed 2021-09-07 19:13:21 +02:00
1 parent 6ab0874a1a
commit 2d9d0dbd66
5 files changed
+59 -6

No files matched your search

+40
View File
@@ -0,0 +1,40 @@
import { CommandInteraction } from "discord.js";
import { CommandInteractionData } from "../types/commands";
import { MessageCommand } from "../structures/MessageCommand";
import { CommandManager } from "./CommandManager.js";
import { CommandNotFound } from "errors";
import { ObjectParameter, Parameter } from "structures/Parameter";
export class MessageCommandManager extends CommandManager {
protected readonly _list: MessageCommand[] = [];
constructor() {
super();
}
public add(command: MessageCommand) {
if (MessageCommand.isCommand(command)) {
super.add(command);
} else {
console.error("[❌ ERROR] Incorrect argument type");
}
}
public fetch(i: CommandInteraction): CommandInteractionData | null {
if (!i.isContextMenu() || i.targetType !== "MESSAGE") return null;
const command = this.get(i.commandName);
if (!command) {
throw new CommandNotFound(i.commandName);
}
const parameter = new Parameter({
name: "target",
optional: false,
type: "target",
});
const inputParameter = new ObjectParameter(parameter, i.targetId);
return {
command: command,
parameters: [inputParameter],
};
}
}
+13 -5
View File
@@ -6,15 +6,16 @@ import { ChatCommandManager } from "../managers/ChatCommandManager.js";
import { OperationSuccess, PermissionsError } from "../errors.js";
import { HelpMessage } from "./Help.js";
import { SystemMessageManager } from "../managers/SystemMessage.js";
import { CommandMessageStructure } from "../types/ChatCommand.js";
import { CommandInteractionData } from "../types/commands.js";
import { InitOptions } from "../types/Bot.js";
import { HelpMessageParams } from "../types/HelpMessage.js";
import { applicationState } from "../state.js";
import { MessageCommandManager } from "../managers/MessageCommandManager.js";
export declare interface Bot {
on(event: "READY", listener: Function): this;
on(event: "MESSAGE", listener: (m: Message) => void): this;
on(event: "COMMAND", listener: (m: Message | CommandInteraction, cmdMsg: CommandMessageStructure) => void): this;
on(event: "COMMAND", listener: (m: Message | CommandInteraction, cmdMsg: CommandInteractionData) => void): this;
on(event: "ERROR", listener: (e: any) => void): this;
}
@@ -36,11 +37,17 @@ export class Bot extends EventEmitter {
public readonly client: Client;
/**
* Instance command manager
* Instance command manager for chat commands
* @type {ChatCommandManager}
*/
public readonly chatCommands: ChatCommandManager;
/**
* Instance command manager for message context menu commands
* @type {MessageCommandManager}
*/
public readonly messageCommands: MessageCommandManager;
/**
* Discord bot token
* @type {string}
@@ -99,6 +106,7 @@ export class Bot extends EventEmitter {
}
);
this.chatCommands = new ChatCommandManager(options.prefix, options.parameterSeparator);
this.messageCommands = new MessageCommandManager();
this.token = options.token;
this.applicationId = options.applicationId;
this.messages = {
@@ -157,7 +165,7 @@ export class Bot extends EventEmitter {
this.emit("READY");
});
this.client.on("messageCreate", async (m) => {
let cmdMsg: CommandMessageStructure | null = null;
let cmdMsg: CommandInteractionData | null = null;
try {
cmdMsg = this.chatCommands.fetch(m);
if (cmdMsg) {
@@ -198,7 +206,7 @@ export class Bot extends EventEmitter {
}
});
this.client.on("interactionCreate", async (i) => {
let cmd: CommandMessageStructure | null = null;
let cmd: CommandInteractionData | null = null;
try {
if (!i.isCommand() && !i.isContextMenu()) return;
cmd = this.chatCommands.fetch(i);
+2
View File
@@ -126,6 +126,8 @@ export class ChatCommand extends BaseCommand {
case "number":
type = 10;
break;
case "target":
throw new Error(`"target" parameter cannot be used in chat commands`);
default:
type = 3;
break;
+3
View File
@@ -156,6 +156,9 @@ export class ObjectID {
export class NullParameter extends InputParameter {
constructor(parameter: Parameter) {
if (parameter.optional === false) {
throw new Error(`Parameter "${parameter.name}" cannot be null`);
}
super(parameter, null);
}
}
+1 -1
View File
@@ -1,6 +1,6 @@
import { ObjectID } from "../structures/Parameter.js";
export type ParameterType = "string" | "boolean" | "number" | "user" | "role" | "channel" | "mentionable";
export type ParameterType = "string" | "boolean" | "number" | "user" | "role" | "channel" | "mentionable" | "target";
export type ParameterResolvable = string | boolean | number | ObjectID | null;