woomy-v2/bot/util/functions.js

132 lines
3.5 KiB
JavaScript
Raw Normal View History

2020-10-17 01:16:38 +00:00
const { MessageEmbed } = require('discord.js');
const { inspect, promisify } = require('util');
2020-10-09 05:02:35 +00:00
2020-10-08 02:05:06 +00:00
class Functions {
2020-10-17 01:16:38 +00:00
constructor (client) {
this.client = client;
2020-10-17 05:00:41 +00:00
}
2020-10-17 01:16:38 +00:00
userError (channel, cmd, error) {
const embed = new MessageEmbed()
.setColor('#EF5350')
.setTitle(`${cmd.help.name}:${cmd.help.category.toLowerCase()}`)
.setDescription(error)
.addField('**Usage**', cmd.help.usage)
.setFooter(`Run 'help ${cmd.help.name}' for more information.`);
channel.send(embed);
2020-10-17 05:00:41 +00:00
}
2020-10-17 01:16:38 +00:00
async getLastMessage (channel) {
2020-10-17 05:17:02 +00:00
const messages = await channel.messages.fetch({ limit: 2 });
2020-10-17 01:16:38 +00:00
return messages.last().content;
2020-10-17 05:00:41 +00:00
}
2020-10-17 01:16:38 +00:00
2020-10-17 05:17:02 +00:00
async awaitReply (message, question, limit = 60000) {
2020-10-17 01:16:38 +00:00
const filter = (m) => m.author.id === message.author.id;
await message.channel.send(question);
try {
const collected = await message.channel.awaitMessages(filter, {
max: 1,
time: limit,
errors: ['time']
});
return collected.first().content;
} catch (err) {
return false;
}
2020-10-08 02:06:06 +00:00
}
2020-10-17 01:16:38 +00:00
searchForMembers (guild, query) {
query = query.toLowerCase();
2020-10-17 05:17:02 +00:00
const matches = [];
2020-10-17 01:16:38 +00:00
let match;
try {
match = guild.members.cache.find(x => x.displayName.toLowerCase() == query);
if (!match) guild.members.cache.find(x => x.user.username.toLowerCase() == query);
2020-10-17 05:00:41 +00:00
} catch (err) {} //eslint-disable-line no-empty
2020-10-17 01:16:38 +00:00
if (match) matches.push(match);
guild.members.cache.forEach(member => {
2020-10-17 05:00:41 +00:00
if (
(member.displayName.toLowerCase().startsWith(query) ||
2020-10-17 01:16:38 +00:00
member.user.tag.toLowerCase().startsWith(query)) &&
member.id != (match && match.id)
) {
matches.push(member);
2020-10-17 05:00:41 +00:00
}
2020-10-17 01:16:38 +00:00
});
return matches;
2020-10-17 05:00:41 +00:00
}
2020-10-17 01:16:38 +00:00
findRole (input, message) {
let role;
role = message.guild.roles.cache.find(r => r.name.toLowerCase() === input.toLowerCase());
2020-10-17 05:17:02 +00:00
if (!role) {
2020-10-17 01:16:38 +00:00
role = message.guild.roles.cache.get(input.toLowerCase());
2020-10-17 05:00:41 +00:00
}
2020-10-17 05:17:02 +00:00
if (!role) return;
2020-10-17 01:16:38 +00:00
return role;
2020-10-17 05:00:41 +00:00
}
2020-10-17 01:16:38 +00:00
intBetween (min, max) {
return Math.round((Math.random() * (max - min) + min));
2020-10-17 05:00:41 +00:00
}
2020-10-17 01:16:38 +00:00
isDeveloper (id) {
if (this.client.config.ownerIDs.includes(id)) {
return true;
} else {
return false;
2020-10-17 05:00:41 +00:00
}
}
2020-10-17 01:16:38 +00:00
shutdown () {
const exitQuotes = [
'Shutting down.',
'I don\'t blame you.',
'I don\'t hate you.',
'Whyyyyy',
'Goodnight.',
'Goodbye'
];
this.client.db.pool.end().then(() => {
2020-10-17 05:00:41 +00:00
this.client.logger.info('Connection to database closed.');
2020-10-17 01:16:38 +00:00
});
this.client.destroy();
console.log(exitQuotes);
2020-10-17 05:00:41 +00:00
}
2020-10-17 01:16:38 +00:00
async clean (text) {
if (text && text.constructor.name === 'Promise') {
text = await text;
2020-10-17 05:00:41 +00:00
}
2020-10-17 01:16:38 +00:00
if (typeof text !== 'string') {
text = inspect(text, { depth: 1});
2020-10-17 05:00:41 +00:00
}
2020-10-17 01:16:38 +00:00
text = text
2020-10-17 05:00:41 +00:00
.replace(/`/g, '`' + String.fromCharCode(8203))
.replace(/@/g, '@' + String.fromCharCode(8203))
.replace(this.client.token, 'mfa.VkO_2G4Qv3T--NO--lWetW_tjND--TOKEN--QFTm6YGtzq9PH--4U--tG0');
2020-10-17 01:16:38 +00:00
return text;
2020-10-17 05:00:41 +00:00
}
2020-10-17 01:16:38 +00:00
wait () {
promisify(setTimeout);
2020-10-17 05:00:41 +00:00
}
}
2020-08-18 04:58:36 +00:00
2020-10-08 02:05:06 +00:00
module.exports = Functions;