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