mirror of
https://github.com/discordeno/discordeno.git
synced 2026-09-17 08:47:22 +00:00
refactor(gateway)!: Remove guildMemberChunk shard event (#3151)
* Remove guildMemberChunk shard event * use DiscordMemberWithUser for requestMembers
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/* eslint-disable @typescript-eslint/no-confusing-void-expression */
|
||||
import type { DiscordGatewayPayload, DiscordGuildMembersChunk, DiscordHello, DiscordReady } from '@discordeno/types'
|
||||
import type { DiscordGatewayPayload, DiscordHello, DiscordReady } from '@discordeno/types'
|
||||
import { GatewayCloseEventCodes, GatewayOpcodes } from '@discordeno/types'
|
||||
import { camelize, delay, LeakyBucket, logger } from '@discordeno/utils'
|
||||
import { inflateSync } from 'node:zlib'
|
||||
@@ -448,9 +448,6 @@ export class DiscordenoShard {
|
||||
this.resolves.delete('READY')
|
||||
break
|
||||
}
|
||||
case 'GUILD_MEMBERS_CHUNK': {
|
||||
this.events.guildMemberChunk?.(packet.d as DiscordGuildMembersChunk)
|
||||
}
|
||||
}
|
||||
|
||||
// Update the sequence number if it is present
|
||||
|
||||
@@ -6,11 +6,10 @@ import {
|
||||
type BigString,
|
||||
type Camelize,
|
||||
type DiscordGetGatewayBot,
|
||||
type DiscordMember,
|
||||
type DiscordMemberWithUser,
|
||||
type RequestGuildMembers,
|
||||
} from '@discordeno/types'
|
||||
import { camelize, Collection, delay, logger } from '@discordeno/utils'
|
||||
import { Collection, delay, logger } from '@discordeno/utils'
|
||||
import Shard from './Shard.js'
|
||||
import type { ShardEvents, ShardSocketRequest, StatusUpdate, UpdateVoiceState } from './types.js'
|
||||
|
||||
@@ -26,29 +25,6 @@ export function createGatewayManager(options: CreateGatewayManagerOptions): Gate
|
||||
},
|
||||
}
|
||||
|
||||
options.events.guildMemberChunk ??= (payload) => {
|
||||
// If it's not enabled skip checks.
|
||||
if (!gateway.cache.requestMembers?.enabled) return
|
||||
|
||||
// If this request has no nonce, skip checks.
|
||||
if (!payload.nonce) return
|
||||
|
||||
const pending = 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.
|
||||
gateway.cache.requestMembers.pending.delete(payload.nonce)
|
||||
}
|
||||
|
||||
const gateway: GatewayManager = {
|
||||
events: options.events,
|
||||
compress: options.compress ?? false,
|
||||
@@ -320,7 +296,7 @@ export function createGatewayManager(options: CreateGatewayManagerOptions): Gate
|
||||
const members =
|
||||
!gateway.cache.requestMembers?.enabled || !options?.nonce
|
||||
? []
|
||||
: new Promise<Camelize<DiscordMember[]>>((resolve, reject) => {
|
||||
: new Promise<Camelize<DiscordMemberWithUser[]>>((resolve, reject) => {
|
||||
// Should never happen.
|
||||
if (!gateway.cache.requestMembers?.enabled || !options?.nonce) {
|
||||
reject(new Error("Can't request the members without the nonce or with the feature disabled."))
|
||||
@@ -551,7 +527,7 @@ export interface GatewayManager extends Required<CreateGatewayManagerOptions> {
|
||||
*
|
||||
* @see {@link https://discord.com/developers/docs/topics/gateway#request-guild-members}
|
||||
*/
|
||||
requestMembers: (guildId: BigString, options?: Omit<RequestGuildMembers, 'guildId'>) => Promise<Camelize<DiscordMember[]>>
|
||||
requestMembers: (guildId: BigString, options?: Omit<RequestGuildMembers, 'guildId'>) => Promise<Camelize<DiscordMemberWithUser[]>>
|
||||
/**
|
||||
* Leaves the voice channel the bot user is currently in.
|
||||
*
|
||||
@@ -571,7 +547,7 @@ export interface RequestMemberRequest {
|
||||
/** The unique nonce for this request. */
|
||||
nonce: string
|
||||
/** The resolver handler to run when all members arrive. */
|
||||
resolve: (value: Camelize<DiscordMember[]> | PromiseLike<Camelize<DiscordMember[]>>) => void
|
||||
resolve: (value: Camelize<DiscordMemberWithUser[]> | PromiseLike<Camelize<DiscordMemberWithUser[]>>) => void
|
||||
/** The members that have already arrived for this request. */
|
||||
members: DiscordMemberWithUser[]
|
||||
}
|
||||
|
||||
@@ -1,12 +1,4 @@
|
||||
import type {
|
||||
ActivityTypes,
|
||||
Camelize,
|
||||
DiscordActivity,
|
||||
DiscordGatewayPayload,
|
||||
DiscordGuildMembersChunk,
|
||||
GatewayOpcodes,
|
||||
PresenceStatus,
|
||||
} from '@discordeno/types'
|
||||
import type { ActivityTypes, Camelize, DiscordActivity, DiscordGatewayPayload, GatewayOpcodes, PresenceStatus } from '@discordeno/types'
|
||||
import type Shard from './Shard.js'
|
||||
|
||||
export enum ShardState {
|
||||
@@ -121,8 +113,6 @@ export interface ShardEvents {
|
||||
identified?: (shard: Shard) => unknown
|
||||
/** The shard has received a message from Discord. */
|
||||
message?: (shard: Shard, payload: Camelize<DiscordGatewayPayload>) => unknown
|
||||
/* The shard has received a GUILD_MEMBER_CHUNK from Discord and should be handled accordingly */
|
||||
guildMemberChunk?: (payload: DiscordGuildMembersChunk) => unknown
|
||||
}
|
||||
|
||||
export enum ShardSocketCloseCodes {
|
||||
|
||||
Reference in New Issue
Block a user