woomy-v2/bot/util/functions.js

161 lines
4.6 KiB
JavaScript
Raw Normal View History

// const { MessageCollector } = require('eris-collector');
2020-10-20 05:47:05 +00:00
const { inspect, promisify } = require('util');
2020-10-25 02:10:05 +00:00
class Functions {
2020-10-20 05:47:05 +00:00
constructor (client) {
this.client = client;
}
async getLastMessage (channel) {
const messages = await channel.messages.fetch({ limit: 2 });
return messages.last().content;
}
2020-10-22 07:50:28 +00:00
async awaitReply (message, input, limit = 60000) {
2020-10-20 05:47:05 +00:00
const filter = (m) => m.author.id === message.author.id;
2020-10-22 07:50:28 +00:00
await message.channel.createMessage(input);
2020-10-20 05:47:05 +00:00
try {
const collected = await message.channel.awaitMessages(filter, {
max: 1,
time: limit,
errors: ['time']
});
return collected.first().content;
} catch (err) {
return false;
}
}
2020-10-28 01:17:54 +00:00
randomColour () {
const n = (Math.random() * 0xfffff * 1000000).toString(16);
return '#' + n.slice(0, 6);
}
roleObjects (guild, roles) {
const roleMap = roles.map(roleID => guild.roles.get(roleID));
return roleMap.sort((a, b) => b.position - a.position);
}
displayHexColour (guild, userID) {
const roles = this.roleObjects(guild, guild.members.get(userID).roles);
for (const object of roles) {
if (object.color !== 0) return '#' + object.color.toString(16);
}
const colours = Object.keys(this.client.constants.colours);
return this.client.constants.colours[colours[ colours.length * Math.random() << 0]];
}
2020-10-23 08:11:06 +00:00
highestRole (member) {
if (member.roles.length === 0) return member.guild.roles.find(r => r.name === '@everyone');
2020-10-20 05:47:05 +00:00
2020-10-23 08:11:06 +00:00
let highestRole;
2020-10-20 05:47:05 +00:00
2020-10-23 08:11:06 +00:00
for (const roleID of member.roles) {
const role = member.guild.roles.get(roleID);
if (!highestRole || highestRole.position < role.position) highestRole = role;
}
return highestRole;
}
2020-10-20 05:47:05 +00:00
2020-10-23 08:11:06 +00:00
findRole (input, guild) {
2020-10-20 05:47:05 +00:00
let role;
2020-10-23 08:11:06 +00:00
role = guild.roles.find(r => r.name.toLowerCase() === input.toLowerCase());
2020-10-20 05:47:05 +00:00
if (!role) {
2020-10-23 08:11:06 +00:00
role = guild.roles.get(input.toLowerCase());
2020-10-20 05:47:05 +00:00
}
if (!role) return;
return role;
}
checkPermissions (channel, user_id, requiredPerms) {
const minimumPerms = ['readMessages', 'sendMessages', 'embedLinks'];
const pendingPerms = (!requiredPerms) ? minimumPerms : minimumPerms.concat(requiredPerms);
2020-10-20 05:47:05 +00:00
const missingPerms = [];
pendingPerms.forEach(p => {
if (!channel.permissionsOf(user_id).has(p)) missingPerms.push(p);
});
2020-10-20 05:47:05 +00:00
if (missingPerms.length > 0) return missingPerms;
return;
2020-10-20 05:47:05 +00:00
}
intBetween (min, max) {
return Math.round((Math.random() * (max - min) + min));
}
shutdown () {
const exitQuotes = [
'Shutting down.',
'I don\'t blame you.',
'I don\'t hate you.',
'Whyyyyy',
'Goodnight.',
'Goodbye'
];
this.client.disconnect();
2020-10-20 05:47:05 +00:00
this.client.logger.success('SHUTDOWN_SUCCESS', exitQuotes.random());
2020-10-20 05:47:05 +00:00
process.exit();
2020-10-20 05:47:05 +00:00
}
async getUser (id) {
if (this.client.users.has(id)) return this.client.users.get(id);
this.client.logger.debug('REST_FETCH_USER', 'Accessing rest API...');
const user = await this.client.getRESTUser(id).catch(err => {
this.client.logger.error('USER_FETCH_ERROR', err);
});
if (user) {
this.client.users.set(id, user);
return user;
}
return;
}
async getGuild (id) {
if (this.client.guilds.has(id)) return this.client.guilds.get(id);
this.client.logger.debug('REST_FETCH_GUILD', 'Accessing rest API...');
const guild = await this.client.getRESTGuild(id).catch(err => {
this.client.logger.error('GUILD_FETCH_ERROR', err);
});
if (guild) {
this.client.guilds.set(id, guild);
return guild;
}
return;
}
2020-10-20 05:47:05 +00:00
async clean (text) {
if (text && text.constructor.name === 'Promise') {
text = await text;
}
if (typeof text !== 'string') {
text = inspect(text, { depth: 1});
}
text = text
.replace(/`/g, '`' + String.fromCharCode(8203))
.replace(/@/g, '@' + String.fromCharCode(8203))
.replace(this.client.token, 'mfa.VkO_2G4Qv3T--NO--lWetW_tjND--TOKEN--QFTm6YGtzq9PH--4U--tG0');
return text;
}
wait () {
promisify(setTimeout);
}
}
2020-10-25 02:10:05 +00:00
module.exports = Functions;