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

Event handler error #1409

Closed Fulwing closed 1 year ago

Fulwing commented 1 year ago

Part of the guide or code sample the question is about

I follow the code till here

Question

TypeError: Cannot read properties of undefined (reading 'get') at Object.execute (D:\Discord-Bot\discord-bottest\events\interactionCreate.js:6:47) at Client.<anonymous> (D:\Discord-Bot\discord-bottest\index.js:18:44) at Client.emit (node:events:512:28) at InteractionCreateAction.handle (D:\Discord-Bot\discord-bottest\node_modules\discord.js\src\client\actions\InteractionCreate.js:97:12) at module.exports [as INTERACTION_CREATE] (D:\Discord-Bot\discord-bottest\node_modules\discord.js\src\client\websocket\handlers\INTERACTION_CREATE.js:4:36) at WebSocketManager.handlePacket (D:\Discord-Bot\discord-bottest\node_modules\discord.js\src\client\websocket\WebSocketManager.js:352:31) at WebSocketShard.onPacket (D:\Discord-Bot\discord-bottest\node_modules\discord.js\src\client\websocket\WebSocketShard.js:489:22) at WebSocketShard.onMessage (D:\Discord-Bot\discord-bottest\node_modules\discord.js\src\client\websocket\WebSocketShard.js:328:10) at callListener (D:\Discord-Bot\discord-bottest\node_modules\ws\lib\event-target.js:290:14) at WebSocket.onMessage (D:\Discord-Bot\discord-bottest\node_modules\ws\lib\event-target.js:209:9) Emitted 'error' event on Client instance at: at emitUnhandledRejectionOrErr (node:events:393:10) at process.processTicksAndRejections (node:internal/process/task_queues:84:21)

and I got this error, I am sure I got every code correct, I don't know why do I have this error

Fulwing commented 1 year ago
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);
}

I added this to the index.js that solves the problem, but I don't know do I suppose to do that

almostSouji commented 1 year ago

looks like - in doing up to here - you skipped half of the command guide, for example, this section: https://discordjs.guide/creating-your-bot/command-handling.html#loading-command-files

Fulwing commented 1 year ago

this is the guide source code

I copy and paste them into my work(everything is the same as the guide), but when I try /ping that error appears. If I add the code I comment on the top in index.js. The error will be gone and it will respond to me with a pong. But I am not sure, is the event handler trying to do is to avoid the code I put in. So i don't know is this the correct way to solve this problem or there are a real correct way of solving it

Fulwing commented 1 year ago

Ok, so this will be included in the index.js? When I got to Event handler the index.js in the resulting code don't have that, so I deleted it.

almostSouji commented 1 year ago

i see, looks like this scrapped most of the command loader without mentioning it at all we generally recommend following the guide, not just hoping the resulting code samples work in any case, even after a potential fix, the event handler will not have as in-depth code (perhaps even an empty command list).

it should however not throw an error - or at the very least have a comment referencing to the other section

tracking in https://github.com/discordjs/guide/issues/1411 with a clear description of the problem

Fulwing commented 1 year ago

Yeah, I understand we need these codes in the index.js

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);
}

It is just a bit confusing if the resulting code is not the same, because I would think that, the resulting code should be the code I am having right now if I follow the guide. The guide teaches really well, I think it is better to add this part of the code to the index.js of the Event Handler resulting code. Easier to understand. I deleted that part because when I check the resulting code, that part is gone so I think it should be deleted from my index.js. Just advise on the reader's side. That will give a better understanding, but you could decide. Thank you so much for solving my problem! Have a nice day!