Expand multi-line .ini value support

The following options now also support multi-line values:
- responseMessage
- closeMessage
- botMentionResponse
- greetingMessage
- accountAgeDeniedMessage
- timeOnServerDeniedMessage
master
Dragory 2020-01-19 21:25:10 +02:00
parent 015c9fae31
commit 00fe240eb0
No known key found for this signature in database
GPG Key ID: 5F387BA66DF8AAC1
6 changed files with 32 additions and 16 deletions

View File

@ -241,13 +241,6 @@ if (finalConfig.greetingMessage || finalConfig.greetingAttachment) {
}
}
// Convert arrays of lines to multiline strings in greetings
for (const obj of Object.values(finalConfig.guildGreetings)) {
if (Array.isArray(obj.message)) {
obj.message = obj.message.join('\n');
}
}
// newThreadCategoryId is syntactic sugar for categoryAutomation.newThread
if (finalConfig.newThreadCategoryId) {
finalConfig.categoryAutomation.newThread = finalConfig.newThreadCategoryId;

View File

@ -68,8 +68,9 @@ async function createNewThreadForUser(user, quiet = false, ignoreRequirements =
if (config.requiredAccountAge && ! ignoreRequirements) {
if (user.createdAt > moment() - config.requiredAccountAge * HOURS){
if (config.accountAgeDeniedMessage) {
const accountAgeDeniedMessage = utils.readMultilineConfigValue(config.accountAgeDeniedMessage);
const privateChannel = await user.getDMChannel();
await privateChannel.createMessage(config.accountAgeDeniedMessage);
await privateChannel.createMessage(accountAgeDeniedMessage);
}
return;
}
@ -106,8 +107,9 @@ async function createNewThreadForUser(user, quiet = false, ignoreRequirements =
if (! isAllowed) {
if (config.timeOnServerDeniedMessage) {
const timeOnServerDeniedMessage = utils.readMultilineConfigValue(config.timeOnServerDeniedMessage);
const privateChannel = await user.getDMChannel();
await privateChannel.createMessage(config.timeOnServerDeniedMessage);
await privateChannel.createMessage(timeOnServerDeniedMessage);
}
return;
}
@ -173,8 +175,10 @@ async function createNewThreadForUser(user, quiet = false, ignoreRequirements =
// Send auto-reply to the user
if (config.responseMessage) {
const responseMessage = utils.readMultilineConfigValue(config.responseMessage);
try {
await newThread.postToUser(config.responseMessage);
await newThread._sendDMToUser(responseMessage);
} catch (err) {
responseMessageError = err;
}

View File

@ -223,7 +223,8 @@ function initBaseMessageHandlers() {
// Send an auto-response to the mention, if enabled
if (config.botMentionResponse) {
bot.createMessage(msg.channel.id, config.botMentionResponse.replace(/{userMention}/g, `<@${msg.author.id}>`));
const botMentionResponse = utils.readMultilineConfigValue(config.botMentionResponse);
bot.createMessage(msg.channel.id, botMentionResponse.replace(/{userMention}/g, `<@${msg.author.id}>`));
}
});
}

View File

@ -12,7 +12,8 @@ module.exports = ({ bot, knex, config, commands }) => {
const threadsToBeClosed = await threads.getThreadsThatShouldBeClosed();
for (const thread of threadsToBeClosed) {
if (config.closeMessage && ! thread.scheduled_close_silent) {
await thread.postToUser(config.closeMessage).catch(() => {});
const closeMessage = utils.readMultilineConfigValue(config.closeMessage);
await thread.postToUser(closeMessage).catch(() => {});
}
await thread.close(false, thread.scheduled_close_silent);
@ -116,7 +117,8 @@ module.exports = ({ bot, knex, config, commands }) => {
// Send close message (unless suppressed with a silent close)
if (hasCloseMessage && ! silentClose) {
await thread.postToUser(config.closeMessage).catch(() => {});
const closeMessage = utils.readMultilineConfigValue(config.closeMessage);
await thread._sendDMToUser(closeMessage).catch(() => {});
}
const logUrl = await thread.getLogUrl();
@ -135,7 +137,11 @@ module.exports = ({ bot, knex, config, commands }) => {
if (! thread) return;
console.log(`[INFO] Auto-closing thread with ${thread.user_name} because the channel was deleted`);
if (config.closeMessage) await thread.postToUser(config.closeMessage).catch(() => {});
if (config.closeMessage) {
const closeMessage = utils.readMultilineConfigValue(config.closeMessage);
await thread._sendDMToUser(closeMessage).catch(() => {});
}
await thread.close(true);
const logUrl = await thread.getLogUrl();

View File

@ -1,6 +1,7 @@
const path = require('path');
const fs = require('fs');
const config = require('../config');
const utils = require('../utils');
module.exports = ({ bot }) => {
if (! config.enableGreeting) return;
@ -21,14 +22,16 @@ module.exports = ({ bot }) => {
});
}
const greetingMessage = utils.readMultilineConfigValue(guildGreeting.message);
if (guildGreeting.attachment) {
const filename = path.basename(guildGreeting.attachment);
fs.readFile(guildGreeting.attachment, (err, data) => {
const file = {file: data, name: filename};
sendGreeting(guildGreeting.message, file);
sendGreeting(greetingMessage, file);
});
} else {
sendGreeting(guildGreeting.message);
sendGreeting(greetingMessage);
}
});
};

View File

@ -309,6 +309,13 @@ function disableCodeBlocks(str) {
return str.replace(/`/g, "`\u200b");
}
/**
*
*/
function readMultilineConfigValue(str) {
return Array.isArray(str) ? str.join('\n') : str;
}
module.exports = {
BotError,
@ -345,4 +352,6 @@ module.exports = {
escapeMarkdown,
disableCodeBlocks,
readMultilineConfigValue,
};