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.

master
Miikka Virtanen 2017-05-18 05:47:45 +03:00
parent ad3417c827
commit e258bf8432
2 changed files with 32 additions and 6 deletions

View File

@ -87,13 +87,17 @@ bot.on('messageCreate', (msg) => {
const attachmentSavePromise = attachments.saveAttachmentsInMessage(msg); const attachmentSavePromise = attachments.saveAttachmentsInMessage(msg);
let thread, userLogs; 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 // Private message handling is queued so e.g. multiple message in quick succession don't result in multiple channels being created
messageQueue.add(() => { messageQueue.add(() => {
return threads.getForUser(bot, msg.author) return threads.getForUser(bot, msg.author, true, msg)
.then(userThread => { .then(userThread => {
thread = userThread; thread = userThread;
return logs.getLogsByUserId(msg.author.id); 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 => { .then(foundUserLogs => {
userLogs = foundUserLogs; userLogs = foundUserLogs;
@ -101,8 +105,8 @@ bot.on('messageCreate', (msg) => {
.then(() => { .then(() => {
let content = msg.content; 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 (threadCreationFailed) {
if (! thread) { // 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 = ` let warningMessage = `
@here Error creating modmail thread for ${msg.author.username}#${msg.author.discriminator} (${msg.author.id})! @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(); `.trim();
bot.createMessage(utils.getModmailGuild(bot).id, { 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, disableEveryone: false,
}); });
return;
} else if (! thread) {
// No thread but creation didn't fail either -> probably ignored
return; return;
} }

View File

@ -1,6 +1,16 @@
const Eris = require('eris'); const Eris = require('eris');
const utils = require('./utils'); const utils = require('./utils');
const jsonDb = require('./jsonDb'); const jsonDb = require('./jsonDb');
const config = require('../config');
const accidentalThreadMessages = [
'ok',
'okay',
'thanks',
'ty',
'k',
'thank you'
];
/** /**
* @typedef {Object} ModMailThread * @typedef {Object} ModMailThread
@ -18,7 +28,7 @@ const jsonDb = require('./jsonDb');
* @param {Boolean} allowCreate * @param {Boolean} allowCreate
* @returns {Promise<ModMailThread>} * @returns {Promise<ModMailThread>}
*/ */
function getForUser(bot, user, allowCreate = true) { function getForUser(bot, user, allowCreate = true, originalMessage = null) {
return jsonDb.get('threads', []).then(threads => { return jsonDb.get('threads', []).then(threads => {
const thread = threads.find(t => t.userId === user.id); const thread = threads.find(t => t.userId === user.id);
if (thread) return thread; if (thread) return thread;
@ -31,8 +41,16 @@ function getForUser(bot, user, allowCreate = true) {
if (cleanName === '') cleanName = 'unknown'; if (cleanName === '') cleanName = 'unknown';
const channelName = `${cleanName}-${user.discriminator}`; 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}`) return utils.getModmailGuild(bot).createChannel(`${channelName}`)
.then(channel => { .then(channel => {
const thread = { const thread = {
@ -49,6 +67,7 @@ function getForUser(bot, user, allowCreate = true) {
}); });
}, err => { }, err => {
console.error(`Error creating modmail channel for ${user.username}#${user.discriminator}!`); console.error(`Error creating modmail channel for ${user.username}#${user.discriminator}!`);
throw err;
}); });
}); });
} }