From e258bf84321f3325ba76688875e51519c7356226 Mon Sep 17 00:00:00 2001 From: Miikka Virtanen Date: Thu, 18 May 2017 05:47:45 +0300 Subject: [PATCH] Add config option ignoreAccidentalThreads. Using this option, messages that start with 'ok', 'thanks', etc. will be ignored, as they probably shouldn't start a new thread. --- src/index.js | 15 +++++++++++---- src/threads.js | 23 +++++++++++++++++++++-- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/src/index.js b/src/index.js index b4a9d93..7b66d7e 100644 --- a/src/index.js +++ b/src/index.js @@ -87,13 +87,17 @@ bot.on('messageCreate', (msg) => { const attachmentSavePromise = attachments.saveAttachmentsInMessage(msg); let thread, userLogs; + let threadCreationFailed = false; // Private message handling is queued so e.g. multiple message in quick succession don't result in multiple channels being created messageQueue.add(() => { - return threads.getForUser(bot, msg.author) + return threads.getForUser(bot, msg.author, true, msg) .then(userThread => { thread = userThread; return logs.getLogsByUserId(msg.author.id); + }, err => { + console.log(`[ERROR] Modmail channel for ${msg.author.username}#${msg.author.discriminator} could not be created:\n${err.message}`); + threadCreationFailed = true; }) .then(foundUserLogs => { userLogs = foundUserLogs; @@ -101,8 +105,8 @@ bot.on('messageCreate', (msg) => { .then(() => { let content = msg.content; - // If the thread does not exist and could not be created, send a warning about this to all mods so they can DM the user directly instead - if (! thread) { + if (threadCreationFailed) { + // If the thread could not be created, send a warning about this to all mods so they can DM the user directly instead let warningMessage = ` @here Error creating modmail thread for ${msg.author.username}#${msg.author.discriminator} (${msg.author.id})! @@ -111,10 +115,13 @@ Here's what their message contained: `.trim(); bot.createMessage(utils.getModmailGuild(bot).id, { - content: `@here Error creating modmail thread for ${msg.author.username}#${msg.author.discriminator} (${msg.author.id})!`, + content: warningMessage, disableEveryone: false, }); + return; + } else if (! thread) { + // No thread but creation didn't fail either -> probably ignored return; } diff --git a/src/threads.js b/src/threads.js index fd57bad..61a4ac5 100644 --- a/src/threads.js +++ b/src/threads.js @@ -1,6 +1,16 @@ const Eris = require('eris'); const utils = require('./utils'); const jsonDb = require('./jsonDb'); +const config = require('../config'); + +const accidentalThreadMessages = [ + 'ok', + 'okay', + 'thanks', + 'ty', + 'k', + 'thank you' +]; /** * @typedef {Object} ModMailThread @@ -18,7 +28,7 @@ const jsonDb = require('./jsonDb'); * @param {Boolean} allowCreate * @returns {Promise} */ -function getForUser(bot, user, allowCreate = true) { +function getForUser(bot, user, allowCreate = true, originalMessage = null) { return jsonDb.get('threads', []).then(threads => { const thread = threads.find(t => t.userId === user.id); if (thread) return thread; @@ -31,8 +41,16 @@ function getForUser(bot, user, allowCreate = true) { if (cleanName === '') cleanName = 'unknown'; const channelName = `${cleanName}-${user.discriminator}`; - console.log(`[NOTE] Creating new thread channel ${channelName}`); + if (originalMessage && originalMessage.cleanContent && config.ignoreAccidentalThreads) { + const cleaned = originalMessage.cleanContent.replace(/[^a-z\s]/gi, '').toLowerCase().trim(); + if (accidentalThreadMessages.includes(cleaned)) { + console.log('[NOTE] Skipping thread creation for message:', originalMessage.cleanContent); + return null; + } + } + + console.log(`[NOTE] Creating new thread channel ${channelName}`); return utils.getModmailGuild(bot).createChannel(`${channelName}`) .then(channel => { const thread = { @@ -49,6 +67,7 @@ function getForUser(bot, user, allowCreate = true) { }); }, err => { console.error(`Error creating modmail channel for ${user.username}#${user.discriminator}!`); + throw err; }); }); }