From 012a819242b26ae2215ab58a7c312f2dbf5340e0 Mon Sep 17 00:00:00 2001 From: Dragory <2606411+Dragory@users.noreply.github.com> Date: Sun, 25 Oct 2020 04:27:12 +0200 Subject: [PATCH] Send response message after creating thread This way the response message is shown in the right order in the created thread. --- src/data/threads.js | 16 ---------------- src/main.js | 16 +++++++++++++++- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/src/data/threads.js b/src/data/threads.js index c4429d6..240fd12 100644 --- a/src/data/threads.js +++ b/src/data/threads.js @@ -198,17 +198,6 @@ async function createNewThreadForUser(user, opts = {}) { allowedMentions: utils.getInboxMentionAllowedMentions(), }); } - - // Send auto-reply to the user - if (config.responseMessage) { - const responseMessage = utils.readMultilineConfigValue(config.responseMessage); - - try { - await newThread.sendSystemMessageToUser(responseMessage); - } catch (err) { - responseMessageError = err; - } - } } // Post some info to the beginning of the new thread @@ -276,11 +265,6 @@ async function createNewThreadForUser(user, opts = {}) { } } - // 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 384b02a..788d0ef 100644 --- a/src/main.js +++ b/src/main.js @@ -147,9 +147,10 @@ function initBaseMessageHandlers() { // Private message handling is queued so e.g. multiple message in quick succession don't result in multiple channels being created messageQueue.add(async () => { let thread = await threads.findOpenThreadByUserId(msg.author.id); + const createNewThread = (thread == null); // New thread - if (! thread) { + if (createNewThread) { // Ignore messages that shouldn't usually open new threads, such as "ok", "thanks", etc. if (config.ignoreAccidentalThreads && msg.content && ACCIDENTAL_THREAD_MESSAGES.includes(msg.content.trim().toLowerCase())) return; @@ -161,6 +162,19 @@ function initBaseMessageHandlers() { if (thread) { await thread.receiveUserReply(msg); + + if (createNewThread) { + // Send auto-reply to the user + if (config.responseMessage) { + const responseMessage = utils.readMultilineConfigValue(config.responseMessage); + + try { + await thread.sendSystemMessageToUser(responseMessage); + } catch (err) { + await thread.postSystemMessage(`**NOTE:** Could not send auto-response to the user. The error given was: \`${err.message}\``); + } + } + } } }); });