Allow overriding mentionRole in threads.createNewThreadForUser() opts

cshd
Dragory 2020-11-01 22:59:54 +02:00
parent dd4640bfff
commit a5279feb18
No known key found for this signature in database
GPG Key ID: 5F387BA66DF8AAC1
2 changed files with 50 additions and 8 deletions

View File

@ -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 {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} [categoryId] Category where to open the thread
* @property {string} [source] A string identifying the source of the new 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) { if (! quiet) {
// Ping moderators of the new thread // 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() !== "") { if (staffMention.trim() !== "") {
const allowedMentions = opts.mentionRole
? utils.mentionRolesToAllowedMentions(utils.getValidMentionRoles(opts.mentionRole))
: utils.getInboxMentionAllowedMentions();
await newThread.postNonLogMessage({ await newThread.postNonLogMessage({
content: `${staffMention}New modmail thread (${newThread.user_name})`, content: `${staffMention}New modmail thread (${newThread.user_name})`,
allowedMentions: utils.getInboxMentionAllowedMentions(), allowedMentions,
}); });
} }
} }

View File

@ -251,15 +251,25 @@ function convertDelayStringToMS(str) {
return ms; 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 mentionRoles.filter(roleStr => {
return (roleStr !== null && roleStr !== "none" && roleStr !== "off" && roleStr !== ""); return (roleStr !== null && roleStr !== "none" && roleStr !== "off" && roleStr !== "");
}); });
} }
function getInboxMention() { /**
const mentionRoles = getValidMentionRoles(); * @param {string[]} mentionRoles
* @returns {string}
*/
function mentionRolesToMention(mentionRoles) {
const mentions = []; const mentions = [];
for (const role of mentionRoles) { for (const role of mentionRoles) {
if (role === "here") mentions.push("@here"); if (role === "here") mentions.push("@here");
@ -269,8 +279,19 @@ function getInboxMention() {
return mentions.join(" ") + " "; 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 = { const allowedMentions = {
everyone: false, everyone: false,
roles: [], roles: [],
@ -284,6 +305,14 @@ function getInboxMentionAllowedMentions() {
return allowedMentions; return allowedMentions;
} }
/**
* @returns {object}
*/
function getInboxMentionAllowedMentions() {
const mentionRoles = getValidMentionRoles(config.mentionRole);
return mentionRolesToAllowedMentions(mentionRoles);
}
function postSystemMessageWithFallback(channel, thread, text) { function postSystemMessageWithFallback(channel, thread, text) {
if (thread) { if (thread) {
thread.postSystemMessage(text); thread.postSystemMessage(text);
@ -484,8 +513,13 @@ module.exports = {
getMainRole, getMainRole,
delayStringRegex, delayStringRegex,
convertDelayStringToMS, convertDelayStringToMS,
getValidMentionRoles,
mentionRolesToMention,
getInboxMention, getInboxMention,
mentionRolesToAllowedMentions,
getInboxMentionAllowedMentions, getInboxMentionAllowedMentions,
postSystemMessageWithFallback, postSystemMessageWithFallback,
chunk, chunk,