2020-08-12 17:08:37 -04:00
|
|
|
const Eris = require("eris");
|
|
|
|
const bot = require("./bot");
|
|
|
|
const moment = require("moment");
|
|
|
|
const humanizeDuration = require("humanize-duration");
|
|
|
|
const publicIp = require("public-ip");
|
|
|
|
const config = require("./cfg");
|
2017-09-19 13:23:55 -04:00
|
|
|
|
|
|
|
class BotError extends Error {}
|
|
|
|
|
2018-04-21 08:38:21 -04:00
|
|
|
const userMentionRegex = /^<@!?([0-9]+?)>$/;
|
2017-02-09 21:56:36 -05:00
|
|
|
|
2017-09-19 10:38:37 -04:00
|
|
|
let inboxGuild = null;
|
2018-04-21 08:38:21 -04:00
|
|
|
let mainGuilds = [];
|
2017-09-19 10:38:37 -04:00
|
|
|
let logChannel = null;
|
2017-02-14 17:57:41 -05:00
|
|
|
|
2018-02-11 14:54:30 -05:00
|
|
|
/**
|
|
|
|
* @returns {Eris~Guild}
|
|
|
|
*/
|
2017-09-19 13:23:55 -04:00
|
|
|
function getInboxGuild() {
|
2020-08-16 16:53:35 -04:00
|
|
|
if (! inboxGuild) inboxGuild = bot.guilds.find(g => g.id === config.inboxServerId);
|
2020-08-12 17:08:37 -04:00
|
|
|
if (! inboxGuild) throw new BotError("The bot is not on the modmail (inbox) server!");
|
2017-09-19 10:38:37 -04:00
|
|
|
return inboxGuild;
|
2017-02-09 21:56:36 -05:00
|
|
|
}
|
|
|
|
|
2018-02-11 14:54:30 -05:00
|
|
|
/**
|
2018-04-21 08:38:21 -04:00
|
|
|
* @returns {Eris~Guild[]}
|
2018-02-11 14:54:30 -05:00
|
|
|
*/
|
2018-04-21 08:38:21 -04:00
|
|
|
function getMainGuilds() {
|
|
|
|
if (mainGuilds.length === 0) {
|
2020-08-16 16:53:35 -04:00
|
|
|
mainGuilds = bot.guilds.filter(g => config.mainServerId.includes(g.id));
|
2018-04-21 08:38:21 -04:00
|
|
|
}
|
|
|
|
|
2020-08-16 16:53:35 -04:00
|
|
|
if (mainGuilds.length !== config.mainServerId.length) {
|
|
|
|
if (config.mainServerId.length === 1) {
|
2020-08-12 17:08:37 -04:00
|
|
|
console.warn("[WARN] The bot hasn't joined the main guild!");
|
2018-04-21 08:38:21 -04:00
|
|
|
} else {
|
2020-08-12 17:08:37 -04:00
|
|
|
console.warn("[WARN] The bot hasn't joined one or more main guilds!");
|
2018-04-21 08:38:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return mainGuilds;
|
2017-02-14 17:57:41 -05:00
|
|
|
}
|
|
|
|
|
2017-09-19 13:23:55 -04:00
|
|
|
/**
|
|
|
|
* Returns the designated log channel, or the default channel if none is set
|
2018-02-11 14:54:30 -05:00
|
|
|
* @returns {Eris~TextChannel}
|
2017-09-19 13:23:55 -04:00
|
|
|
*/
|
|
|
|
function getLogChannel() {
|
2020-08-20 22:13:24 -04:00
|
|
|
const _inboxGuild = getInboxGuild();
|
|
|
|
const _logChannel = _inboxGuild.channels.get(config.logChannelId);
|
2017-09-19 10:38:37 -04:00
|
|
|
|
2020-08-20 22:13:24 -04:00
|
|
|
if (! _logChannel) {
|
2020-08-12 17:08:37 -04:00
|
|
|
throw new BotError("Log channel (logChannelId) not found!");
|
2017-09-19 10:38:37 -04:00
|
|
|
}
|
|
|
|
|
2020-08-20 22:13:24 -04:00
|
|
|
if (! (_logChannel instanceof Eris.TextChannel)) {
|
2020-08-12 17:08:37 -04:00
|
|
|
throw new BotError("Make sure the logChannelId option is set to a text channel!");
|
2019-12-05 14:04:45 -05:00
|
|
|
}
|
|
|
|
|
2020-08-20 22:13:24 -04:00
|
|
|
return _logChannel;
|
2017-09-19 10:38:37 -04:00
|
|
|
}
|
|
|
|
|
2018-02-18 14:21:03 -05:00
|
|
|
function postLog(...args) {
|
2020-09-22 20:16:26 -04:00
|
|
|
return getLogChannel().createMessage(...args);
|
2018-02-18 14:21:03 -05:00
|
|
|
}
|
|
|
|
|
2019-06-16 15:27:30 -04:00
|
|
|
function postError(channel, str, opts = {}) {
|
|
|
|
return channel.createMessage({
|
|
|
|
...opts,
|
|
|
|
content: `⚠ ${str}`
|
2017-09-19 13:23:55 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether the given member has permission to use modmail commands
|
2020-05-24 18:33:10 -04:00
|
|
|
* @param {Eris.Member} member
|
2017-09-19 13:23:55 -04:00
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
function isStaff(member) {
|
2019-04-15 09:43:22 -04:00
|
|
|
if (! member) return false;
|
2018-09-20 15:27:59 -04:00
|
|
|
if (config.inboxServerPermission.length === 0) return true;
|
2020-05-24 18:33:10 -04:00
|
|
|
if (member.guild.ownerID === member.id) return true;
|
2018-09-20 15:27:59 -04:00
|
|
|
|
|
|
|
return config.inboxServerPermission.some(perm => {
|
|
|
|
if (isSnowflake(perm)) {
|
|
|
|
// If perm is a snowflake, check it against the member's user id and roles
|
|
|
|
if (member.id === perm) return true;
|
|
|
|
if (member.roles.includes(perm)) return true;
|
|
|
|
} else {
|
|
|
|
// Otherwise assume perm is the name of a permission
|
|
|
|
return member.permission.has(perm);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
});
|
2017-09-19 13:23:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether the given message is on the inbox server
|
|
|
|
* @param msg
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
function messageIsOnInboxServer(msg) {
|
|
|
|
if (! msg.channel.guild) return false;
|
|
|
|
if (msg.channel.guild.id !== getInboxGuild().id) return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether the given message is on the main server
|
|
|
|
* @param msg
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
function messageIsOnMainServer(msg) {
|
|
|
|
if (! msg.channel.guild) return false;
|
2018-04-21 08:38:21 -04:00
|
|
|
|
|
|
|
return getMainGuilds()
|
|
|
|
.some(g => msg.channel.guild.id === g.id);
|
2017-09-19 13:23:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param attachment
|
|
|
|
* @returns {Promise<string>}
|
|
|
|
*/
|
2019-03-06 16:31:24 -05:00
|
|
|
async function formatAttachment(attachment, attachmentUrl) {
|
2017-09-19 13:23:55 -04:00
|
|
|
let filesize = attachment.size || 0;
|
|
|
|
filesize /= 1024;
|
|
|
|
|
|
|
|
return `**Attachment:** ${attachment.filename} (${filesize.toFixed(1)}KB)\n${attachmentUrl}`;
|
|
|
|
}
|
2017-02-09 21:56:36 -05:00
|
|
|
|
2017-02-09 23:36:47 -05:00
|
|
|
/**
|
|
|
|
* Returns the user ID of the user mentioned in str, if any
|
|
|
|
* @param {String} str
|
|
|
|
* @returns {String|null}
|
|
|
|
*/
|
2017-02-09 21:56:36 -05:00
|
|
|
function getUserMention(str) {
|
2019-06-09 13:25:21 -04:00
|
|
|
if (! str) return null;
|
|
|
|
|
2017-02-09 21:56:36 -05:00
|
|
|
str = str.trim();
|
|
|
|
|
2019-06-09 13:25:21 -04:00
|
|
|
if (isSnowflake(str)) {
|
2017-02-09 21:56:36 -05:00
|
|
|
// User ID
|
|
|
|
return str;
|
|
|
|
} else {
|
|
|
|
let mentionMatch = str.match(userMentionRegex);
|
|
|
|
if (mentionMatch) return mentionMatch[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-02-09 23:36:47 -05:00
|
|
|
/**
|
|
|
|
* Returns the current timestamp in an easily readable form
|
|
|
|
* @returns {String}
|
|
|
|
*/
|
2018-02-18 16:29:24 -05:00
|
|
|
function getTimestamp(...momentArgs) {
|
2020-08-12 17:08:37 -04:00
|
|
|
return moment.utc(...momentArgs).format("HH:mm");
|
2017-02-09 21:56:36 -05:00
|
|
|
}
|
|
|
|
|
2017-02-09 23:36:47 -05:00
|
|
|
/**
|
|
|
|
* Disables link previews in the given string by wrapping links in < >
|
|
|
|
* @param {String} str
|
|
|
|
* @returns {String}
|
|
|
|
*/
|
2017-02-09 21:56:36 -05:00
|
|
|
function disableLinkPreviews(str) {
|
2020-08-12 17:08:37 -04:00
|
|
|
return str.replace(/(^|[^<])(https?:\/\/\S+)/ig, "$1<$2>");
|
2017-02-09 21:56:36 -05:00
|
|
|
}
|
|
|
|
|
2017-02-09 23:36:47 -05:00
|
|
|
/**
|
|
|
|
* Returns a URL to the bot's web server
|
|
|
|
* @param {String} path
|
2017-12-31 19:16:05 -05:00
|
|
|
* @returns {Promise<String>}
|
2017-02-09 23:36:47 -05:00
|
|
|
*/
|
2020-08-12 17:08:37 -04:00
|
|
|
async function getSelfUrl(path = "") {
|
2017-02-09 21:56:36 -05:00
|
|
|
if (config.url) {
|
2017-12-31 19:16:05 -05:00
|
|
|
return `${config.url}/${path}`;
|
2017-02-09 21:56:36 -05:00
|
|
|
} else {
|
2017-02-09 23:36:47 -05:00
|
|
|
const port = config.port || 8890;
|
2017-12-31 19:16:05 -05:00
|
|
|
const ip = await publicIp.v4();
|
|
|
|
return `http://${ip}:${port}/${path}`;
|
2017-02-09 21:56:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-09 23:36:47 -05:00
|
|
|
/**
|
|
|
|
* Returns the highest hoisted role of the given member
|
2018-02-11 14:54:30 -05:00
|
|
|
* @param {Eris~Member} member
|
|
|
|
* @returns {Eris~Role}
|
2017-02-09 23:36:47 -05:00
|
|
|
*/
|
|
|
|
function getMainRole(member) {
|
|
|
|
const roles = member.roles.map(id => member.guild.roles.get(id));
|
|
|
|
roles.sort((a, b) => a.position > b.position ? -1 : 1);
|
|
|
|
return roles.find(r => r.hoist);
|
|
|
|
}
|
|
|
|
|
2017-07-23 20:54:03 -04:00
|
|
|
/**
|
|
|
|
* Splits array items into chunks of the specified size
|
2018-05-03 13:33:19 -04:00
|
|
|
* @param {Array|String} items
|
2017-07-23 20:54:03 -04:00
|
|
|
* @param {Number} chunkSize
|
|
|
|
* @returns {Array}
|
|
|
|
*/
|
|
|
|
function chunk(items, chunkSize) {
|
|
|
|
const result = [];
|
|
|
|
|
|
|
|
for (let i = 0; i < items.length; i += chunkSize) {
|
|
|
|
result.push(items.slice(i, i + chunkSize));
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-03-11 15:32:14 -04:00
|
|
|
/**
|
|
|
|
* Trims every line in the string
|
|
|
|
* @param {String} str
|
|
|
|
* @returns {String}
|
|
|
|
*/
|
2018-02-18 14:21:03 -05:00
|
|
|
function trimAll(str) {
|
|
|
|
return str
|
2020-08-12 17:08:37 -04:00
|
|
|
.split("\n")
|
2020-08-20 22:13:24 -04:00
|
|
|
.map(_str => _str.trim())
|
2020-08-12 17:08:37 -04:00
|
|
|
.join("\n");
|
2018-02-18 14:21:03 -05:00
|
|
|
}
|
|
|
|
|
2019-03-06 14:15:09 -05:00
|
|
|
const delayStringRegex = /^([0-9]+)(?:([dhms])[a-z]*)?/i;
|
|
|
|
|
2018-03-11 15:32:14 -04:00
|
|
|
/**
|
|
|
|
* Turns a "delay string" such as "1h30m" to milliseconds
|
|
|
|
* @param {String} str
|
2019-06-09 13:04:40 -04:00
|
|
|
* @returns {Number|null}
|
2018-03-11 15:32:14 -04:00
|
|
|
*/
|
|
|
|
function convertDelayStringToMS(str) {
|
|
|
|
let match;
|
|
|
|
let ms = 0;
|
|
|
|
|
2018-03-13 01:24:01 -04:00
|
|
|
str = str.trim();
|
|
|
|
|
2020-08-12 17:08:37 -04:00
|
|
|
while (str !== "" && (match = str.match(delayStringRegex)) !== null) {
|
|
|
|
if (match[2] === "d") ms += match[1] * 1000 * 60 * 60 * 24;
|
|
|
|
else if (match[2] === "h") ms += match[1] * 1000 * 60 * 60;
|
|
|
|
else if (match[2] === "s") ms += match[1] * 1000;
|
|
|
|
else if (match[2] === "m" || ! match[2]) ms += match[1] * 1000 * 60;
|
2018-03-13 01:24:01 -04:00
|
|
|
|
|
|
|
str = str.slice(match[0].length);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Invalid delay string
|
2020-08-12 17:08:37 -04:00
|
|
|
if (str !== "") {
|
2018-03-13 01:24:01 -04:00
|
|
|
return null;
|
2018-03-11 15:32:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return ms;
|
|
|
|
}
|
|
|
|
|
2020-11-01 14:01:42 -05:00
|
|
|
function getValidMentionRoles() {
|
2019-06-09 12:24:00 -04:00
|
|
|
const mentionRoles = Array.isArray(config.mentionRole) ? config.mentionRole : [config.mentionRole];
|
2020-11-01 14:01:42 -05:00
|
|
|
return mentionRoles.filter(roleStr => {
|
|
|
|
return (roleStr !== null && roleStr !== "none" && roleStr !== "off" && roleStr !== "");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function getInboxMention() {
|
|
|
|
const mentionRoles = getValidMentionRoles();
|
2019-06-09 12:24:00 -04:00
|
|
|
const mentions = [];
|
|
|
|
for (const role of mentionRoles) {
|
2020-11-01 14:01:42 -05:00
|
|
|
if (role === "here") mentions.push("@here");
|
2020-08-12 17:08:37 -04:00
|
|
|
else if (role === "everyone") mentions.push("@everyone");
|
2019-06-09 12:24:00 -04:00
|
|
|
else mentions.push(`<@&${role}>`);
|
|
|
|
}
|
2020-08-12 17:08:37 -04:00
|
|
|
return mentions.join(" ") + " ";
|
2018-03-11 15:55:47 -04:00
|
|
|
}
|
|
|
|
|
2020-08-20 22:13:24 -04:00
|
|
|
function getInboxMentionAllowedMentions() {
|
2020-11-01 14:01:42 -05:00
|
|
|
const mentionRoles = getValidMentionRoles();
|
2020-08-20 22:13:24 -04:00
|
|
|
const allowedMentions = {
|
|
|
|
everyone: false,
|
|
|
|
roles: [],
|
|
|
|
};
|
|
|
|
|
|
|
|
for (const role of mentionRoles) {
|
2020-11-01 14:01:42 -05:00
|
|
|
if (role === "here" || role === "everyone") allowedMentions.everyone = true;
|
2020-08-20 22:13:24 -04:00
|
|
|
else allowedMentions.roles.push(role);
|
|
|
|
}
|
|
|
|
|
|
|
|
return allowedMentions;
|
|
|
|
}
|
|
|
|
|
2018-03-11 17:17:14 -04:00
|
|
|
function postSystemMessageWithFallback(channel, thread, text) {
|
|
|
|
if (thread) {
|
|
|
|
thread.postSystemMessage(text);
|
|
|
|
} else {
|
|
|
|
channel.createMessage(text);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-13 00:23:32 -04:00
|
|
|
/**
|
|
|
|
* A normalized way to set props in data models, fixing some inconsistencies between different DB drivers in knex
|
|
|
|
* @param {Object} target
|
|
|
|
* @param {Object} props
|
|
|
|
*/
|
|
|
|
function setDataModelProps(target, props) {
|
|
|
|
for (const prop in props) {
|
|
|
|
if (! props.hasOwnProperty(prop)) continue;
|
|
|
|
// DATETIME fields are always returned as Date objects in MySQL/MariaDB
|
|
|
|
if (props[prop] instanceof Date) {
|
|
|
|
// ...even when NULL, in which case the date's set to unix epoch
|
|
|
|
if (props[prop].getUTCFullYear() === 1970) {
|
|
|
|
target[prop] = null;
|
|
|
|
} else {
|
|
|
|
// Set the value as a string in the same format it's returned in SQLite
|
2020-08-12 17:08:37 -04:00
|
|
|
target[prop] = moment.utc(props[prop]).format("YYYY-MM-DD HH:mm:ss");
|
2018-03-13 00:23:32 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
target[prop] = props[prop];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-20 15:27:59 -04:00
|
|
|
const snowflakeRegex = /^[0-9]{17,}$/;
|
|
|
|
function isSnowflake(str) {
|
2019-06-09 13:25:21 -04:00
|
|
|
return str && snowflakeRegex.test(str);
|
2018-09-20 15:27:59 -04:00
|
|
|
}
|
|
|
|
|
2020-08-12 17:08:37 -04:00
|
|
|
const humanizeDelay = (delay, opts = {}) => humanizeDuration(delay, Object.assign({conjunction: " and "}, opts));
|
2019-03-06 14:37:36 -05:00
|
|
|
|
2019-03-27 23:03:47 -04:00
|
|
|
const markdownCharsRegex = /([\\_*|`~])/g;
|
|
|
|
function escapeMarkdown(str) {
|
2020-08-12 17:08:37 -04:00
|
|
|
return str.replace(markdownCharsRegex, "\\$1");
|
2019-03-27 23:03:47 -04:00
|
|
|
}
|
|
|
|
|
2020-08-16 18:53:21 -04:00
|
|
|
function disableInlineCode(str) {
|
|
|
|
return str.replace(/`/g, "'");
|
|
|
|
}
|
|
|
|
|
2019-06-16 15:27:30 -04:00
|
|
|
function disableCodeBlocks(str) {
|
|
|
|
return str.replace(/`/g, "`\u200b");
|
|
|
|
}
|
|
|
|
|
2020-01-19 14:25:10 -05:00
|
|
|
function readMultilineConfigValue(str) {
|
2020-08-12 17:08:37 -04:00
|
|
|
return Array.isArray(str) ? str.join("\n") : str;
|
2020-01-19 14:25:10 -05:00
|
|
|
}
|
|
|
|
|
2020-08-16 11:45:11 -04:00
|
|
|
function noop() {}
|
|
|
|
|
2020-10-21 14:03:33 -04:00
|
|
|
// https://discord.com/developers/docs/resources/channel#create-message-params
|
|
|
|
const MAX_MESSAGE_CONTENT_LENGTH = 2000;
|
|
|
|
|
|
|
|
// https://discord.com/developers/docs/resources/channel#embed-limits
|
|
|
|
const MAX_EMBED_CONTENT_LENGTH = 6000;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if the given message content is within Discord's message length limits.
|
|
|
|
*
|
|
|
|
* Based on testing, Discord appears to enforce length limits (at least in the client)
|
|
|
|
* the same way JavaScript does, using the UTF-16 byte count as the number of characters.
|
|
|
|
*
|
|
|
|
* @param {string|Eris.MessageContent} content
|
|
|
|
*/
|
|
|
|
function messageContentIsWithinMaxLength(content) {
|
|
|
|
if (typeof content === "string") {
|
|
|
|
content = { content };
|
|
|
|
}
|
|
|
|
|
|
|
|
if (content.content && content.content.length > MAX_MESSAGE_CONTENT_LENGTH) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (content.embed) {
|
|
|
|
let embedContentLength = 0;
|
|
|
|
|
|
|
|
if (content.embed.title) embedContentLength += content.embed.title.length;
|
|
|
|
if (content.embed.description) embedContentLength += content.embed.description.length;
|
|
|
|
if (content.embed.footer && content.embed.footer.text) {
|
|
|
|
embedContentLength += content.embed.footer.text.length;
|
|
|
|
}
|
|
|
|
if (content.embed.author && content.embed.author.name) {
|
|
|
|
embedContentLength += content.embed.author.name.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (content.embed.fields) {
|
|
|
|
for (const field of content.embed.fields) {
|
|
|
|
if (field.title) embedContentLength += field.name.length;
|
|
|
|
if (field.description) embedContentLength += field.value.length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (embedContentLength > MAX_EMBED_CONTENT_LENGTH) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-10-21 14:36:50 -04:00
|
|
|
/**
|
|
|
|
* Splits a string into chunks, preferring to split at a newline
|
|
|
|
* @param {string} str
|
|
|
|
* @param {number} [maxChunkLength=2000]
|
|
|
|
* @returns {string[]}
|
|
|
|
*/
|
|
|
|
function chunkByLines(str, maxChunkLength = 2000) {
|
|
|
|
if (str.length < maxChunkLength) {
|
|
|
|
return [str];
|
|
|
|
}
|
|
|
|
|
|
|
|
const chunks = [];
|
|
|
|
|
|
|
|
while (str.length) {
|
|
|
|
if (str.length <= maxChunkLength) {
|
|
|
|
chunks.push(str);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
const slice = str.slice(0, maxChunkLength);
|
|
|
|
|
|
|
|
const lastLineBreakIndex = slice.lastIndexOf("\n");
|
|
|
|
if (lastLineBreakIndex === -1) {
|
|
|
|
chunks.push(str.slice(0, maxChunkLength));
|
|
|
|
str = str.slice(maxChunkLength);
|
|
|
|
} else {
|
|
|
|
chunks.push(str.slice(0, lastLineBreakIndex));
|
|
|
|
str = str.slice(lastLineBreakIndex + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return chunks;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Chunks a long message to multiple smaller messages, retaining leading and trailing line breaks, open code blocks, etc.
|
|
|
|
*
|
|
|
|
* Default maxChunkLength is 1990, a bit under the message length limit of 2000, so we have space to add code block
|
|
|
|
* shenanigans to the start/end when needed. Take this into account when choosing a custom maxChunkLength as well.
|
|
|
|
*/
|
|
|
|
function chunkMessageLines(str, maxChunkLength = 1990) {
|
|
|
|
const chunks = chunkByLines(str, maxChunkLength);
|
|
|
|
let openCodeBlock = false;
|
|
|
|
|
|
|
|
return chunks.map(_chunk => {
|
|
|
|
// If the chunk starts with a newline, add an invisible unicode char so Discord doesn't strip it away
|
|
|
|
if (_chunk[0] === "\n") _chunk = "\u200b" + _chunk;
|
|
|
|
// If the chunk ends with a newline, add an invisible unicode char so Discord doesn't strip it away
|
|
|
|
if (_chunk[_chunk.length - 1] === "\n") _chunk = _chunk + "\u200b";
|
|
|
|
// If the previous chunk had an open code block, open it here again
|
|
|
|
if (openCodeBlock) {
|
|
|
|
openCodeBlock = false;
|
|
|
|
if (_chunk.startsWith("```")) {
|
|
|
|
// Edge case: chunk starts with a code block delimiter, e.g. the previous chunk and this one were split right before the end of a code block
|
|
|
|
// Fix: just strip the code block delimiter away from here, we don't need it anymore
|
|
|
|
_chunk = _chunk.slice(3);
|
|
|
|
} else {
|
|
|
|
_chunk = "```" + _chunk;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// If the chunk has an open code block, close it and open it again in the next chunk
|
|
|
|
const codeBlockDelimiters = _chunk.match(/```/g);
|
|
|
|
if (codeBlockDelimiters && codeBlockDelimiters.length % 2 !== 0) {
|
|
|
|
_chunk += "```";
|
|
|
|
openCodeBlock = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return _chunk;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-02-09 21:56:36 -05:00
|
|
|
module.exports = {
|
2017-09-19 13:23:55 -04:00
|
|
|
BotError,
|
|
|
|
|
2017-09-19 10:38:37 -04:00
|
|
|
getInboxGuild,
|
2018-04-21 08:38:21 -04:00
|
|
|
getMainGuilds,
|
2017-09-19 10:38:37 -04:00
|
|
|
getLogChannel,
|
2017-09-19 13:23:55 -04:00
|
|
|
postError,
|
2018-02-18 14:21:03 -05:00
|
|
|
postLog,
|
2017-09-19 13:23:55 -04:00
|
|
|
|
|
|
|
isStaff,
|
|
|
|
messageIsOnInboxServer,
|
|
|
|
messageIsOnMainServer,
|
|
|
|
|
|
|
|
formatAttachment,
|
|
|
|
|
2017-02-09 21:56:36 -05:00
|
|
|
getUserMention,
|
|
|
|
getTimestamp,
|
|
|
|
disableLinkPreviews,
|
|
|
|
getSelfUrl,
|
2017-02-09 23:36:47 -05:00
|
|
|
getMainRole,
|
2019-03-06 14:15:09 -05:00
|
|
|
delayStringRegex,
|
2018-03-11 15:32:14 -04:00
|
|
|
convertDelayStringToMS,
|
2018-03-11 15:55:47 -04:00
|
|
|
getInboxMention,
|
2020-08-20 22:13:24 -04:00
|
|
|
getInboxMentionAllowedMentions,
|
2018-03-11 17:17:14 -04:00
|
|
|
postSystemMessageWithFallback,
|
2018-02-18 14:21:03 -05:00
|
|
|
|
2017-07-23 20:54:03 -04:00
|
|
|
chunk,
|
2018-02-18 14:21:03 -05:00
|
|
|
trimAll,
|
2018-03-13 00:23:32 -04:00
|
|
|
|
|
|
|
setDataModelProps,
|
2018-09-20 15:27:59 -04:00
|
|
|
|
|
|
|
isSnowflake,
|
2019-03-06 14:37:36 -05:00
|
|
|
|
|
|
|
humanizeDelay,
|
2019-03-27 23:03:47 -04:00
|
|
|
|
|
|
|
escapeMarkdown,
|
2020-08-16 18:53:21 -04:00
|
|
|
disableInlineCode,
|
2019-06-16 15:27:30 -04:00
|
|
|
disableCodeBlocks,
|
2020-01-19 14:25:10 -05:00
|
|
|
|
|
|
|
readMultilineConfigValue,
|
2020-08-16 11:45:11 -04:00
|
|
|
|
2020-10-21 14:03:33 -04:00
|
|
|
messageContentIsWithinMaxLength,
|
2020-10-21 14:36:50 -04:00
|
|
|
chunkMessageLines,
|
2020-10-21 14:03:33 -04:00
|
|
|
|
2020-08-16 11:45:11 -04:00
|
|
|
noop,
|
2017-02-09 21:56:36 -05:00
|
|
|
};
|