discord_bot/functions.js

174 lines
5.3 KiB
JavaScript

const yes = ['yes', 'y', 'ye', 'yea', 'correct'];
const no = ['no', 'n', 'nah', 'nope', 'fuck off'];
const MONEY = ['', 'k', 'M', 'G', 'T', 'P', 'E'];
const inviteRegex = /(https?:\/\/)?(www\.|canary\.|ptb\.)?discord(\.gg|(app)?\.com\/invite|\.me)\/([^ ]+)\/?/gi;
const botInvRegex = /(https?:\/\/)?(www\.|canary\.|ptb\.)?discord(app)\.com\/(api\/)?oauth2\/authorize\?([^ ]+)\/?/gi;
module.exports = {
getMember(message, toFind = '') {
toFind = toFind.toLowerCase();
var target = message.guild.members.cache.get(toFind);
if (!target && message.mentions.members)
target = message.mentions.members.first();
if (!target && toFind) {
target = message.guild.members.cache.find(member => {
return member.displayName.toLowerCase().includes(toFind) ||
member.user.tag.toLowerCase().includes(toFind)
});
}
if (!target)
target = message.member;
return target;
},
delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
},
formatDate: function (date) {
return new Intl.DateTimeFormat('en-US').format(date);
},
promptMessage: async function (message, author, time, validReactions) {
time *= 1000;
for (const reaction of validReactions) await message.react(reaction);
const filter = (reaction, user) => validReactions.includes(reaction.emoji.name) && user.id === author.id;
return message
.awaitReactions(filter, { max: 1, time: time })
.then(collected => collected.first() && collected.first().emoji.name);
},
drawImageWithTint: function (ctx, image, color, x, y, width, height) {
const { fillStyle, globalAlpha } = ctx;
ctx.fillStyle = color;
ctx.drawImage(image, x, y, width, height);
ctx.globalAlpha = 0.5;
ctx.fillRect(x, y, width, height);
ctx.fillStyle = fillStyle;
ctx.globalAlpha = globalAlpha;
},
randomRange(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
},
shuffle: function (array) {
const arr = array.slice(0);
for (let i = arr.length - 1; i >= 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
const temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
return arr;
},
verify: async function (channel, user, { time = 30000, extraYes = [], extraNo = [] } = {}) {
const filter = res => {
const value = res.content.toLowerCase();
return (user ? res.author.id === user.id : true)
&& (yes.includes(value) || no.includes(value) || extraYes.includes(value) || extraNo.includes(value));
};
const verify = await channel.awaitMessages(filter, {
max: 1,
time
});
if (!verify.size) return 0;
const choice = verify.first().content.toLowerCase();
if (yes.includes(choice) || extraYes.includes(choice)) return true;
if (no.includes(choice) || extraNo.includes(choice)) return false;
return false;
},
chunk: function (array, chunkSize) {
const temp = [];
for (let i = 0; i < array.length; i += chunkSize) {
temp.push(array.slice(i, i + chunkSize));
}
return temp;
},
getWrapText: function (text, length) {
const temp = [];
for (let i = 0; i < text.length; i += length) {
temp.push(text.slice(i, i + length));
}
return temp.map(x => x.trim());
},
crFormat: function (number) {
const ranking = Math.log10(number) / 3 | 0;
if (!ranking) return number.toString();
const last = MONEY[ranking];
const scale = Math.pow(10, ranking * 3);
const scaled = number / scale;
return `${scaled.toFixed(2)}${last}`;
},
formatNumber(number, minimumFractionDigits = 0) {
return Number.parseFloat(number).toLocaleString(undefined, {
minimumFractionDigits,
maximumFractionDigits: 2
});
},
list: function (arr, conj = 'and') {
const len = arr.length;
if (len === 0) return '';
if (len === 1) return arr[0];
return `${arr.slice(0, -1).join(', ')}${len > 1 ? `${len > 2 ? ',' : ''} ${conj} ` : ''}${arr.slice(-1)}`;
},
firstUpperCase(text, split = ' ') {
return text.split(split).map(word => `${word.charAt(0).toUpperCase()}${word.slice(1)}`).join(' ');
},
shorten(text, maxLen = 2000) {
return text.length > maxLen ? `${text.substr(0, maxLen - 3)}...` : text;
},
stripInvites(str, { guild = true, bot = true, text = '[redacted invite]' } = {}) {
if (guild) str = str.replace(inviteRegex, text);
if (bot) str = str.replace(botInvRegex, text);
return str;
},
wrapText (ctx, text, maxWidth) {
return new Promise(resolve => {
if (ctx.measureText(text).width < maxWidth) return resolve([text]);
if (ctx.measureText('W').width > maxWidth) return resolve(null);
const words = text.split(' ');
const lines = [];
let line = '';
while (words.length > 0) {
let split = false;
while (ctx.measureText(words[0]).width >= maxWidth) {
const temp = words[0];
words[0] = temp.slice(0, -1);
if (split) {
words[1] = `${temp.slice(-1)}${words[1]}`;
} else {
split = true;
words.splice(1, 0, temp.slice(-1));
}
}
if (ctx.measureText(`${line}${words[0]}`).width < maxWidth) {
line += `${words.shift()} `;
} else {
lines.push(line.trim());
line = '';
}
if (words.length === 0) lines.push(line.trim());
}
return resolve(lines);
});
}
}