woomy-v2/bot/util/functions.js

115 lines
3.1 KiB
JavaScript
Raw Normal View History

2020-10-09 05:02:35 +00:00
const { MessageEmbed } = require('discord.js')
2020-10-08 02:05:06 +00:00
class Functions {
2020-08-18 04:58:36 +00:00
constructor (client) {
this.client = client;
}
2020-10-09 05:02:35 +00:00
userError (channel, cmd, error) {
const embed = new MessageEmbed()
embed.setColor('#EF5350')
embed.setTitle(cmd.help.name + ':' + cmd.help.category.toLowerCase())
embed.setDescription(error)
embed.addField('**Usage**', cmd.help.usage)
embed.setFooter(`Run 'help ${cmd.help.name}' for more information.`)
channel.send(embed).then(msg => {
msg.delete({ timeout: 60000 })
})
}
2020-08-18 08:29:36 +00:00
// Simple check to see if someone is a developer or not
2020-08-18 04:58:36 +00:00
isDeveloper (userID) {
2020-10-10 02:15:40 +00:00
if (this.client.config.ownerIDs.includes(userID)) {
return true;
};
return false;
};
2020-08-18 04:58:36 +00:00
2020-08-18 08:29:36 +00:00
// Cleans output and removes sensitive information, used by eval
2020-08-18 04:58:36 +00:00
async clean (text) {
if (text && text.constructor.name == "Promise")
text = await text;
if (typeof text !== "string")
text = require("util").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;
}
2020-08-18 08:29:36 +00:00
// Return random integer between two given integers
intBetween (min, max) {
return Math.round((Math.random() * (max - min))+min);
}
2020-10-08 02:06:06 +00:00
// Grab the last message sent in a channel (excludes the command)
async getLastMessage (channel) {
if (channel) {
let lastMsg;
2020-08-18 08:29:36 +00:00
2020-10-08 02:06:06 +00:00
await channel.messages.fetch({ limit: 2 }).then(messages => {
lastMsg = messages.last().content;
});
return lastMsg;
}
}
// Wait for a reply to a message, then pass through the response
async awaitReply (msg, question, limit = 60000) {
const filter = m => m.author.id === msg.author.id;
await msg.channel.send(question);
try {
const collected = await msg.channel.awaitMessages(filter, {
max: 1,
time: limit,
errors: ["time"]
});
return collected.first().content;
} catch (e) {
return false;
}
}
searchForMembers (guild, query) {
if (!query) return;
query = query.toLowerCase();
var matches = [];
var match;
try {
match = guild.members.cache.find(x => x.displayName.toLowerCase() == query);
if (!match) guild.members.cache.find(x => x.user.username.toLowerCase() == query);
} catch (err) {};
if (match) matches.push(match);
guild.members.cache.forEach(member => {
if (
(member.displayName.toLowerCase().startsWith(query) ||
member.user.tag.toLowerCase().startsWith(query)) &&
member.id != (match && match.id)
) {
matches.push(member);
};
});
return matches;
};
findRole (input, message) {
var role;
role = message.guild.roles.cache.find(r => r.name.toLowerCase() === input.toLowerCase());
if(!role) {
role = message.guild.roles.cache.get(input.toLowerCase());
};
if(!role) return;
return role;
};
wait () {
require("util").promisify(setTimeout);
}
2020-08-18 04:58:36 +00:00
}
2020-10-08 02:05:06 +00:00
module.exports = Functions;