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:
- CommonJS
- ESM
- TypeScript
const { Listener } = require('@sapphire/framework');
class RawGuildMemberAddListener extends Listener {
constructor(context, options) {
super(context, {
...options,
emitter: 'ws',
event: 'GUILD_MEMBER_ADD'
});
}
}
module.exports = {
RawGuildMemberAddListener
};
import { Listener } from '@sapphire/framework';
export class RawGuildMemberAddListener extends Listener {
constructor(context, options) {
super(context, {
...options,
emitter: 'ws',
event: 'GUILD_MEMBER_ADD'
});
}
}
import { Listener } from '@sapphire/framework';
export class RawGuildMemberAddListener extends Listener {
public constructor(context: Listener.LoaderContext, 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:
- CommonJS
- ESM
- TypeScript
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
};
import { Listener, container } from '@sapphire/framework';
export class RawGuildMemberAddListener extends Listener {
constructor(context, options) {
super(context, {
...options,
emitter: container.client.ws,
event: 'GUILD_MEMBER_ADD'
});
}
}
import { Listener, container } from '@sapphire/framework';
export class RawGuildMemberAddListener extends Listener {
public constructor(context: Listener.LoaderContext, options: Listener.Options) {
super(context, {
...options,
emitter: container.client.ws,
event: 'GUILD_MEMBER_ADD'
});
}
}
The code is equivalent to the following:
- CommonJS
- ESM
- TypeScript
client.ws.on('GUILD_MEMBER_ADD', (data) => {
// `data` here is the raw packet
});
client.ws.on('GUILD_MEMBER_ADD', (data) => {
// `data` here is the raw packet
});
client.ws.on('GUILD_MEMBER_ADD', (data) => {
// `data` here is the raw packet
});