From cc5281cd1eed8edaa60fa55bf13fe791ce3ca7cb Mon Sep 17 00:00:00 2001 From: Jordan Eldredge Date: Fri, 24 Apr 2020 22:52:55 -0400 Subject: [PATCH] Better handling of Discord command errors --- .../skin-database/discord-bot/index.js | 26 ++++++++++++------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/experiments/skin-database/discord-bot/index.js b/experiments/skin-database/discord-bot/index.js index d4ab842b..72e649f6 100644 --- a/experiments/skin-database/discord-bot/index.js +++ b/experiments/skin-database/discord-bot/index.js @@ -7,13 +7,13 @@ const DiscordWinstonTransport = require("../DiscordWinstonTransport"); const client = new Discord.Client(); const handlers = { - help: handleHelp, + help: handleHelp }; const commands = fs .readdirSync(path.resolve(__dirname, "./commands")) - .filter((file) => file.endsWith(".js")) - .map((file) => { + .filter(file => file.endsWith(".js")) + .map(file => { return require(`./commands/${file}`); }); @@ -23,7 +23,7 @@ for (const command of commands) { async function handleHelp(message) { const commandHelp = commands - .map((command) => { + .map(command => { return `\`!${command.command} ${command.usage || ""}\` -- ${ command.description }`; @@ -41,7 +41,7 @@ You can issue these commands in a DM to me or in any channel. For tasks relating message.channel.send(help); } -client.on("message", async (message) => { +client.on("message", async message => { if (message.author.bot) { return; } @@ -54,7 +54,7 @@ client.on("message", async (message) => { command, user: message.author.username, args, - channel: message.channel.name || "DM", + channel: message.channel.name || "DM" }); const handler = handlers[command]; if (handler == null) { @@ -62,14 +62,22 @@ client.on("message", async (message) => { command, user: message.author.username, args, - alert: true, + alert: true }); return; } - await handler(message, args); + try { + await handler(message, args); + } catch (e) { + console.error(e); + logger.error("Error handling command", e); + message.channel.send( + "Oops. Something went wrong. Ping @captbaritone and yell at him to fix it." + ); + } }); -client.on("error", (e) => { +client.on("error", e => { logger.error("The WebSocket encountered an error:", e); });