discordjs / guide

The official guide for discord.js, created and maintained by core members of its community.
https://discordjs.guide
MIT License
1.57k stars 2.31k forks source link

Resulting Code (Event handler) is missing #commands definition #1411

Closed almostSouji closed 1 year ago

almostSouji commented 1 year ago

Tries to get from Client#commands

https://github.com/discordjs/guide/blob/6bea5078bc76911f9600d0ef80448610a73b8f61/code-samples/creating-your-bot/event-handling/events/interactionCreate.js#L6

but never defines it:

https://github.com/discordjs/guide/blob/6bea5078bc76911f9600d0ef80448610a73b8f61/code-samples/creating-your-bot/event-handling/index.js#L6-L21

Resulting Code is supposed to be a working Code base to fall back to and compare against.

Bunnyfrost commented 1 year ago

Do this in your index.js above eventsPath = ....

client.commands = new Collection(); const commandsPath = path.join(__dirname, 'commands'); 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); client.commands.set(command.data.name, command); }