0% found this document useful (0 votes)
17 views2 pages

Handle Commands

This code handles command interactions by checking if the command is valid, if the user has permission to run it, and if the bot has permission. It gets the local commands, finds the matching one, and checks flags like if it's only for devs or a test server before executing the callback if permitted.

Uploaded by

rtdnf yghetwe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views2 pages

Handle Commands

This code handles command interactions by checking if the command is valid, if the user has permission to run it, and if the bot has permission. It gets the local commands, finds the matching one, and checks flags like if it's only for devs or a test server before executing the callback if permitted.

Uploaded by

rtdnf yghetwe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

const { devs, testServer } = require('../../../config.

json');
const getLocalCommands = require('../../utils/getLocalCommands');

module.exports = async (client, interaction) => {


if (!interaction.isChatInputCommand()) return;

const localCommands = getLocalCommands();

try {
const commandObject = localCommands.find(
(cmd) => cmd.name === interaction.commandName
);

if (!commandObject) return;

if (commandObject.devOnly) {
if (!devs.includes(interaction.member.id)) {
interaction.reply({
content: 'Only developers are allowed to run this command.',
ephemeral: true,
});
return;
}
}

if (commandObject.testOnly) {
if (!(interaction.guild.id === testServer)) {
interaction.reply({
content: 'This command cannot be ran here.',
ephemeral: true,
});
return;
}
}

if (commandObject.permissionsRequired?.length) {
for (const permission of commandObject.permissionsRequired) {
if (!interaction.member.permissions.has(permission)) {
interaction.reply({
content: 'Not enough permissions.',
ephemeral: true,
});
return;
}
}
}

if (commandObject.botPermissions?.length) {
for (const permission of commandObject.botPermissions) {
const bot = interaction.guild.members.me;

if (!bot.permissions.has(permission)) {
interaction.reply({
content: "I don't have enough permissions.",
ephemeral: true,
});
return;
}
}
}

await commandObject.callback(client, interaction);


} catch (error) {
console.log(`There was an error running this command: ${error}`);
}
};

You might also like