mirror of
https://github.com/discordeno/discordeno.git
synced 2026-06-16 11:28:15 +00:00
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:
+24
-18
@@ -7,6 +7,7 @@ import {
|
|||||||
import {
|
import {
|
||||||
DiscordBotGatewayData,
|
DiscordBotGatewayData,
|
||||||
DiscordHeartbeatPayload,
|
DiscordHeartbeatPayload,
|
||||||
|
DiscordPayload,
|
||||||
FetchMembersOptions,
|
FetchMembersOptions,
|
||||||
GatewayOpcode,
|
GatewayOpcode,
|
||||||
ReadyPayload,
|
ReadyPayload,
|
||||||
@@ -182,25 +183,21 @@ function identify(shard: BasicShard, payload: IdentifyPayload) {
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
return shard.ws.send(
|
sendWS({
|
||||||
JSON.stringify(
|
op: GatewayOpcode.Identify,
|
||||||
{
|
d: { ...payload, shard: [shard.id, payload.shard[1]] },
|
||||||
op: GatewayOpcode.Identify,
|
}, shard.id);
|
||||||
d: { ...payload, shard: [shard.id, payload.shard[1]] },
|
|
||||||
},
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function resume(shard: BasicShard, payload: IdentifyPayload) {
|
function resume(shard: BasicShard, payload: IdentifyPayload) {
|
||||||
return shard.ws.send(JSON.stringify({
|
sendWS({
|
||||||
op: GatewayOpcode.Resume,
|
op: GatewayOpcode.Resume,
|
||||||
d: {
|
d: {
|
||||||
token: payload.token,
|
token: payload.token,
|
||||||
session_id: shard.sessionID,
|
session_id: shard.sessionID,
|
||||||
seq: shard.previousSequenceNumber,
|
seq: shard.previousSequenceNumber,
|
||||||
},
|
},
|
||||||
}));
|
}, shard.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function heartbeat(
|
async function heartbeat(
|
||||||
@@ -238,10 +235,9 @@ async function heartbeat(
|
|||||||
// Set it to false as we are issuing a new heartbeat
|
// Set it to false as we are issuing a new heartbeat
|
||||||
heartbeating.set(shard.id, false);
|
heartbeating.set(shard.id, false);
|
||||||
|
|
||||||
shard.ws.send(
|
sendWS(
|
||||||
JSON.stringify(
|
{ op: GatewayOpcode.Heartbeat, d: shard.previousSequenceNumber },
|
||||||
{ op: GatewayOpcode.Heartbeat, d: shard.previousSequenceNumber },
|
shard.id,
|
||||||
),
|
|
||||||
);
|
);
|
||||||
eventHandlers.debug?.(
|
eventHandlers.debug?.(
|
||||||
{
|
{
|
||||||
@@ -311,7 +307,7 @@ export function requestGuildMembers(
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
shard?.ws.send(JSON.stringify({
|
sendWS({
|
||||||
op: GatewayOpcode.RequestGuildMembers,
|
op: GatewayOpcode.RequestGuildMembers,
|
||||||
d: {
|
d: {
|
||||||
guild_id: guildID,
|
guild_id: guildID,
|
||||||
@@ -322,7 +318,7 @@ export function requestGuildMembers(
|
|||||||
user_ids: options?.userIDs,
|
user_ids: options?.userIDs,
|
||||||
nonce,
|
nonce,
|
||||||
},
|
},
|
||||||
}));
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async function processGatewayQueue() {
|
async function processGatewayQueue() {
|
||||||
@@ -389,7 +385,7 @@ async function processGatewayQueue() {
|
|||||||
|
|
||||||
export function botGatewayStatusRequest(payload: BotStatusRequest) {
|
export function botGatewayStatusRequest(payload: BotStatusRequest) {
|
||||||
basicShards.forEach((shard) => {
|
basicShards.forEach((shard) => {
|
||||||
shard.ws.send(JSON.stringify({
|
sendWS({
|
||||||
op: GatewayOpcode.StatusUpdate,
|
op: GatewayOpcode.StatusUpdate,
|
||||||
d: {
|
d: {
|
||||||
since: null,
|
since: null,
|
||||||
@@ -402,6 +398,16 @@ export function botGatewayStatusRequest(payload: BotStatusRequest) {
|
|||||||
status: payload.status,
|
status: payload.status,
|
||||||
afk: false,
|
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;
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user