From bc8d4e5b52833a5d838d4f85996da8b8b3d97e87 Mon Sep 17 00:00:00 2001 From: Superchupu Date: Sat, 30 Oct 2021 12:45:58 +0100 Subject: [PATCH] refactor(interactions): use built-in methods to check the interaction type --- events/interactionCreate.js | 109 ++++++++++++++++++------------------ 1 file changed, 53 insertions(+), 56 deletions(-) diff --git a/events/interactionCreate.js b/events/interactionCreate.js index fc63aa0..0949875 100644 --- a/events/interactionCreate.js +++ b/events/interactionCreate.js @@ -4,63 +4,60 @@ module.exports = { async execute(interaction, client) { if (!interaction.inCachedGuild()) return; - switch (interaction.type) { - case 'APPLICATION_COMMAND': - const name = interaction.commandName; - const command = client.interactions.commands.get(name) ?? client.interactions.commands.find(cmd => cmd.contextMenu === name); - - if (!command) return; - - if (!interaction.member.permissions.has(command.permissions ?? 0n)) { - return interaction.reply({ - content: `You need the \`${command.permissions}\` permission to use this command`, - ephemeral: true - }); - } - - if (!interaction.guild.me.permissions.has(command.botPermissions ?? 0n)) { - return interaction.reply({ - embeds: [{ - title: 'Missinq Permissions', - description: `I need the \`${command.botPermissions}\` permission to use this command`, - color: client.config.color - }], - ephemeral: true - }); - } - - command.execute(client, interaction); - break; - - - case 'MESSAGE_COMPONENT': - [interaction.name, interaction.value, interaction.author] = interaction.customId.split(':'); - const component = client.interactions.components.get(interaction.name); - - if (!component || interaction.author !== interaction.user.id) { - return interaction.deferUpdate(); - } - - if (!interaction.member.permissions.has(component.permissions ?? 0n)) { - return interaction.reply({ - content: `You need the \`${component.permissions}\` permission to use this component`, - ephemeral: true - }); - } - - if (!interaction.guild.me.permissions.has(component.botPermissions ?? 0n)) { - return interaction.reply({ - embeds: [{ - title: 'Missinq Permissions', - description: `I need the \`${component.botPermissions}\` permission to use this component`, - color: client.config.color - }], - ephemeral: true - }); - } + if (interaction.isApplicationCommand()) { + const name = interaction.commandName; + const command = client.interactions.commands.get(name) ?? client.interactions.commands.find(c => c.contextMenu === name); + + if (!command) return; + + if (!interaction.member.permissions.has(command.permissions ?? 0n)) { + return interaction.reply({ + content: `You need the \`${command.permissions}\` permission to use this command`, + ephemeral: true + }); + } + + if (!interaction.guild.me.permissions.has(command.botPermissions ?? 0n)) { + return interaction.reply({ + embeds: [{ + title: 'Missinq Permissions', + description: `I need the \`${command.botPermissions}\` permission to use this command`, + color: client.config.color + }], + ephemeral: true + }); + } + + return command.execute(client, interaction); + } - component.execute(client, interaction); - break; + if (interaction.isMessageComponent) { + [interaction.name, interaction.value, interaction.author] = interaction.customId.split(':'); + const component = client.interactions.components.get(interaction.name); + + if (!component || interaction.author !== interaction.user.id) { + return interaction.deferUpdate(); + } + + if (!interaction.member.permissions.has(component.permissions ?? 0n)) { + return interaction.reply({ + content: `You need the \`${component.permissions}\` permission to use this component`, + ephemeral: true + }); + } + + if (!interaction.guild.me.permissions.has(component.botPermissions ?? 0n)) { + return interaction.reply({ + embeds: [{ + title: 'Missinq Permissions', + description: `I need the \`${component.botPermissions}\` permission to use this component`, + color: client.config.color + }], + ephemeral: true + }); + } + + component.execute(client, interaction); } } };