woomy-v2/bot/event_modules/interactionCreate/interactionHandler.js

54 lines
2.5 KiB
JavaScript

module.exports = class {
constructor (wsEvent) {
this.wsEvent = wsEvent;
}
async run (client, interaction) {
if (!interaction.isChatInputCommand()) return;
// Request all the data we need from the database
const data = {};
data.user = await client.db.getUser(interaction.user.id);
data.guild = await client.db.getGuild(interaction.guild.id);
data.member = await client.db.getMember(interaction.guild.id, interaction.user.id);
const command = client.commands.get(interaction.commandName);
// Return if the command is disabled globally
if (command.enabled === false) interaction.reply(
client.config.emojis.permError + ' This command has been disabled by my developers.'
);
// Return if the command is restricted to developers (and the user is not a developer)
if (command.devOnly === true && client.config.devIds.includes(interaction.user.id) !== true) {
return interaction.reply(
`${client.config.emojis.permError} ${interaction.user.username} is not in the sudoers file. This incident will be reported.`
);
}
// Cooldown
if (client.cooldowns.get(command.name).has(interaction.user.id)) {
const timestamp = client.cooldowns.get(command.name).get(interaction.user.id);
const currentTime = Date.now();
const cooldown = command.cooldown / 1000;
const timePassed = Math.floor((currentTime - timestamp) / 1000);
return interaction.reply(
`${client.config.emojis.wait} <@${interaction.user.id}>, you need to wait ${cooldown - timePassed} seconds before using this command again.`
);
} else {
client.cooldowns.get(command.name).set(interaction.user.id, new Date());
setTimeout(() => {
client.cooldowns.get(command.name).delete(interaction.user.id);
}, client.commands.get(command.name).cooldown);
}
// Try to execute the command, if it fails return error stack and inform the user
try {
command.run(client, interaction, data);
client.logger.command(`Ran ${command.name}`);
} catch (error) {
client.logger.error('COMMAND_EXECUTION_ERROR', `${command.name}: ${error.stack}`);
interaction.reply(`${client.config.emojis.botError} An error occurred when I was trying to run this command. I've sent through the details of the error to my developers.`);
}
}
};