more error fixes and cleanups

This commit is contained in:
mmacierzanka committed 2021-09-08 22:39:20 +02:00
1 parent 4724f18fdc
commit b8f08e87e3
3 files changed
+44 -53

No files matched your search

+8 -9
View File
@@ -1,10 +1,10 @@
import { MessageEmbed } from "discord.js";
import { ChatCommand } from "./ChatCommand.js";
import { ChatCommandManager } from "../managers/ChatCommandManager.js";
import { CommandManager } from "../commands/CommandManager.js";
import { HelpMessageParams } from "./types/HelpMessage.js";
export class HelpMessage extends ChatCommand {
constructor(cmdManager: ChatCommandManager, params: HelpMessageParams, botName?: string) {
constructor(cmdManager: CommandManager, params: HelpMessageParams, botName?: string) {
super({
name: "help",
usage: params.usage,
@@ -19,8 +19,8 @@ export class HelpMessage extends ChatCommand {
type: "string",
},
],
function: (i, p) => {
if (!p) {
function: (args, i) => {
if (!(args instanceof Map)) {
return;
}
const helpMsg = new MessageEmbed();
@@ -28,8 +28,8 @@ export class HelpMessage extends ChatCommand {
helpMsg.setTimestamp();
helpMsg.setFooter(botName || "");
if (helpMsg != null) {
if (p("command_name")) {
const cmd: ChatCommand | null = cmdManager.get(p("command_name")?.toString() || "");
if (args.get("command_name")) {
const cmd: ChatCommand | null = cmdManager.get(args.get("command_name") || "", "CHAT");
if (cmd) {
if (Array.isArray(cmd.guilds) && cmd.guilds.length > 0 && !cmd.guilds.find((g) => i?.guild?.id === g)) {
throw new ReferenceError(`Command "${cmd.name}" is not available`);
@@ -58,15 +58,14 @@ export class HelpMessage extends ChatCommand {
helpMsg.addField("Slash command:", cmd.slash ? "ENABLED" : "DISABLED", false);
helpMsg.addField("Command scope:", Array.isArray(cmd.guilds) && cmd.guilds.length > 0 ? "CUSTOM" : "GLOBAL", false);
} else {
throw new ReferenceError(`Command "${p("command_name")}" does not exist`);
throw new ReferenceError(`Command "${args.get("command_name")}" does not exist`);
}
} else {
helpMsg.setTitle(params.title);
helpMsg.setDescription(params.bottomText);
cmdManager.list.map((c) => {
cmdManager.list("CHAT").map((c) => {
if (
c instanceof ChatCommand &&
c.visible &&
((Array.isArray(c.guilds) && c.guilds.length > 0 && c.guilds.find((g) => i?.guild?.id === g)) || !Array.isArray(c.guilds) || c.guilds.length === 0)
) {
-1
View File
@@ -2,7 +2,6 @@ import { BaseCommand } from "../BaseCommand.js";
import { ChatCommand } from "../ChatCommand.js";
import { MessageCommand } from "../MessageCommand.js";
import { UserCommand } from "../UserCommand.js";
import { Interaction, Message } from "discord.js";
import { ParameterResolvable } from "structures/types/Parameter.js";
import { TargetID } from "structures/parameter.js";
+36 -43
View File
@@ -127,6 +127,9 @@ export class Bot extends EventEmitter {
if (applicationState.running) {
throw new Error("This bot is already running");
}
if (applicationState.dev) {
console.warn(`[⚠️ WARNING] You're using an unstable version of the CommandBot package. It is not recommended to use this version in production.`);
}
console.log(`\nBot name: ${this.name}`);
console.log(`Prefix: ${this.commands.prefix || "/ (only slash commands)"} \n`);
if (this.token === "") {
@@ -162,7 +165,7 @@ export class Bot extends EventEmitter {
cmdMsg = this.commands.fetch(m);
if (cmdMsg) {
this.emit("COMMAND", m, cmdMsg);
await cmdMsg.command.start(m, cmdMsg.parameters);
await cmdMsg.command.start(cmdMsg.parameters, m);
} else {
this.emit("MESSAGE", m);
}
@@ -199,49 +202,39 @@ export class Bot extends EventEmitter {
this.client.on("interactionCreate", async (i) => {
let cmd: CommandInteractionData | null = null;
try {
// if (i.isContextMenu()) {
// switch (i.targetType) {
// case "MESSAGE":
// cmd = this.messageCommands.fetch(i);
// break;
// case "USER":
// break;
// }
// } else if (i.isCommand()) {
// cmd = this.commands.fetch(i);
// }
// if (cmd) {
// this.emit("COMMAND", i, cmd);
// await cmd.command.start(i, cmd.parameters);
// }
cmd = this.commands.fetch(i);
if (cmd) {
this.emit("COMMAND", i, cmd);
await cmd.command.start(cmd.parameters, i);
}
} catch (e) {
// if (e instanceof PermissionsError) {
// await this.messages.system.send(
// "PERMISSION",
// {
// user: (i.member as GuildMember) || undefined,
// command: cmd?.command,
// },
// i as CommandInteraction
// );
// 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 }, i);
// } else {
// await this.messages.system.send(
// "ERROR",
// {
// command: cmd?.command,
// user: (i.member as GuildMember) || undefined,
// error: e as Error,
// },
// i as CommandInteraction
// );
// this.emit("ERROR", e);
// }
// return;
if (e instanceof PermissionsError) {
await this.messages.system.send(
"PERMISSION",
{
user: (i.member as GuildMember) || undefined,
command: cmd?.command,
},
i as CommandInteraction
);
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 }, i);
} else {
await this.messages.system.send(
"ERROR",
{
command: cmd?.command,
user: (i.member as GuildMember) || undefined,
error: e as Error,
},
i as CommandInteraction
);
this.emit("ERROR", e);
}
return;
}
});
return true;