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
- 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!: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.Huh?
im very tired, so im very slow-
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
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!');
}
}
},
};
oh, and you're trying to call that execute method on messageReactionAdd, right?
No?
I dont know?
did you make that code yourself?
or at the very least understand what it does
I had ai help me, i kinda understand it…
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
I followed it, but i never fully understood events and that stuff. Stuff like buttons, panels, etc.
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
Ik.
and there's resources for learning that at the beginning of the guide
How would i do the event thing?
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
Is this the event handler pattern?

Im gonna head to sleep.
Ill talk tmrw
no, that's just for handling commands
Can you send the part in the docs for event handler patterns?
I dont know.
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?
have you read and understood what it does?
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?
yes
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?
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?No. Im gonna re follow the guide.