ramirez/src/utils.js

382 lines
8.9 KiB
JavaScript
Raw Normal View History

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 {}
const userMentionRegex = /^<@!?([0-9]+?)>$/;
2017-02-09 21:56:36 -05:00
2017-09-19 10:38:37 -04:00
let inboxGuild = null;
let mainGuilds = [];
2017-09-19 10:38:37 -04:00
let logChannel = null;
/**
* @returns {Eris~Guild}
*/
2017-09-19 13:23:55 -04:00
function getInboxGuild() {
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
}
/**
* @returns {Eris~Guild[]}
*/
function getMainGuilds() {
if (mainGuilds.length === 0) {
mainGuilds = bot.guilds.filter(g => config.mainServerId.includes(g.id));
}
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!");
} else {
2020-08-12 17:08:37 -04:00
console.warn("[WARN] The bot hasn't joined one or more main guilds!");
}
}
return mainGuilds;
}
2017-09-19 13:23:55 -04:00
/**
* Returns the designated log channel, or the default channel if none is set
* @returns {Eris~TextChannel}
2017-09-19 13:23:55 -04:00
*/
function getLogChannel() {
const _inboxGuild = getInboxGuild();
const _logChannel = _inboxGuild.channels.get(config.logChannelId);
2017-09-19 10:38:37 -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
}
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!");
}
return _logChannel;
2017-09-19 10:38:37 -04:00
}
function postLog(...args) {
return getLogChannel().createMessage(...args);
}
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
* @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;
if (config.inboxServerPermission.length === 0) return true;
if (member.guild.ownerID === member.id) return true;
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;
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) {
if (! str) return null;
2017-02-09 21:56:36 -05:00
str = str.trim();
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}
*/
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
* @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);
}
/**
* Splits array items into chunks of the specified size
* @param {Array|String} items
* @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;
}
/**
* Trims every line in the string
* @param {String} str
* @returns {String}
*/
function trimAll(str) {
return str
2020-08-12 17:08:37 -04:00
.split("\n")
.map(_str => _str.trim())
2020-08-12 17:08:37 -04:00
.join("\n");
}
const delayStringRegex = /^([0-9]+)(?:([dhms])[a-z]*)?/i;
/**
* Turns a "delay string" such as "1h30m" to milliseconds
* @param {String} str
* @returns {Number|null}
*/
function convertDelayStringToMS(str) {
let match;
let ms = 0;
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;
str = str.slice(match[0].length);
}
// Invalid delay string
2020-08-12 17:08:37 -04:00
if (str !== "") {
return null;
}
return ms;
}
function getInboxMention() {
const mentionRoles = Array.isArray(config.mentionRole) ? config.mentionRole : [config.mentionRole];
const mentions = [];
for (const role of mentionRoles) {
if (role == null) continue;
2020-08-12 17:08:37 -04:00
else if (role === "here") mentions.push("@here");
else if (role === "everyone") mentions.push("@everyone");
else mentions.push(`<@&${role}>`);
}
2020-08-12 17:08:37 -04:00
return mentions.join(" ") + " ";
}
function getInboxMentionAllowedMentions() {
const mentionRoles = Array.isArray(config.mentionRole) ? config.mentionRole : [config.mentionRole];
const allowedMentions = {
everyone: false,
roles: [],
};
for (const role of mentionRoles) {
if (role == null || role === "none" || role === "") continue;
else if (role === "here" || role === "everyone") allowedMentions.everyone = true;
else allowedMentions.roles.push(role);
}
return allowedMentions;
}
function postSystemMessageWithFallback(channel, thread, text) {
if (thread) {
thread.postSystemMessage(text);
} else {
channel.createMessage(text);
}
}
/**
* 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");
}
} else {
target[prop] = props[prop];
}
}
}
const snowflakeRegex = /^[0-9]{17,}$/;
function isSnowflake(str) {
return str && snowflakeRegex.test(str);
}
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
const markdownCharsRegex = /([\\_*|`~])/g;
function escapeMarkdown(str) {
2020-08-12 17:08:37 -04:00
return str.replace(markdownCharsRegex, "\\$1");
}
2020-08-16 18:53:21 -04:00
function disableInlineCode(str) {
return str.replace(/`/g, "'");
}
function disableCodeBlocks(str) {
return str.replace(/`/g, "`\u200b");
}
function readMultilineConfigValue(str) {
2020-08-12 17:08:37 -04:00
return Array.isArray(str) ? str.join("\n") : str;
}
function noop() {}
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,
getMainGuilds,
2017-09-19 10:38:37 -04:00
getLogChannel,
2017-09-19 13:23:55 -04:00
postError,
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,
delayStringRegex,
convertDelayStringToMS,
getInboxMention,
getInboxMentionAllowedMentions,
postSystemMessageWithFallback,
chunk,
trimAll,
setDataModelProps,
isSnowflake,
2019-03-06 14:37:36 -05:00
humanizeDelay,
escapeMarkdown,
2020-08-16 18:53:21 -04:00
disableInlineCode,
disableCodeBlocks,
readMultilineConfigValue,
noop,
2017-02-09 21:56:36 -05:00
};