Custom Events not Creating Listeners

I'm having issues using Sapphire with custom events. For example, I have an event for when a member subscribes to a role:
import { ApplyOptions } from '@sapphire/decorators';
import { container, Listener } from '@sapphire/framework';
import { type GuildMember } from 'discord.js';

const commandOptions = { event: 'guildMemberUpdate' };

@ApplyOptions<Listener.Options>(commandOptions)
export class ListenerExt extends Listener {
public async run(oldGuildMember: GuildMember, newGuildMember: GuildMember) {
// ! Subscriber events.
// Check if the guild member is premium.
const isPremium = newGuildMember.roles.cache.some((role) => role.tags?.premiumSubscriberRole);

// Check if the guild member was premium.
const wasPremium = oldGuildMember.roles.cache.some((role) => role.tags?.premiumSubscriberRole);

// Check if the guild member's premiums status changed.
if (isPremium && !wasPremium) {
container.client.emit('guildMemberSubscribed', newGuildMember);
} else if (!isPremium && wasPremium) {
container.client.emit('guildMemberUnsubscribed', newGuildMember);
}
}
}
import { ApplyOptions } from '@sapphire/decorators';
import { container, Listener } from '@sapphire/framework';
import { type GuildMember } from 'discord.js';

const commandOptions = { event: 'guildMemberUpdate' };

@ApplyOptions<Listener.Options>(commandOptions)
export class ListenerExt extends Listener {
public async run(oldGuildMember: GuildMember, newGuildMember: GuildMember) {
// ! Subscriber events.
// Check if the guild member is premium.
const isPremium = newGuildMember.roles.cache.some((role) => role.tags?.premiumSubscriberRole);

// Check if the guild member was premium.
const wasPremium = oldGuildMember.roles.cache.some((role) => role.tags?.premiumSubscriberRole);

// Check if the guild member's premiums status changed.
if (isPremium && !wasPremium) {
container.client.emit('guildMemberSubscribed', newGuildMember);
} else if (!isPremium && wasPremium) {
container.client.emit('guildMemberUnsubscribed', newGuildMember);
}
}
}
Then, in another Sapphire listener:
import { ApplyOptions } from '@sapphire/decorators';
import { Listener } from '@sapphire/framework';
import { type GuildMember } from 'discord.js';

const commandOptions = { event: 'guildMemberUnsubscribed' };

@ApplyOptions<Listener.Options>(commandOptions)
export class ListenerExt extends Listener {
public async run(guildMember: GuildMember) {
// Remove all perks from the guild member.
}
}
import { ApplyOptions } from '@sapphire/decorators';
import { Listener } from '@sapphire/framework';
import { type GuildMember } from 'discord.js';

const commandOptions = { event: 'guildMemberUnsubscribed' };

@ApplyOptions<Listener.Options>(commandOptions)
export class ListenerExt extends Listener {
public async run(guildMember: GuildMember) {
// Remove all perks from the guild member.
}
}
My issue is that with custom named events, Sapphire doesn't seem to register all of them. For example, I'll have guildMemberDemoted, guildMemberPromoted, guildMemberSubscribed, and guildMemberUnsubscribed. guildMemberDemoted will fire off, but guildMemberPromoted does not. If I check the client for a guildMemberPromoted listener, none exists. I thought maybe I'd have to increase the max listeners for the client, but that also doesn't work. Any ideas?
Solution:
https://github.com/skyra-project/skyra/blob/main/src/listeners/guilds/members/notMutedMemberAddInitialRole.ts https://github.com/skyra-project/skyra/blob/main/src/listeners/guilds/members/guildMemberAdd.ts#L15 Custom events should work just fine. Make sure your emits are actually called and you do not have duplicate file names. By default the name of the file is used as the piece name, and the piece name has to be unique because we use a Map which has unique keys https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map...
Jump to solution
3 Replies
Solution
Favna
Favna13mo ago
https://github.com/skyra-project/skyra/blob/main/src/listeners/guilds/members/notMutedMemberAddInitialRole.ts https://github.com/skyra-project/skyra/blob/main/src/listeners/guilds/members/guildMemberAdd.ts#L15 Custom events should work just fine. Make sure your emits are actually called and you do not have duplicate file names. By default the name of the file is used as the piece name, and the piece name has to be unique because we use a Map which has unique keys https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map
Stev
Stev13mo ago
By default the name of the file is used as the piece name, and the piece name
Oh! I didn't realize this! Despite them being in different directories, this is probably what's happening! I'll check and let you know! That was it. Thank you, @Favna!
Favna
Favna13mo ago
FYI you can also add name: 'whatever' to those options if you dont want to change file names
Want results from more Discord servers?
Add your server
More Posts