Listener with `once: true` appears to be triggering multiple times

import { ApplyOptions } from '@sapphire/decorators';
import { Events, Listener } from '@sapphire/framework';
import { ChannelType } from 'discord.js';
import { DBMessageLogs } from '../lib/database/DBMessageLogs';

@ApplyOptions<Listener.Options>({ once: true, event: Events.ClientReady })
export class ReadyMessageLogsDBListener extends Listener {
public override async run() {
const { client, database } = this.container;
const guilds = client.guilds.cache.map(g => g.id);

guilds.forEach(guild_id => {
const guild = client.guilds.cache.get(guild_id);
const channelsCollection = guild?.channels.cache;

if (!channelsCollection) {
return;
}

const channels = [...channelsCollection.values()]
.filter(channel => channel.type === ChannelType.GuildText);

let logDB = new DBMessageLogs(database);
channels.forEach(async (channel) => {
console.log(`ADDING CHANNEL NAME ${channel.name} with ID ${channel.id}`);
await logDB.channelAdd(BigInt(channel.id), channel.name, BigInt(guild.id));
});
})
}
}
import { ApplyOptions } from '@sapphire/decorators';
import { Events, Listener } from '@sapphire/framework';
import { ChannelType } from 'discord.js';
import { DBMessageLogs } from '../lib/database/DBMessageLogs';

@ApplyOptions<Listener.Options>({ once: true, event: Events.ClientReady })
export class ReadyMessageLogsDBListener extends Listener {
public override async run() {
const { client, database } = this.container;
const guilds = client.guilds.cache.map(g => g.id);

guilds.forEach(guild_id => {
const guild = client.guilds.cache.get(guild_id);
const channelsCollection = guild?.channels.cache;

if (!channelsCollection) {
return;
}

const channels = [...channelsCollection.values()]
.filter(channel => channel.type === ChannelType.GuildText);

let logDB = new DBMessageLogs(database);
channels.forEach(async (channel) => {
console.log(`ADDING CHANNEL NAME ${channel.name} with ID ${channel.id}`);
await logDB.channelAdd(BigInt(channel.id), channel.name, BigInt(guild.id));
});
})
}
}
29 Replies
abe
abeOP•16mo ago
I have this listener which adds all the text channels in a discord server to a table in a database. It appears to be triggering twice with the second time containing the right data from the channel.:
RECEIVED CHANNEL general with ID 1144586568296824800
ADDING CHANNEL NAME general with ID 1144586568296824888
RECEIVED CHANNEL NAME general with ID 1144586568296824888
RECEIVED CHANNEL general with ID 1144586568296824800
ADDING CHANNEL NAME general with ID 1144586568296824888
RECEIVED CHANNEL NAME general with ID 1144586568296824888
This is the code to add:
async channelAdd(channel_id: bigint, channel_name: string, guild_id: bigint) {
try {
console.log(`RECEIVED CHANNEL NAME ${channel_name} with ID ${channel_id}`);
await this.db.$connect();
const channel = await this.db.channels.findFirst({ where: { channel_id } });
if (!channel) {
await this.db.channels.create({
data: {
channel_id,
channel_name,
guild_id
}
});
}
await this.db.$disconnect();
} catch (error) {
throw error;
}
}
async channelAdd(channel_id: bigint, channel_name: string, guild_id: bigint) {
try {
console.log(`RECEIVED CHANNEL NAME ${channel_name} with ID ${channel_id}`);
await this.db.$connect();
const channel = await this.db.channels.findFirst({ where: { channel_id } });
if (!channel) {
await this.db.channels.create({
data: {
channel_id,
channel_name,
guild_id
}
});
}
await this.db.$disconnect();
} catch (error) {
throw error;
}
}
abe
abeOP•16mo ago
it creates two records
No description
abe
abeOP•16mo ago
@Helpers please
vladdy
vladdy•16mo ago
Firstly you are showing two different listeners for stuff
abe
abeOP•16mo ago
wdym two different listeners
vladdy
vladdy•16mo ago
But it also looks like whatever you're doing with ids isn't going smoothly because i see them encountering rounding errors Ready and channel create I'd check your sql stuff instead
abe
abeOP•16mo ago
i have two listener files that have onready
abe
abeOP•16mo ago
this is the structure
No description
vladdy
vladdy•16mo ago
We'll need more info because i don't see how it's related to once Each listener will only run once if one is set to true You have two ready listeners -> each will run only once
abe
abeOP•16mo ago
yep
No description
abe
abeOP•16mo ago
but i end up triggering ReadyMessageLogsDBListener twice cos of that RECEIVED CHANNEL NAME... thing its being output twice
vladdy
vladdy•16mo ago
Two different ids tho
abe
abeOP•16mo ago
you suspect a rounding error?
abe
abeOP•16mo ago
this pair is correct btw
No description
abe
abeOP•16mo ago
that id is an actual channel id in my testing server idk where the one ending in 800 came from
vladdy
vladdy•16mo ago
Are you sure there's not just two channels with very close ids? Maybe log the channel name too
abe
abeOP•16mo ago
i did log the channel name
abe
abeOP•16mo ago
see
No description
abe
abeOP•16mo ago
this is the server im testing on
No description
vladdy
vladdy•16mo ago
Interesting Oh Hmm Why do you get it twice then
abe
abeOP•16mo ago
1144586568296824888 1144586568296824889 these are the IDs of the text and voice channel respectively that's what i also would like to know 😅
vladdy
vladdy•16mo ago
Where else do you call your channel add function Because it seems to be called outside ready Since there's no logs for adding
abe
abeOP•16mo ago
No description
abe
abeOP•16mo ago
only 1 invocation 2nd search result
vladdy
vladdy•16mo ago
rm -rf dist, rebuild And check again
abe
abeOP•16mo ago
omg
No description
abe
abeOP•16mo ago
idk how that fixed it but the ghost id is gone
vladdy
vladdy•16mo ago
Stale code or a previous listener
abe
abeOP•16mo ago
Thank you! much appreciated would've banged my head on this for ages 💀
Want results from more Discord servers?
Add your server