From 1d2f3dd4d88f1245486a359b038285fd31ff76f4 Mon Sep 17 00:00:00 2001 From: Dragory Date: Thu, 3 May 2018 20:26:12 +0300 Subject: [PATCH] Fix crash when the auto-response cannot be sent. Add pingOnBotMention option for controlling whether the staff get pinged for bot mentions. --- src/config.js | 1 + src/data/threads.js | 12 +++++++++++- src/main.js | 5 +++-- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/config.js b/src/config.js index 7208612..8ea73e8 100644 --- a/src/config.js +++ b/src/config.js @@ -48,6 +48,7 @@ const defaultConfig = { "newThreadCategoryId": null, "mentionRole": "here", + "pingOnBotMention": true, "inboxServerPermission": null, "alwaysReply": false, diff --git a/src/data/threads.js b/src/data/threads.js index 2701529..8e195d3 100644 --- a/src/data/threads.js +++ b/src/data/threads.js @@ -85,6 +85,7 @@ async function createNewThreadForUser(user, quiet = false) { }); const newThread = await findById(newThreadId); + let responseMessageError = null; if (! quiet) { // Ping moderators of the new thread @@ -97,7 +98,11 @@ async function createNewThreadForUser(user, quiet = false) { // Send auto-reply to the user if (config.responseMessage) { - newThread.postToUser(config.responseMessage); + try { + await newThread.postToUser(config.responseMessage); + } catch (err) { + responseMessageError = err; + } } } @@ -145,6 +150,11 @@ async function createNewThreadForUser(user, quiet = false) { await newThread.postSystemMessage(infoHeader); + // If there were errors sending a response to the user, note that + if (responseMessageError) { + await newThread.postSystemMessage(`**NOTE:** Could not send auto-response to the user. The error given was: \`${responseMessageError.message}\``); + } + // Return the thread return newThread; } diff --git a/src/main.js b/src/main.js index 58f3c30..6b51fcc 100644 --- a/src/main.js +++ b/src/main.js @@ -155,11 +155,12 @@ bot.on('messageCreate', async msg => { let content; const mainGuilds = utils.getMainGuilds(); + const staffMention = (config.pingOnBotMention ? utils.getInboxMention() : ''); if (mainGuilds.length === 1) { - content = `${utils.getInboxMention()}Bot mentioned in ${msg.channel.mention} by **${msg.author.username}#${msg.author.discriminator}**: "${msg.cleanContent}"`; + content = `${staffMention}Bot mentioned in ${msg.channel.mention} by **${msg.author.username}#${msg.author.discriminator}**: "${msg.cleanContent}"`; } else { - content = `${utils.getInboxMention()}Bot mentioned in ${msg.channel.mention} (${msg.channel.guild.name}) by **${msg.author.username}#${msg.author.discriminator}**: "${msg.cleanContent}"`; + content = `${staffMention}Bot mentioned in ${msg.channel.mention} (${msg.channel.guild.name}) by **${msg.author.username}#${msg.author.discriminator}**: "${msg.cleanContent}"`; } bot.createMessage(utils.getLogChannel(bot).id, {