Better handling of Discord command errors

This commit is contained in:
Jordan Eldredge 2020-04-24 22:52:55 -04:00
parent 82766c1d08
commit cc5281cd1e

View file

@ -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);
});