Haha-Yes/events/client/interactionCreate.js

129 lines
4.7 KiB
JavaScript
Raw Normal View History

2024-01-30 00:25:41 +01:00
// TODO: Moving that to a dedicated function that works for both messages and interactions
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-12-18 23:30:25 +01:00
import ratelimiter from '../../utils/ratelimiter.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) {
const client = interaction.client;
2022-08-28 17:04:11 +02:00
if (interaction.type !== InteractionType.ApplicationCommand) return;
2024-07-11 07:19:25 +02:00
const globalBlacklist = await db.Blacklists.findOne({ where: { type:'global', uid:interaction.user.id } });
2022-08-14 22:29:19 +02:00
const commandBlacklist = await db.Blacklists.findOne({ where: { type:interaction.commandName, uid:interaction.user.id } });
2024-07-11 07:19:25 +02:00
2024-06-30 20:05:09 +02:00
if (interaction.guild) {
const serverBlacklist = await db.Blacklists.findOne({ where: { type:'guild', uid:interaction.guild.id } });
if (serverBlacklist) {
interaction.reply({ content: `This guild has been blacklisted for the following reason: \`${serverBlacklist.reason}\``, ephemeral: true });
return interaction.guild.leave();
}
}
2022-08-14 22:29:19 +02:00
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
}
2023-09-13 20:56:06 +02:00
const userTag = interaction.user.username;
2022-08-14 23:28:24 +02:00
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;
2024-07-11 07:19:25 +02:00
let isOptOut = await db.optout.findOne({ where: { userID: interaction.user.id } });
if (commandName === 'optout') {
2024-07-11 07:19:25 +02:00
isOptOut = true;
}
2024-07-11 07:19:25 +02:00
2023-12-12 21:45:09 +01:00
const timestamp = new Date();
console.log(`[${timestamp.toISOString()}] \x1b[33m${ isOptOut ? 'A user' : `${userTag} (${userID})`}\x1b[0m launched command \x1b[33m${commandName}\x1b[0m using slash`);
2022-08-17 16:22:22 +02:00
// 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 });
}
2023-04-14 17:46:12 +02:00
// Guild only check
if (command.guildOnly && !interaction.guild) {
return interaction.reply({ content: '❌ This command only work in a server!', 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 });
}
}
*/
// Check if the limit of parallel execution has been reached
if (command.parallelLimit) {
const doParallelLimit = await ratelimiter.checkParallel(interaction.user, commandName, command);
if (doParallelLimit.limited) {
return await interaction.reply({ content: doParallelLimit.msg, ephemeral: true });
}
ratelimiter.addParallel(commandName);
}
2022-12-18 23:30:25 +01:00
// Check the ratelimit
const doRateLimit = await ratelimiter.check(interaction.user, commandName, command);
2022-12-18 23:30:25 +01:00
if (doRateLimit) {
return interaction.reply({ content: doRateLimit, ephemeral: true });
2022-12-18 22:53:23 +01:00
2022-12-18 23:30:25 +01:00
}
2022-12-18 22:53:23 +01:00
2022-12-18 23:30:25 +01:00
try {
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
});
if (!isOptOut) {
2023-12-12 21:45:09 +01:00
console.log(`[${timestamp.toISOString()}] \x1b[33m⤷\x1b[0m with args ${JSON.stringify(args)}`);
}
await command.execute(interaction, args, client)
.then(async () => {
const hasPrallelLimit = await ratelimiter.checkParallel(interaction.user, commandName, command);
if (hasPrallelLimit) ratelimiter.removeParallel(commandName);
});
}
catch (error) {
console.error(error);
const hasPrallelLimit = await ratelimiter.checkParallel(interaction.user, commandName, command);
if (hasPrallelLimit) ratelimiter.removeParallel(commandName);
2024-01-30 00:25:41 +01:00
await interaction.followUp({ content: `There was an error while executing this command!\n\`${error}\``, ephemeral: true });
}
},
};