tan
DIAdiscord.js - Imagine an app
•Created by tan on 6/22/2024 in #djs-questions
Failed to load slash commands
const prefix = 'K.';
client.commands = new Collection();
const readCommands = (dir) => {
const files = fs.readdirSync(dir);
for (const file of files) {
const filePath = path.join(dir, file);
const stat = fs.statSync(filePath);
if (stat.isDirectory()) {
readCommands(filePath);
} else if (file.endsWith('.js')) {
const command = require(filePath);
client.commands.set(command.data.name, command);
if (command.data.slash) {
client.guilds.cache.forEach((guild) => {
guild.commands.create(command.data);
});
}
}
}
};
// Read all command files recursively from the 'commands' directory
readCommands(path.join(__dirname, 'commands'));
const commandsData = [
{
name: 'info',
description: 'Get information about the bot.',
slash: true,
},
];
commandsData.forEach((command) => {
client.guilds.cache.forEach((guild) => {
guild.commands.create(command);
});
});
client.on('messageCreate', async (message) => {
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).trim().split(/ +/);
const commandName = args.shift().toLowerCase();
const command = client.commands.get(commandName);
if (!command) return;
try {
await command.execute(message, args);
} catch (error) {
console.error(error);
client.on('interactionCreate', async (interaction) => { if (!interaction.isCommand()) return; const { commandName } = interaction; const command = client.commands.get(commandName); if (!command) return; try { await command.execute(interaction); } catch (error) { console.error(error);
client.on('interactionCreate', async (interaction) => { if (!interaction.isCommand()) return; const { commandName } = interaction; const command = client.commands.get(commandName); if (!command) return; try { await command.execute(interaction); } catch (error) { console.error(error);
32 replies