Skip to content

Commit

Permalink
refactor(interactions): use built-in methods to check the interaction…
Browse files Browse the repository at this point in the history
… type
  • Loading branch information
SuperchupuDev committed Oct 30, 2021
1 parent 190d1a6 commit bc8d4e5
Showing 1 changed file with 53 additions and 56 deletions.
109 changes: 53 additions & 56 deletions events/interactionCreate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
};

0 comments on commit bc8d4e5

Please sign in to comment.