Skip to main content

Listening to events emitted by other emitters

By default, Sapphire's event emitter is your client. However, for advanced use cases, you can specify a different emitter.

One method is to set the name a Client property containing the emitter like so:

const { Listener } = require('@sapphire/framework');

class RawGuildMemberAddListener extends Listener {
constructor(context, options) {
super(context, {
...options,
emitter: 'ws',
event: 'GUILD_MEMBER_ADD'
});
}
}
module.exports = {
RawGuildMemberAddListener
};

The other way is to specify an emitter directly instead of a property name:

const { Listener, container } = require('@sapphire/framework');

class RawGuildMemberAddListener extends Listener {
constructor(context, options) {
super(context, {
...options,
emitter: container.client.ws,
event: 'GUILD_MEMBER_ADD'
});
}
}
module.exports = {
RawGuildMemberAddListener
};

The code is equivalent to the following:

client.ws.on('GUILD_MEMBER_ADD', (data) => {
// `data` here is the raw packet
});