How to execute an event?

I have an event that I made, and that i dont know how to make that event be ran when I react to a message. Please help.
28 Replies
d.js toolkit
d.js toolkit•7mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button!
d.js docs
d.js docs•7mo ago
:node: emitter.emit(eventName[, ...args]) Synchronously calls each of the listeners registered for the event named eventName, in the order they were registered, passing the supplied arguments to each.Returns true if the event had listeners, false otherwise.
Skull
SkullOP•7mo ago
Huh? im very tired, so im very slow-
Amgelo
Amgelo•7mo ago
maybe return back once you're not tired but either way I doubt you're asking about djs since you shouldn't emit djs events by yourself it sounds like you're trying to emit an unrelated to djs event on an emitter you made, or the client
Skull
SkullOP•7mo ago
One sec. I have this? module.exports = { name: 'messageReactionAdd', async execute(reaction, user) { if (user.bot) return; if (reaction.partial) { try { await reaction.fetch(); } catch (error) { console.error('Error fetching reaction:', error); return; } } const targetEmoji = '🔥'; if (reaction.emoji.name === targetEmoji) { const isSuccess = Math.random() < 0.5; if (isSuccess) { console.log('Success!'); } else { console.log('Failure!'); } } }, };
Amgelo
Amgelo•7mo ago
oh, and you're trying to call that execute method on messageReactionAdd, right?
Skull
SkullOP•7mo ago
No? I dont know?
Amgelo
Amgelo•7mo ago
did you make that code yourself? or at the very least understand what it does
Skull
SkullOP•7mo ago
I had ai help me, i kinda understand it…
Amgelo
Amgelo•7mo ago
yeah the ai is probably writing that based on the most common pattern for event handlers which is the one the guide describes did you follow the guide for making your bot? or are you starting from scratch
Skull
SkullOP•7mo ago
I followed it, but i never fully understood events and that stuff. Stuff like buttons, panels, etc.
Amgelo
Amgelo•7mo ago
you should read it, follow it, and if at some point you don't understand how something works, ask here though it does expect some level of knowledge in js
Skull
SkullOP•7mo ago
Ik.
Amgelo
Amgelo•7mo ago
and there's resources for learning that at the beginning of the guide
Skull
SkullOP•7mo ago
How would i do the event thing?
Amgelo
Amgelo•7mo ago
well if you don't understand or haven't followed the event handler pattern in the guide, that'll be hard and long to explain because you do need that programmed in your bot at least for that codeblock to do anything at all
Skull
SkullOP•7mo ago
Is this the event handler pattern?
No description
Skull
SkullOP•7mo ago
Im gonna head to sleep. Ill talk tmrw
Amgelo
Amgelo•7mo ago
no, that's just for handling commands
Skull
SkullOP•7mo ago
Can you send the part in the docs for event handler patterns? I dont know.
d.js docs
d.js docs•7mo ago
:guide: Creating Your Bot: Event handling read more
Skull
SkullOP•7mo ago
Thank You . THERES A WHOLE PART I MISSED?! const eventsPath = path.join(__dirname, 'events'); const eventFiles = fs.readdirSync(eventsPath).filter(file => file.endsWith('.js')); for (const file of eventFiles) { const filePath = path.join(eventsPath, file); const event = require(filePath); if (event.once) { client.once(event.name, (...args) => event.execute(...args)); } else { client.on(event.name, (...args) => event.execute(...args)); } } If i add all this, would it like, work?
Amgelo
Amgelo•7mo ago
have you read and understood what it does?
Skull
SkullOP•7mo ago
Yeah. It basically does what the commands do, but for the events. Itll go into the events folder, take the event, and use it. Right?
Amgelo
Amgelo•7mo ago
yes
Skull
SkullOP•7mo ago
Okay. I mustve missed it when i went through the guide. Thanks for your help. I appreciate it. Imma head to sleep though. Do i have to node the event too? The event handler didnt work? Am i supposed to do something else?
Samtino
Samtino•7mo ago
No. The require(filePath) means that you are loading the file at "filePath" into memory. You only ever manually run 1 file with node (typically an index.js file) What didn't work? Did you get an error. Did you NOT get a log claiming it worked? Do you even have logging to ensure it does or doesn't work?
Skull
SkullOP•7mo ago
No. Im gonna re follow the guide.

Did you find this page helpful?