refactor(gateway)!: Remove guildMemberChunk shard event (#3151)

* Remove guildMemberChunk shard event

* use DiscordMemberWithUser for requestMembers
This commit is contained in:
Fleny
2023-10-18 22:21:46 +00:00
committed by GitHub
parent 895b482a02
commit c0615a2983
4 changed files with 28 additions and 65 deletions
@@ -1,29 +1,29 @@
import type { DiscordGatewayPayload, DiscordGuildMembersChunk } from '@discordeno/types'
import { PresenceStatus } from '@discordeno/types'
import { camelize } from '@discordeno/utils'
import type { Bot } from '../../index.js'
export async function handleGuildMembersChunk(bot: Bot, data: DiscordGatewayPayload): Promise<any> {
export async function handleGuildMembersChunk(bot: Bot, data: DiscordGatewayPayload): Promise<void> {
const payload = data.d as DiscordGuildMembersChunk
const guildId = bot.transformers.snowflake(payload.guild_id)
// If it's not enabled skip checks.
if (!bot.gateway.cache.requestMembers?.enabled) return
return {
guildId,
members: payload.members.map((m) => bot.transformers.member(bot, m, guildId, bot.transformers.snowflake(m.user.id))),
chunkIndex: payload.chunk_index,
chunkCount: payload.chunk_count,
notFound: payload.not_found?.map((id) => bot.transformers.snowflake(id)),
presences: payload.presences?.map((presence) => ({
user: bot.transformers.user(bot, presence.user),
guildId,
status: PresenceStatus[presence.status],
activities: presence.activities.map((activity) => bot.transformers.activity(bot, activity)),
clientStatus: {
desktop: presence.client_status.desktop,
mobile: presence.client_status.mobile,
web: presence.client_status.web,
},
})),
nonce: payload.nonce,
}
// If this request has no nonce, skip checks.
if (!payload.nonce) return
const pending = bot.gateway.cache.requestMembers.pending.get(payload.nonce)
if (!pending) return
if (payload.chunk_count === 1) pending.members = payload.members
else pending.members.push(...payload.members)
// If this is not the final chunk, just save to cache.
if (payload.chunk_index + 1 < payload.chunk_count) return
// Resolve the promise that all requests are done.
pending.resolve(camelize(pending.members))
// Delete the cache to clean up once its done.
bot.gateway.cache.requestMembers.pending.delete(payload.nonce)
}