Haha-Yes/events/client/interactionCreate.js

106 lines
3.8 KiB
JavaScript
Raw Normal View History

2022-08-28 17:04:11 +02:00
import { PermissionFlagsBits, InteractionType } from 'discord.js';
2022-08-14 22:29:19 +02:00
import db from '../../models/index.js';
2022-08-17 16:22:22 +02:00
const { ownerId } = process.env;
2022-06-17 01:25:05 +02:00
export default {
name: 'interactionCreate',
async execute(interaction) {
2022-12-18 22:53:23 +01:00
const ratelimit = global.ratelimit;
const client = interaction.client;
2022-08-28 17:04:11 +02:00
if (interaction.type !== InteractionType.ApplicationCommand) return;
2022-08-14 22:29:19 +02:00
const globalBlacklist = await db.Blacklists.findOne({ where: { type:'global', uid:interaction.user.id } });
const commandBlacklist = await db.Blacklists.findOne({ where: { type:interaction.commandName, uid:interaction.user.id } });
if (globalBlacklist) {
2022-08-14 23:34:19 +02:00
return interaction.reply({ content: `You are globally blacklisted for the following reason: \`${globalBlacklist.reason}\``, ephemeral: true });
2022-08-14 22:29:19 +02:00
}
else if (commandBlacklist) {
2022-08-14 23:34:19 +02:00
return interaction.reply({ content: `You are blacklisted for the following reason: \`${commandBlacklist.reason}\``, ephemeral: true });
2022-08-14 22:29:19 +02:00
}
2022-08-14 23:28:24 +02:00
const userTag = interaction.user.tag;
const userID = interaction.user.id;
const commandName = interaction.commandName;
2022-08-14 23:28:24 +02:00
const command = client.commands.get(commandName);
2022-06-16 13:29:23 +02:00
if (!command) return;
2022-08-17 16:22:22 +02:00
console.log(`\x1b[33m${userTag} (${userID})\x1b[0m launched command \x1b[33m${commandName}\x1b[0m`);
// Owner only check
2022-08-17 16:22:22 +02:00
if (command.ownerOnly && interaction.user.id !== ownerId) {
return interaction.reply({ content: '❌ This command is reserved for the owner!', ephemeral: true });
}
// Check if the bot has the needed permissions
if (command.default_permission) {
const clientMember = await interaction.guild.members.fetch(client.user.id);
if (!clientMember.permissions.has(command.clientPermissions)) {
2022-08-28 17:04:11 +02:00
return interaction.reply({ content: `❌ I am missing one of the following permission(s): \`${new PermissionFlagsBits(command.clientPermissions).toArray()}\``, ephemeral: true });
}
}
// Check if the user has the needed permissions
/*
if (command.default_member_permissions) {
if (!interaction.member.permissions.has(command.userPermissions)) {
2022-08-28 17:04:11 +02:00
return interaction.reply({ content: `❌ You are missing one of the following permission(s): \`${new PermissionFlagsBits(command.userPermissions).toArray()}\``, ephemeral: true });
}
}
*/
try {
2022-12-18 22:53:23 +01:00
if (!ratelimit[userID]) {
ratelimit[userID] = {};
}
2022-08-14 23:28:24 +02:00
const date = new Date();
2022-12-18 22:53:23 +01:00
if (ratelimit[userID][commandName]) {
if (ratelimit[userID][commandName].cooldown) {
if (date > ratelimit[userID][commandName].cooldown) {
ratelimit[userID][commandName].limit = 0;
ratelimit[userID][commandName].cooldown = undefined;
2022-08-14 23:28:24 +02:00
}
}
2022-12-18 22:53:23 +01:00
if (command.ratelimit === ratelimit[userID][commandName].limit) {
return await interaction.reply({ content: `You are being rate limited. You can try again in ${Math.floor((ratelimit[userID][commandName].cooldown - date) / 1000)} seconds.`, ephemeral: true });
2022-08-14 23:28:24 +02:00
}
}
2022-12-18 22:53:23 +01:00
2022-08-14 23:28:24 +02:00
if (command.ratelimit) {
2022-12-18 22:53:23 +01:00
ratelimit[userID][commandName] = { limit: ratelimit[userID][commandName] ? ratelimit[userID][commandName].limit + 1 : 1 };
if (command.ratelimit === ratelimit[userID][commandName].limit) {
2022-08-14 23:28:24 +02:00
date.setSeconds(date.getSeconds() + command.cooldown);
2022-12-18 22:53:23 +01:00
ratelimit[userID][commandName] = { limit: ratelimit[userID][commandName].limit, cooldown: date };
2022-08-14 23:28:24 +02:00
}
}
2022-08-28 17:04:11 +02:00
2022-11-24 21:34:54 +01:00
interaction.prefix = '/';
2022-09-01 01:43:59 +02:00
const args = {};
// https://discord-api-types.dev/api/discord-api-types-v10/enum/ApplicationCommandOptionType
2022-08-28 17:04:11 +02:00
interaction.options.data.forEach(arg => {
2022-09-01 01:43:59 +02:00
let payload = arg.value;
if (arg.type === 9) {
payload = arg.member;
2022-08-28 17:04:11 +02:00
}
2022-09-01 01:43:59 +02:00
else if (arg.type === 11) {
payload = arg.attachment;
2022-08-28 17:04:11 +02:00
}
2022-09-01 01:43:59 +02:00
args[arg.name] = payload;
2022-08-28 17:04:11 +02:00
});
await command.execute(interaction, args, client);
}
catch (error) {
console.error(error);
await interaction.followUp({ content: 'There was an error while executing this command!', ephemeral: true });
}
},
};