import { GatewayBotInfo , GatewayProvider , GatewaySendOptions , GatewayShardsInfo } from '@discordoo/providers' ; import { Client } from '@src/core' ; import { WebSocketManager } from '@src/gateway/WebSocketManager' ; import { rawToDiscordoo } from '@src/utils/rawToDiscordoo' ; import { CompletedGatewayOptions } from '@src/gateway/interfaces/CompletedGatewayOptions' ; import { GatewaySendPayloadLike } from '@discordoo/providers' ; import { WebSocketClientEvents } from '@src/constants' ; export class DefaultGatewayProvider implements GatewayProvider { public client : Client ; private manager : WebSocketManager ; private options : CompletedGatewayOptions ; constructor ( client , options ) { this . client = client ; this . options = options ; this . manager = new WebSocketManager ( this , options ) ; } connect ( shards ? ) { return this . manager . connect ( shards ) ; } async disconnect ( shards ? ) { return this . manager . disconnect ( shards ) ; } emit ( shardId , event , ... data ) { return this . client . internals . gateway . emit ( shardId , event === WebSocketClientEvents . CONNECTED ? 'shardConnected' : rawToDiscordoo ( event ) , ... data ) ; } getGateway ( ) { return this . client . internals . gateway . getGateway ( ) ; } ping ( ) ; ping ( shards ) ; ping ( shards ? ) { switch ( Array . isArray ( shards ) ) { case true : return shards ! . map ( id => ( [ id , this . manager . shards . get ( id ) ?. ping ?? - 1 ] ) ) ; case false : return ( this . manager . shards . reduce ( ( prev , curr ) => prev + curr . ping , 0 ) / this . manager . shards . size ) | 1 ; } } reorganizeShards ( shards ) { this . manager . destroy ( ) ; return this . manager . connect ( shards ) ; } async reconnect ( shards ? ) { switch ( Array . isArray ( shards ) ) { case true : return shards ! . forEach ( shard => this . manager . shards . get ( shard ) ?. destroy ( { reconnect : true } ) ) ; case false : return this . manager . shards . forEach ( shard => shard . destroy ( { reconnect : true } ) ) ; } } send ( data , options = { } ) { switch ( Array . isArray ( options . shards ) ) { case true : options . shards ! . forEach ( id => this . manager . shards . get ( id ) ?. socketSend ( data ) ) ; break ; case false : this . manager . shards . forEach ( shard => shard . socketSend ( data ) ) ; } return undefined ; } waitShardSpawnTurn ( shardId ) { return this . client . internals . gateway . waitShardSpawnTurn ( shardId ) ; } async init ( ) { return undefined ; } }