From d03903ce807478454c06ebf953151d633ee925ab Mon Sep 17 00:00:00 2001 From: Dragory <2606411+Dragory@users.noreply.github.com> Date: Wed, 12 Aug 2020 23:19:11 +0300 Subject: [PATCH] Move beforeNewThread hook after validations, fix a couple bugs --- src/data/Thread.js | 5 +++-- src/data/constants.js | 11 +++++++++++ src/data/threads.js | 14 +++++++++----- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/src/data/Thread.js b/src/data/Thread.js index 7046366..465d25f 100644 --- a/src/data/Thread.js +++ b/src/data/Thread.js @@ -208,15 +208,16 @@ class Thread { } // Save the log entry - const logContent = formatters.formatStaffReplyLogMessage(moderator, text, threadMessage.message_number, { isAnonymous, attachmentLinks }); const threadMessage = await this._addThreadMessageToDB({ message_type: THREAD_MESSAGE_TYPE.TO_USER, user_id: moderator.id, user_name: fullModeratorName, - body: logContent, + body: '', is_anonymous: (isAnonymous ? 1 : 0), dm_message_id: dmMessage.id }); + const logContent = formatters.formatStaffReplyLogMessage(moderator, text, threadMessage.message_number, { isAnonymous, attachmentLinks }); + await this._updateThreadMessage(threadMessage.id, { body: logContent }); // Show the reply in the inbox thread const inboxContent = formatters.formatStaffReplyThreadMessage(moderator, text, threadMessage.message_number, { isAnonymous }); diff --git a/src/data/constants.js b/src/data/constants.js index 01bae0c..40388db 100644 --- a/src/data/constants.js +++ b/src/data/constants.js @@ -15,6 +15,17 @@ module.exports = { SYSTEM_TO_USER: 7 }, + // https://discord.com/developers/docs/resources/channel#channel-object-channel-types + DISOCRD_CHANNEL_TYPES: { + GUILD_TEXT: 0, + DM: 1, + GUILD_VOICE: 2, + GROUP_DM: 3, + GUILD_CATEGORY: 4, + GUILD_NEWS: 5, + GUILD_STORE: 6, + }, + ACCIDENTAL_THREAD_MESSAGES: [ 'ok', 'okay', diff --git a/src/data/threads.js b/src/data/threads.js index 6288cb1..4120489 100644 --- a/src/data/threads.js +++ b/src/data/threads.js @@ -13,7 +13,7 @@ const updates = require('./updates'); const Thread = require('./Thread'); const {callBeforeNewThreadHooks} = require("../hooks/beforeNewThread"); -const {THREAD_STATUS} = require('./constants'); +const {THREAD_STATUS, DISOCRD_CHANNEL_TYPES} = require('./constants'); const MINUTES = 60 * 1000; const HOURS = 60 * MINUTES; @@ -73,9 +73,6 @@ async function createNewThreadForUser(user, opts = {}) { throw new Error('Attempted to create a new thread for a user with an existing open thread!'); } - const hookResult = await callBeforeNewThreadHooks({ user, opts }); - if (hookResult.cancelled) return; - // If set in config, check that the user's account is old enough (time since they registered on Discord) // If the account is too new, don't start a new thread and optionally reply to them with a message if (config.requiredAccountAge && ! ignoreRequirements) { @@ -128,6 +125,10 @@ async function createNewThreadForUser(user, opts = {}) { } } + // Call any registered beforeNewThreadHooks + const hookResult = await callBeforeNewThreadHooks({ user, opts }); + if (hookResult.cancelled) return; + // Use the user's name+discrim for the thread channel's name // Channel names are particularly picky about what characters they allow, so we gotta do some clean-up let cleanName = transliterate.slugify(user.username); @@ -159,7 +160,10 @@ async function createNewThreadForUser(user, opts = {}) { // Attempt to create the inbox channel for this thread let createdChannel; try { - createdChannel = await utils.getInboxGuild().createChannel(channelName, null, 'New Modmail thread', newThreadCategoryId); + createdChannel = await utils.getInboxGuild().createChannel(channelName, DISOCRD_CHANNEL_TYPES.GUILD_TEXT, { + reason: 'New Modmail thread', + parentID: newThreadCategoryId, + }); } catch (err) { console.error(`Error creating modmail channel for ${user.username}#${user.discriminator}!`); throw err;