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•6d 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•6d 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.
Skullfromroblox
SkullfromrobloxOP•6d ago
Huh? im very tired, so im very slow-
Amgelo
Amgelo•6d 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
Skullfromroblox
SkullfromrobloxOP•6d 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•6d ago
oh, and you're trying to call that execute method on messageReactionAdd, right?
Skullfromroblox
SkullfromrobloxOP•6d ago
No? I dont know?
Amgelo
Amgelo•6d ago
did you make that code yourself? or at the very least understand what it does
Skullfromroblox
SkullfromrobloxOP•6d ago
I had ai help me, i kinda understand it…
Amgelo
Amgelo•6d 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
Skullfromroblox
SkullfromrobloxOP•6d ago
I followed it, but i never fully understood events and that stuff. Stuff like buttons, panels, etc.
Amgelo
Amgelo•6d 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
Skullfromroblox
SkullfromrobloxOP•6d ago
Ik.
Amgelo
Amgelo•6d ago
and there's resources for learning that at the beginning of the guide
Skullfromroblox
SkullfromrobloxOP•6d ago
How would i do the event thing?
Amgelo
Amgelo•6d 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
Skullfromroblox
SkullfromrobloxOP•6d ago
Is this the event handler pattern?
No description
Skullfromroblox
SkullfromrobloxOP•6d ago
Im gonna head to sleep. Ill talk tmrw
Amgelo
Amgelo•6d ago
no, that's just for handling commands
Skullfromroblox
SkullfromrobloxOP•6d ago
Can you send the part in the docs for event handler patterns? I dont know.
d.js docs
d.js docs•6d ago
:guide: Creating Your Bot: Event handling read more
Skullfromroblox
SkullfromrobloxOP•6d 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•6d ago
have you read and understood what it does?
Skullfromroblox
SkullfromrobloxOP•6d 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•6d ago
yes
Skullfromroblox
SkullfromrobloxOP•6d 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•6d 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?
Skullfromroblox
SkullfromrobloxOP•6d ago
No. Im gonna re follow the guide.

Did you find this page helpful?