From a5279feb1864616bf51570205e7345e99881892e Mon Sep 17 00:00:00 2001 From: Dragory <2606411+Dragory@users.noreply.github.com> Date: Sun, 1 Nov 2020 22:59:54 +0200 Subject: [PATCH] Allow overriding mentionRole in threads.createNewThreadForUser() opts --- src/data/threads.js | 12 ++++++++++-- src/utils.js | 46 +++++++++++++++++++++++++++++++++++++++------ 2 files changed, 50 insertions(+), 8 deletions(-) diff --git a/src/data/threads.js b/src/data/threads.js index fbefdb2..946d65d 100644 --- a/src/data/threads.js +++ b/src/data/threads.js @@ -83,6 +83,7 @@ function getHeaderGuildInfo(member) { * @property {Message} [message] Original DM message that is trying to start the thread, if there is one * @property {string} [categoryId] Category where to open the thread * @property {string} [source] A string identifying the source of the new thread + * @property {string} [mentionRole] Override the mentionRole option for this thread */ /** @@ -217,11 +218,18 @@ async function createNewThreadForUser(user, opts = {}) { if (! quiet) { // Ping moderators of the new thread - const staffMention = utils.getInboxMention(); + const staffMention = opts.mentionRole + ? utils.mentionRolesToMention(utils.getValidMentionRoles(opts.mentionRole)) + : utils.getInboxMention(); + if (staffMention.trim() !== "") { + const allowedMentions = opts.mentionRole + ? utils.mentionRolesToAllowedMentions(utils.getValidMentionRoles(opts.mentionRole)) + : utils.getInboxMentionAllowedMentions(); + await newThread.postNonLogMessage({ content: `${staffMention}New modmail thread (${newThread.user_name})`, - allowedMentions: utils.getInboxMentionAllowedMentions(), + allowedMentions, }); } } diff --git a/src/utils.js b/src/utils.js index cba844b..8fe1770 100644 --- a/src/utils.js +++ b/src/utils.js @@ -251,15 +251,25 @@ function convertDelayStringToMS(str) { return ms; } -function getValidMentionRoles() { - const mentionRoles = Array.isArray(config.mentionRole) ? config.mentionRole : [config.mentionRole]; +/** + * @param {string|string[]} mentionRoles + * @returns {string[]} + */ +function getValidMentionRoles(mentionRoles) { + if (! Array.isArray(mentionRoles)) { + mentionRoles = [mentionRoles]; + } + return mentionRoles.filter(roleStr => { return (roleStr !== null && roleStr !== "none" && roleStr !== "off" && roleStr !== ""); }); } -function getInboxMention() { - const mentionRoles = getValidMentionRoles(); +/** + * @param {string[]} mentionRoles + * @returns {string} + */ +function mentionRolesToMention(mentionRoles) { const mentions = []; for (const role of mentionRoles) { if (role === "here") mentions.push("@here"); @@ -269,8 +279,19 @@ function getInboxMention() { return mentions.join(" ") + " "; } -function getInboxMentionAllowedMentions() { - const mentionRoles = getValidMentionRoles(); +/** + * @returns {string} + */ +function getInboxMention() { + const mentionRoles = getValidMentionRoles(config.mentionRole); + return mentionRolesToMention(mentionRoles); +} + +/** + * @param {string[]} mentionRoles + * @returns {object} + */ +function mentionRolesToAllowedMentions(mentionRoles) { const allowedMentions = { everyone: false, roles: [], @@ -284,6 +305,14 @@ function getInboxMentionAllowedMentions() { return allowedMentions; } +/** + * @returns {object} + */ +function getInboxMentionAllowedMentions() { + const mentionRoles = getValidMentionRoles(config.mentionRole); + return mentionRolesToAllowedMentions(mentionRoles); +} + function postSystemMessageWithFallback(channel, thread, text) { if (thread) { thread.postSystemMessage(text); @@ -484,8 +513,13 @@ module.exports = { getMainRole, delayStringRegex, convertDelayStringToMS, + + getValidMentionRoles, + mentionRolesToMention, getInboxMention, + mentionRolesToAllowedMentions, getInboxMentionAllowedMentions, + postSystemMessageWithFallback, chunk,