Skip to content

Commit 71c2a8a

Browse files
committed
handle unacked guilds when guilds become available
1 parent e1ed529 commit 71c2a8a

1 file changed

Lines changed: 18 additions & 1 deletion

File tree

src/api/ApiCache.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ export default class ApiCache {
6565
dmMentions: ReactiveMap<bigint, bigint[]>
6666
customEmojis: ReactiveMap<bigint, CustomEmoji>
6767
guildLoadingStates: ReactiveMap<bigint, boolean>
68+
pendingUnacked: Map<bigint, { last_message_id: bigint | null, mentions: bigint[] }>
6869

6970
constructor(private readonly api: Api) {
7071
this.clientUserReactor = null as any // lazy
@@ -90,6 +91,7 @@ export default class ApiCache {
9091
this.dmMentions = new ReactiveMap()
9192
this.customEmojis = new ReactiveMap()
9293
this.guildLoadingStates = new ReactiveMap()
94+
this.pendingUnacked = new Map()
9395
}
9496

9597
static fromReadyEvent(api: Api, ready: ReadyEvent): ApiCache {
@@ -120,7 +122,11 @@ export default class ApiCache {
120122
for (let { channel_id, last_message_id, mentions } of ready.unacked) {
121123
cache.lastAckedMessages.set(channel_id, last_message_id)
122124
let channel = cache.channels.get(channel_id)
123-
if (!channel) continue
125+
if (!channel) {
126+
// channel not yet loaded (guild channels arrive via guilds_available later)
127+
cache.pendingUnacked.set(channel_id, { last_message_id, mentions })
128+
continue
129+
}
124130
if ('guild_id' in channel) {
125131
const guildId = BigInt(channel.guild_id)
126132
if (!cache.guildMentions.has(guildId))
@@ -169,6 +175,17 @@ export default class ApiCache {
169175
this.updateChannel(channel)
170176

171177
this.guildChannelReactor.set(guild.id, guild.channels.map(channel => channel.id))
178+
179+
for (const channel of guild.channels) {
180+
const pending = this.pendingUnacked.get(channel.id)
181+
if (!pending) continue
182+
this.pendingUnacked.delete(channel.id)
183+
184+
if (!this.guildMentions.has(guild.id))
185+
this.guildMentions.set(guild.id, new ReactiveMap())
186+
187+
this.guildMentions.get(guild.id)!.set(channel.id, pending.mentions)
188+
}
172189
}
173190

174191
if (guild.roles)

0 commit comments

Comments
 (0)