Add categoryAutomation config option and make newThreadCategoryId a shortcut for the newThread sub-option. Add support for per-guild categories via categoryAutomation.

master
Dragory 2019-03-28 04:54:12 +02:00
parent ddc3f6425a
commit 073f3a87ea
2 changed files with 57 additions and 25 deletions

View File

@ -83,6 +83,8 @@ const defaultConfig = {
"attachmentStorage": "local",
"attachmentStorageChannelId": null,
"categoryAutomation": {},
"port": 8890,
"url": null,
@ -155,4 +157,10 @@ if (! Array.isArray(finalConfig['inboxServerPermission'])) {
}
}
// newThreadCategoryId is syntactic sugar for categoryAutomation.newThread
if (finalConfig.newThreadCategoryId) {
finalConfig.categoryAutomation.newThread = finalConfig.newThreadCategoryId;
delete finalConfig.newThreadCategoryId;
}
module.exports = finalConfig;

View File

@ -79,10 +79,48 @@ async function createNewThreadForUser(user, quiet = false) {
console.log(`[NOTE] Creating new thread channel ${channelName}`);
// Find which main guilds this user is part of
const mainGuilds = utils.getMainGuilds();
const userGuildData = new Map();
for (const guild of mainGuilds) {
let member = guild.members.get(user.id);
if (! member) {
try {
member = await bot.getRESTGuildMember(guild.id, user.id);
} catch (e) {
continue;
}
}
if (member) {
userGuildData.set(guild.id, { guild, member });
}
}
// Figure out which category we should place the thread channel in
let newThreadCategoryId;
if (config.categoryAutomation.newThreadFromGuild) {
// Categories for specific source guilds (in case of multiple main guilds)
for (const [guildId, categoryId] of Object.entries(config.categoryAutomation.newThreadFromGuild)) {
if (userGuildData.has(guildId)) {
newThreadCategoryId = categoryId;
break;
}
}
}
if (! newThreadCategoryId && config.categoryAutomation.newThread) {
// Blanket category id for all new threads (also functions as a fallback for the above)
newThreadCategoryId = config.categoryAutomation.newThread;
}
// Attempt to create the inbox channel for this thread
let createdChannel;
try {
createdChannel = await utils.getInboxGuild().createChannel(channelName, null, 'New ModMail thread', config.newThreadCategoryId);
createdChannel = await utils.getInboxGuild().createChannel(channelName, null, 'New ModMail thread', newThreadCategoryId);
} catch (err) {
console.error(`Error creating modmail channel for ${user.username}#${user.discriminator}!`);
throw err;
@ -136,21 +174,8 @@ async function createNewThreadForUser(user, quiet = false) {
let infoHeader = infoHeaderItems.join(', ');
// Guild member info
const mainGuilds = utils.getMainGuilds();
for (const guild of mainGuilds) {
let member = guild.members.get(user.id);
if (! member) {
try {
member = await bot.getRESTGuildMember(guild.id, user.id);
} catch (e) {
continue;
}
}
if (member) {
const {nickname, joinDate} = getHeaderGuildInfo(member);
for (const [guildId, guildData] of userGuildData.entries()) {
const {nickname, joinDate} = getHeaderGuildInfo(guildData.member);
const headerStr = [
`NICKNAME **${nickname}**`,
`JOINED **${joinDate}** ago`
@ -159,8 +184,7 @@ async function createNewThreadForUser(user, quiet = false) {
if (mainGuilds.length === 1) {
infoHeader += `\n${headerStr}`;
} else {
infoHeader += `\n**[${guild.name}]** ${headerStr}`;
}
infoHeader += `\n**[${guildData.guild.name}]** ${headerStr}`;
}
}