ramirez/src/utils.js

128 lines
2.7 KiB
JavaScript
Raw Normal View History

2017-02-09 23:36:47 -05:00
const Eris = require('eris');
2017-02-09 21:56:36 -05:00
const moment = require('moment');
const publicIp = require('public-ip');
const config = require('../config');
const utils = require('./utils');
2017-09-19 10:38:37 -04:00
let inboxGuild = null;
let mainGuild = null;
2017-09-19 10:38:37 -04:00
let logChannel = null;
2017-09-19 10:38:37 -04:00
function getInboxGuild(bot) {
if (! inboxGuild) inboxGuild = bot.guilds.find(g => g.id === config.mailGuildId);
return inboxGuild;
2017-02-09 21:56:36 -05:00
}
function getMainGuild(bot) {
if (! mainGuild) mainGuild = bot.guilds.find(g => g.id === config.mainGuildId);
return mainGuild;
}
2017-09-19 10:38:37 -04:00
function getLogChannel(bot) {
const inboxGuild = getInboxGuild(bot);
if (! config.logChannelId) {
return inboxGuild.channels.get(inboxGuild.id);
}
if (! logChannel) {
logChannel = inboxGuild.channels.get(config.logChannelId);
}
return logChannel;
}
2017-02-09 21:56:36 -05:00
const userMentionRegex = /^<@\!?([0-9]+?)>$/;
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) {
str = str.trim();
if (str.match(/^[0-9]+$/)) {
// 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
* @param {String|Date|undefined} date
* @returns {String}
*/
2017-02-09 21:56:36 -05:00
function getTimestamp(date) {
return moment.utc(date).format('HH:mm');
}
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) {
return str.replace(/(^|[^<])(https?:\/\/\S+)/ig, '$1<$2>');
}
2017-02-09 23:36:47 -05:00
/**
* Returns a URL to the bot's web server
* @param {String} path
* @returns {String}
*/
function getSelfUrl(path = '') {
2017-02-09 21:56:36 -05:00
if (config.url) {
return Promise.resolve(`${config.url}/${path}`);
} else {
2017-02-09 23:36:47 -05:00
const port = config.port || 8890;
2017-02-09 21:56:36 -05:00
return publicIp.v4().then(ip => {
2017-02-09 23:36:47 -05:00
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}
*/
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} 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;
}
2017-02-09 21:56:36 -05:00
module.exports = {
2017-09-19 10:38:37 -04:00
getInboxGuild,
getMainGuild,
2017-09-19 10:38:37 -04:00
getLogChannel,
2017-02-09 21:56:36 -05:00
getUserMention,
getTimestamp,
disableLinkPreviews,
getSelfUrl,
2017-02-09 23:36:47 -05:00
getMainRole,
chunk,
2017-02-09 21:56:36 -05:00
};