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:

import { Listener } from '@sapphire/framework';

export class RawGuildMemberAddListener extends Listener {
public constructor(context: Listener.Context, options: Listener.Options) {
super(context, {
...options,
emitter: 'ws',
event: 'GUILD_MEMBER_ADD'
});
}
}

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

import { Listener, container } from '@sapphire/framework';

export class RawGuildMemberAddListener extends Listener {
public constructor(context: Listener.Context, options: Listener.Options) {
super(context, {
...options,
emitter: container.client.ws,
event: 'GUILD_MEMBER_ADD'
});
}
}

The code is equivalent to the following:

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