woomy-v2/bot/util/helpers.js

117 lines
3.1 KiB
JavaScript
Raw Normal View History

2020-10-22 07:50:28 +00:00
const { MessageCollector } = require('eris-collector');
2020-10-20 05:47:05 +00:00
const { inspect, promisify } = require('util');
class Helpers {
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-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));
}
isDeveloper (id) {
if (this.client.config.ownerIDs.includes(id)) return true;
return false;
2020-10-20 05:47:05 +00:00
}
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 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);
}
}
module.exports = Helpers;