woomy-v2/bot/util/handlers/messageHandler.js

115 lines
5.4 KiB
JavaScript
Raw Normal View History

2020-10-21 07:57:00 +00:00
class MessageHandler {
constructor (client) {
this.client = client;
}
async handle (message) {
2020-10-23 03:26:30 +00:00
// Ignore messages from bots, and messages in DM's
if (message.author.bot) return;
if (!message.channel.guild) return;
2020-10-21 07:57:00 +00:00
2020-10-23 03:25:07 +00:00
// Request all the data we need from the database
2020-10-21 07:57:00 +00:00
const data = {};
data.user = await this.client.db.getUser(message.author.id);
2020-10-23 03:25:07 +00:00
data.guild = await this.client.db.getGuild(message.channel.guild.id);
data.member = await this.client.db.getMember(message.channel.guild.id, message.author.id);
2020-10-21 07:57:00 +00:00
2020-10-23 03:26:30 +00:00
// Ignore users on the guild blocklist
2020-10-23 08:10:32 +00:00
if (data.guild.blocklist.includes(message.author.id)) return;
2020-10-23 03:26:30 +00:00
// If a user pings Woomy, respond to them with the prefixes they can use
if (message.content === `<@${this.client.user.id}>` || message.content === `<@!${this.client.user.id}>`) {
return message.channel.createMessage(
2020-10-26 01:09:06 +00:00
`Hi! The prefix for this server is \`${data.guild.prefix}\`, and your personal prefix is \`${data.user.prefix}\`. You can also ping me ^-^`
2020-10-23 03:26:30 +00:00
);
}
2020-10-21 07:57:00 +00:00
2020-10-23 03:25:07 +00:00
// All the prefixes Woomy will respond to
const prefixes = [
data.user.prefix,
data.guild.prefix,
`<@${this.client.user.id}> `,
`<@!${this.client.user.id}> `
];
2020-10-21 07:57:00 +00:00
let prefix;
2020-10-23 03:26:30 +00:00
// Check the message content to see if it starts with one of our prefixes
2020-10-21 07:57:00 +00:00
for (const thisPrefix of prefixes) {
if (message.content.startsWith(thisPrefix)) {
prefix = thisPrefix;
break;
}
}
2020-10-23 03:26:30 +00:00
// Ignore the message if it doesn't start with a valid prefix
2020-10-21 07:57:00 +00:00
if (!prefix) return;
2020-10-23 03:26:30 +00:00
// Turn the message content into an array (excluding the prefix)
2020-10-21 07:57:00 +00:00
const args = message.content.slice(prefix.length).trim().split(/ +/g);
2020-10-23 03:26:30 +00:00
// Find the command
2020-10-21 07:57:00 +00:00
const commandName = args.shift().toLowerCase();
const command = this.client.commands.get(commandName) || this.client.commands.get(this.client.aliases.get(commandName));
2020-10-23 03:26:30 +00:00
// Return if a command (or its aliases) are not found
2020-10-21 07:57:00 +00:00
if (!command) return;
2020-10-23 03:26:30 +00:00
// Both of these blocks check if the command is disabled/in a disabled category
if (data.guild.disabledcommands.includes(command.name)) return message.channel.createMessage(
2020-10-23 08:10:32 +00:00
this.client.constants.emojis.permError + ' This command has been disabled by a server administrator.'
2020-10-23 03:26:30 +00:00
);
2020-10-21 07:57:00 +00:00
2020-10-23 03:26:30 +00:00
if (data.guild.disabledcategories.includes(command.category)) return message.channel.createMessage(
2020-10-23 08:10:32 +00:00
this.client.constants.emojis.permError + ' The category this command is apart of has been disabled by a server administrator.'
2020-10-23 03:26:30 +00:00
);
2020-10-21 07:57:00 +00:00
2020-10-23 03:26:30 +00:00
// Both of these blocks check the permissions of the user, and reply with missing perms if any are found
2020-10-25 02:10:05 +00:00
const missingUserPerms = this.client.functions.checkPermissions(message.channel, message.author.id, command.userPerms);
2020-10-23 03:26:30 +00:00
if (missingUserPerms) return message.channel.createMessage(
2020-10-23 08:10:32 +00:00
`${this.client.constants.emojis.permError} You can't use this command because you lack these permissions: \`${missingUserPerms.join('`, `')}\``
2020-10-23 03:26:30 +00:00
);
2020-10-21 07:57:00 +00:00
2020-10-25 02:10:05 +00:00
const missingBotPerms = this.client.functions.checkPermissions(message.channel, this.client.user.id, command.botPerms);
2020-10-23 03:26:30 +00:00
if (missingBotPerms) return message.channel.createMessage(
2020-10-23 08:10:32 +00:00
`${this.client.constants.emojis.permError} I can't run this command because I lack these permissions: \`${missingBotPerms.join('`, `')}\``
2020-10-23 03:26:30 +00:00
);
2020-10-21 07:57:00 +00:00
2020-10-23 03:26:30 +00:00
// Return if the command is disabled globally
if (command.enabled === false) return message.channel.createMessage(
2020-10-23 08:10:32 +00:00
this.client.constants.emojis.permError + ' This command has been disabled by my developers.'
2020-10-23 03:26:30 +00:00
);
2020-10-21 07:57:00 +00:00
2020-10-23 03:26:30 +00:00
// Return if the command is restricted to developers (and the user is not a developer)
2020-10-29 01:57:17 +00:00
if (command.devOnly === true && this.client.config.ownerIDs.includes(message.author.id) !== true) {
2020-10-24 04:02:47 +00:00
return message.channel.createMessage(
2020-10-25 21:13:03 +00:00
`${this.client.constants.emojis.permError} ${message.author.username} is not in the sudoers file. This incident will be reported.`
2020-10-23 08:10:32 +00:00
);
2020-10-21 07:57:00 +00:00
}
2020-10-22 01:14:28 +00:00
// Cooldown
if (this.client.cooldowns.get(command.name).has(message.author.id)) {
const timestamp = this.client.cooldowns.get(command.name).get(message.author.id);
const currentTime = Date.now();
const cooldown = command.cooldown / 1000;
const timePassed = Math.floor((currentTime - timestamp) / 1000);
return message.channel.createMessage(
2020-10-23 08:10:32 +00:00
`${this.client.constants.emojis.wait} ${message.author.mention}, you need to wait ${cooldown - timePassed} seconds before using this command again.`
);
2020-10-22 01:14:28 +00:00
} else {
this.client.cooldowns.get(command.name).set(message.author.id, new Date());
setTimeout(() => {
this.client.cooldowns.get(command.name).delete(message.author.id);
}, this.client.commands.get(command.name).cooldown);
}
2020-10-21 07:57:00 +00:00
try {
command.run(this.client, message, args, data);
this.client.logger.command(`Ran ${command.name}`);
} catch (error) {
this.client.logger.error('COMMAND_EXECUTION_ERROR', `${command.name}: ${error}`);
}
}
}
module.exports = MessageHandler;