discordjs / guide

The official guide for discord.js, created and maintained by core members of its community.

Home Page:https://discordjs.guide

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Registering Slash Commands Example incongruent with folder structure

Mrfoofums opened this issue · comments

Describe the bug

In order to register slash commands as shown in the example you need to properly traverse 'commands' folder structure.

The example you provide is

`const commands = [];
// Grab all the command files from the commands directory you created earlier
const commandsPath = path.join(__dirname, 'commands');
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));

// Grab the SlashCommandBuilder#toJSON() output of each command's data for deployment
for (const file of commandFiles) {
const command = require(./commands/${file});
commands.push(command.data.toJSON());
}`

But you need to utilize the previously shown traversal method in index.js

`const foldersPath = path.join(__dirname, 'commands');
const commandFolders = fs.readdirSync(foldersPath);

for (const folder of commandFolders) {
const commandsPath = path.join(foldersPath, folder);
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
// Set a new item in the Collection with the key as the command name and the value as the exported module
if ('data' in command && 'execute' in command) {
client.commands.set(command.data.name, command);
} else {
console.log([WARNING] The command at ${filePath} is missing a required "data" or "execute" property.);
}
}
}`

To Reproduce

Follow the guide as shown

Screenshots

No response

Device (please complete the following information)

Mac

Additional notes

No response

Also saw this

commented

duplicate of #1140 ig