Bot event procedure rework and import fixes

Known issues: throwing an error causes the bot to crash, context menu interaction doesn't seem to actually work
This commit is contained in:
mmacierzanka committed 2021-09-07 20:05:32 +02:00
1 parent 191e213dcf
commit 3004787cf0
6 files changed
+42 -39

No files matched your search

+2 -2
View File
@@ -1,5 +1,5 @@
import { CommandInteraction, Message } from "discord.js";
import { CommandNotFound, MissingParameterError } from "../errors";
import { CommandNotFound, MissingParameterError } from "../errors.js";
import { BooleanParameter, InputParameter, NullParameter, NumberParameter, ObjectParameter, Parameter, StringParameter } from "../structures/Parameter.js";
import { CommandInteractionData } from "../types/commands.js";
import { ChatCommand } from "../structures/ChatCommand.js";
@@ -96,7 +96,7 @@ export class ChatCommandManager extends CommandManager {
throw new CommandNotFound(messageContentRaw[0]);
}
}
} else if (i instanceof CommandInteraction && i.isCommand()) {
} else if (i instanceof CommandInteraction) {
const command = this.get(i.commandName);
if (command) {
if (command.parameters.length > 0) {
+2 -2
View File
@@ -1,5 +1,5 @@
import { applicationState } from "../state";
import { BaseCommand } from "../structures/BaseCommand";
import { applicationState } from "../state.js";
import { BaseCommand } from "../structures/BaseCommand.js";
export class CommandManager {
protected readonly _list: BaseCommand[] = [];
+6 -6
View File
@@ -1,9 +1,9 @@
import { CommandInteraction } from "discord.js";
import { CommandInteractionData } from "../types/commands";
import { MessageCommand } from "../structures/MessageCommand";
import { Interaction } from "discord.js";
import { CommandInteractionData } from "../types/commands.js";
import { MessageCommand } from "../structures/MessageCommand.js";
import { CommandManager } from "./CommandManager.js";
import { CommandNotFound } from "errors";
import { ObjectParameter, Parameter } from "structures/Parameter";
import { CommandNotFound } from "../errors.js";
import { ObjectParameter, Parameter } from "../structures/Parameter.js";
export class MessageCommandManager extends CommandManager {
protected readonly _list: MessageCommand[] = [];
@@ -20,7 +20,7 @@ export class MessageCommandManager extends CommandManager {
}
}
public fetch(i: CommandInteraction): CommandInteractionData | null {
public fetch(i: Interaction): CommandInteractionData | null {
if (!i.isContextMenu() || i.targetType !== "MESSAGE") return null;
const command = this.get(i.commandName);
if (!command) {
+14 -12
View File
@@ -1,4 +1,4 @@
import { CommandInteraction, Message, Permissions, ReplyMessageOptions, MessageEmbed, GuildMember } from "discord.js";
import { Interaction, Message, Permissions, ReplyMessageOptions, MessageEmbed, GuildMember } from "discord.js";
import { PermissionCheckTypes } from "../types/permissions.js";
import { CommandType, BaseCommandInit, CommandFunction } from "../types/BaseCommand.js";
import { InputParameter } from "./Parameter.js";
@@ -34,7 +34,7 @@ export class BaseCommand {
* Command permissions (if *undefined*, no permissions check will be performed)
* @type {Permissions | Function}
*/
public readonly permissions?: Permissions | ((i: Message | CommandInteraction) => boolean);
public readonly permissions?: Permissions | ((i: Message | Interaction) => boolean);
/**
* Whether to send a SUCCESS message if no other response is defined (default: true)
@@ -48,7 +48,7 @@ export class BaseCommand {
* @private
*/
private readonly function: CommandFunction;
private readonly permissionChecker: (i: Message | CommandInteraction) => boolean;
private readonly permissionChecker: (i: Message | Interaction) => boolean;
public static nameRegExp: RegExp = /^.{1,32}$/;
/**
@@ -90,13 +90,14 @@ export class BaseCommand {
/**
* Invoke the command
* @param {Message | CommandInteraction} [interaction] - Used to check caller's permissions. It will get passed to the execution function (specified in *function* property of command's constructor)
* @param {Message | Interaction} [interaction] - Used to check caller's permissions. It will get passed to the execution function (specified in *function* property of command's constructor)
* @param {InputParameter[]} [cmdParams] - list of processed parameters passed in a Discord interaction
* @returns {Promise<void>}
*/
public async start(interaction: Message | CommandInteraction, args?: InputParameter[]): Promise<void> {
public async start(interaction: Message | Interaction, args?: InputParameter[]): Promise<void> {
if (interaction instanceof Interaction && !interaction.isCommand() && !interaction.isContextMenu()) return;
if (this.permissionChecker(interaction)) {
if (interaction instanceof CommandInteraction) {
if (interaction instanceof Interaction) {
await interaction.deferReply();
}
await this.handleReply(interaction, await this.function.call(this, interaction, this.createAccessor(args || [])));
@@ -124,25 +125,26 @@ export class BaseCommand {
};
}
private async handleReply(interaction: Message | CommandInteraction, result: void | string | MessageEmbed | ReplyMessageOptions) {
private async handleReply(interaction: Message | Interaction, result: void | string | MessageEmbed | ReplyMessageOptions) {
if (interaction instanceof Interaction && !interaction.isCommand() && !interaction.isContextMenu()) return null;
if (
result instanceof Object &&
("content" in (result as any) || "embeds" in (result as any) || "files" in (result as any) || "components" in (result as any) || "sticker" in (result as any))
) {
if (interaction instanceof Message) await interaction.reply(result as ReplyMessageOptions);
else if (interaction instanceof CommandInteraction) await interaction.editReply(result as ReplyMessageOptions);
else if (interaction instanceof Interaction) await interaction.editReply(result as ReplyMessageOptions);
} else if (typeof result == "string") {
if (interaction instanceof Message) await interaction?.reply({ content: result });
else if (interaction instanceof CommandInteraction)
else if (interaction instanceof Interaction)
await interaction.editReply({
content: result,
});
} else if (result instanceof MessageEmbed) {
if (interaction instanceof Message) await interaction?.reply({ embeds: [result] });
else if (interaction instanceof CommandInteraction) await interaction.editReply({ embeds: [result] });
} else if (this.announceSuccess && (interaction instanceof CommandInteraction ? !interaction.replied : true)) {
else if (interaction instanceof Interaction) await interaction.editReply({ embeds: [result] });
} else if (this.announceSuccess && (interaction instanceof Interaction ? !interaction.replied : true)) {
throw new OperationSuccess(this);
} else if (interaction instanceof CommandInteraction && !interaction.replied) {
} else if (interaction instanceof Interaction && !interaction.replied) {
await interaction.deleteReply();
}
}
+16 -15
View File
@@ -3,7 +3,7 @@ import { EventEmitter } from "events";
import * as http from "http";
import { ChatCommand } from "./ChatCommand.js";
import { ChatCommandManager } from "../managers/ChatCommandManager.js";
import { OperationSuccess, PermissionsError } from "../errors.js";
import { CommandNotFound, OperationSuccess, PermissionsError } from "../errors.js";
import { HelpMessage } from "./Help.js";
import { SystemMessageManager } from "../managers/SystemMessage.js";
import { CommandInteractionData } from "../types/commands.js";
@@ -171,9 +171,6 @@ export class Bot extends EventEmitter {
if (cmdMsg) {
this.emit("COMMAND", m, cmdMsg);
await cmdMsg.command.start(m, cmdMsg.parameters);
} else if (this.chatCommands.prefix && m.content.startsWith(this.chatCommands.prefix)) {
this.emit("MESSAGE", m);
await this.messages.system.send("NOT_FOUND", { phrase: m.content, user: m.member || undefined }, m);
} else {
this.emit("MESSAGE", m);
}
@@ -190,6 +187,8 @@ export class Bot extends EventEmitter {
this.emit("ERROR", e);
} else if (e instanceof OperationSuccess) {
await this.messages.system.send("SUCCESS", undefined, m);
} else if (e instanceof CommandNotFound) {
await this.messages.system.send("NOT_FOUND", { phrase: e.query, user: m.member || undefined }, m);
} else {
await this.messages.system.send(
"ERROR",
@@ -208,20 +207,20 @@ export class Bot extends EventEmitter {
this.client.on("interactionCreate", async (i) => {
let cmd: CommandInteractionData | null = null;
try {
if (!i.isCommand() && !i.isContextMenu()) return;
cmd = this.chatCommands.fetch(i);
if (i.isContextMenu()) {
switch (i.targetType) {
case "MESSAGE":
cmd = this.messageCommands.fetch(i);
break;
case "USER":
break;
}
} else if (i.isCommand()) {
cmd = this.chatCommands.fetch(i);
}
if (cmd) {
this.emit("COMMAND", i, cmd);
await cmd.command.start(i, cmd.parameters);
} else {
await this.messages.system.send(
"NOT_FOUND",
{
phrase: i.commandName,
user: i.member as GuildMember,
},
i as CommandInteraction
);
}
} catch (e) {
if (e instanceof PermissionsError) {
@@ -236,6 +235,8 @@ export class Bot extends EventEmitter {
this.emit("ERROR", e);
} else if (e instanceof OperationSuccess) {
await this.messages.system.send("SUCCESS", undefined, i as CommandInteraction);
} else if (e instanceof CommandNotFound) {
await this.messages.system.send("NOT_FOUND", { user: i.user, phrase: e.query });
} else {
await this.messages.system.send(
"ERROR",
+2 -2
View File
@@ -1,5 +1,5 @@
import { PermissionResolvable, Message, CommandInteraction } from "discord.js";
import { PermissionResolvable, Message, Interaction } from "discord.js";
export type PermissionCheckTypes = "ALL" | "ANY";
export type PermissionTypes = PermissionResolvable | ((m?: Message | CommandInteraction) => boolean);
export type PermissionTypes = PermissionResolvable | ((m?: Message | Interaction) => boolean);