feat(ws/shard): add sendWS() (#514)

* feat(ws/shard): add sendWS()

Closes #513

* Switch the order of the args

* Remove additional info

* Use sendWS()
This commit is contained in:
ayntee
2021-02-10 18:55:25 +04:00
committed by GitHub
parent 043df68de8
commit 93b7e57293
+21 -15
View File
@@ -7,6 +7,7 @@ import {
import {
DiscordBotGatewayData,
DiscordHeartbeatPayload,
DiscordPayload,
FetchMembersOptions,
GatewayOpcode,
ReadyPayload,
@@ -182,25 +183,21 @@ function identify(shard: BasicShard, payload: IdentifyPayload) {
},
);
return shard.ws.send(
JSON.stringify(
{
sendWS({
op: GatewayOpcode.Identify,
d: { ...payload, shard: [shard.id, payload.shard[1]] },
},
),
);
}, shard.id);
}
function resume(shard: BasicShard, payload: IdentifyPayload) {
return shard.ws.send(JSON.stringify({
sendWS({
op: GatewayOpcode.Resume,
d: {
token: payload.token,
session_id: shard.sessionID,
seq: shard.previousSequenceNumber,
},
}));
}, shard.id);
}
async function heartbeat(
@@ -238,10 +235,9 @@ async function heartbeat(
// Set it to false as we are issuing a new heartbeat
heartbeating.set(shard.id, false);
shard.ws.send(
JSON.stringify(
sendWS(
{ op: GatewayOpcode.Heartbeat, d: shard.previousSequenceNumber },
),
shard.id,
);
eventHandlers.debug?.(
{
@@ -311,7 +307,7 @@ export function requestGuildMembers(
return;
}
shard?.ws.send(JSON.stringify({
sendWS({
op: GatewayOpcode.RequestGuildMembers,
d: {
guild_id: guildID,
@@ -322,7 +318,7 @@ export function requestGuildMembers(
user_ids: options?.userIDs,
nonce,
},
}));
});
}
async function processGatewayQueue() {
@@ -389,7 +385,7 @@ async function processGatewayQueue() {
export function botGatewayStatusRequest(payload: BotStatusRequest) {
basicShards.forEach((shard) => {
shard.ws.send(JSON.stringify({
sendWS({
op: GatewayOpcode.StatusUpdate,
d: {
since: null,
@@ -402,6 +398,16 @@ export function botGatewayStatusRequest(payload: BotStatusRequest) {
status: payload.status,
afk: false,
},
}));
});
});
}
/** Enqueues the specified data to be transmitted to the server over the WebSocket connection, */
export function sendWS(payload: DiscordPayload, shardID = 0) {
const shard = basicShards.get(shardID);
if (!shard) return false;
const serialized = JSON.stringify(payload);
shard.ws.send(serialized);
return true;
}