Silveradoz
Silveradoz

Reputation: 49

Discord.js: Manually triggering events

I have a basic moderation bot in discord.js, written in node.js. I need to run some tests on the bots but to do so, I need to toggle an event. I know that node.js has an e.dispatchEvent(event) but, to my knowledge, discord.js does not have a function like that.

I was wondering what the equivalent of that would be.

Upvotes: 4

Views: 13634

Answers (2)

Caltrop
Caltrop

Reputation: 935

A Discord Client extends a Node.js EventEmitter, so you can use the EventEmitter#emit() method to call listener functions attached to the event. For example...

// Assuming 'client' is a Client, 'member' is a GuildMember
client.emit('guildMemberAdd', member);

You can find a complete list of events normally emitted by a Client here.

Upvotes: 15

slothiful
slothiful

Reputation: 5623

In Discord.js, the Client class is an extension of the Node.js EventEmitter class. Therefore, you can use the EventEmitter.emit() method to emit events yourself.

Simple example:

// Instantiate a Discord Client
const { Client } = require('discord.js');
const client = new Client();

// Attach a listener function
client.on('test', console.log);

// Emit the event
client.emit('test', 'These params', 'will be logged', 'via the listener.');

Upvotes: 8

Related Questions