fetch all message from announcement channel on start

This is necessary because message sent before the bot has started up are not cached and reactions will not be registered.
If the messages are cached manually the reactions will be received and can be processed using the regular event handling
This commit is contained in:
kenobi 2023-06-25 22:48:55 +02:00
parent f6476c609b
commit 331ff89060
1 changed files with 7 additions and 1 deletions

View File

@ -23,7 +23,7 @@ export class ExtendedClient extends Client {
private pollCloseBackgroundTasks: Collection<string, ScheduledTask> = new Collection()
public constructor(jf: JellyfinHandler) {
const intents: IntentsBitField = new IntentsBitField()
intents.add(IntentsBitField.Flags.GuildMembers, IntentsBitField.Flags.MessageContent, IntentsBitField.Flags.Guilds, IntentsBitField.Flags.DirectMessages, IntentsBitField.Flags.GuildScheduledEvents, IntentsBitField.Flags.GuildVoiceStates)
intents.add(IntentsBitField.Flags.GuildMembers, IntentsBitField.Flags.MessageContent, IntentsBitField.Flags.Guilds, IntentsBitField.Flags.DirectMessages, IntentsBitField.Flags.GuildScheduledEvents, IntentsBitField.Flags.GuildMessageReactions, IntentsBitField.Flags.GuildVoiceStates)
const options: ClientOptions = { intents }
super(options)
this.jellyfin = jf
@ -75,6 +75,7 @@ export class ExtendedClient extends Client {
this.registerCommands(slashCommands, guilds)
this.cacheUsers(guilds)
await this.cacheAnnouncementServer(guilds)
this.fetchAnnouncementChannelMessage(this.announcementChannels)
this.startAnnouncementRoleBackgroundTask(guilds)
this.startPollCloseBackgroundTasks()
})
@ -82,6 +83,11 @@ export class ExtendedClient extends Client {
logger.info(`Error refreshing slash commands: ${error}`)
}
}
private async fetchAnnouncementChannelMessage(channels: Collection<string, TextChannel>): Promise<void> {
channels.each(async ch => {
ch.messages.fetch()
})
}
private async cacheAnnouncementServer(guilds: Collection<Snowflake, Guild>) {
for (const guild of guilds.values()) {
const channels: TextChannel[] = <TextChannel[]>(await guild.channels.fetch())