feat: gateway.preferSnakeCase

This commit is contained in:
Skillz
2023-03-24 11:18:26 -05:00
parent ae40e69e99
commit eef8887bc3
2 changed files with 17 additions and 1 deletions

View File

@@ -10,7 +10,7 @@ import type {
RequestGuildMembers,
} from '@discordeno/types'
import { GatewayCloseEventCodes, GatewayIntents, GatewayOpcodes } from '@discordeno/types'
import { camelize, Collection, delay, LeakyBucket, logger } from '@discordeno/utils'
import { Collection, LeakyBucket, camelize, delay, logger } from '@discordeno/utils'
import { inflateSync } from 'node:zlib'
import WebSocket from 'ws'
import type { RequestMemberRequest } from './manager.js'
@@ -467,6 +467,10 @@ export class DiscordenoShard {
this.previousSequenceNumber = packet.s
}
this.forwardToBot(packet);
}
forwardToBot(packet: DiscordGatewayPayload): void {
// The necessary handling required for the Shards connection has been finished.
// Now the event can be safely forwarded.
this.events.message?.(this, camelize(packet))

View File

@@ -37,6 +37,7 @@ export function createGatewayManager(options: CreateGatewayManagerOptions): Gate
totalWorkers: options.totalWorkers ?? 4,
shardsPerWorker: options.shardsPerWorker ?? 25,
spawnShardDelay: options.spawnShardDelay ?? 5300,
preferSnakeCase: false,
shards: new Map(),
buckets: new Map(),
cache: {
@@ -167,6 +168,12 @@ export function createGatewayManager(options: CreateGatewayManagerOptions): Gate
},
})
if (this.preferSnakeCase) {
shard.forwardToBot = async (payload) => {
options.events.message?.(shard!, payload)
}
}
this.shards.set(shardId, shard)
}
@@ -289,6 +296,11 @@ export interface CreateGatewayManagerOptions {
* @default 5300
*/
spawnShardDelay?: number
/**
* Whether to send the discord packets in snake case form.
* @default false
*/
preferSnakeCase?: boolean
/**
* Total amount of shards your bot uses. Useful for zero-downtime updates or resharding.
* @default 1