types: add gateway opcodes enum (#711)

* bring opcodes enum back

* Update src/types/gateway/opcodes.ts

Co-authored-by: ayntee <ayyantee@gmail.com>

* DiscordGatewayOpcodes

Co-authored-by: ayntee <ayyantee@gmail.com>
This commit is contained in:
ITOH
2021-03-29 18:58:33 +02:00
committed by GitHub
parent 367a7975f8
commit 2914874d19
5 changed files with 29 additions and 11 deletions
+14
View File
@@ -0,0 +1,14 @@
/** https://discord.com/developers/docs/topics/opcodes-and-status-codes#gateway-gateway-opcodes */
export enum DiscordGatewayOpcodes {
Dispatch,
Heartbeat,
Identify,
StatusUpdate,
VoiceStateUpdate,
Resume = 6,
Reconnect,
RequestGuildMembers,
InvalidSession,
Hello,
HeartbeatACK,
}
+1
View File
@@ -26,6 +26,7 @@ export * from "./emojis/create_guild_emoji.ts";
export * from "./emojis/emoji.ts"; export * from "./emojis/emoji.ts";
export * from "./emojis/modify_guild_emoji.ts"; export * from "./emojis/modify_guild_emoji.ts";
export * from "./gateway.ts"; export * from "./gateway.ts";
export * from "./gateway/opcodes.ts";
export * from "./invites/get_invite.ts"; export * from "./invites/get_invite.ts";
export * from "./invites/invite.ts"; export * from "./invites/invite.ts";
export * from "./invites/invite_metadata.ts"; export * from "./invites/invite_metadata.ts";
+2 -1
View File
@@ -1,4 +1,5 @@
import { encode } from "../../deps.ts"; import { encode } from "../../deps.ts";
import { DiscordGatewayOpcodes } from "../types/mod.ts";
import { basicShards, sendWS } from "../ws/shard.ts"; import { basicShards, sendWS } from "../ws/shard.ts";
import { SLASH_COMMANDS_NAME_REGEX } from "./constants.ts"; import { SLASH_COMMANDS_NAME_REGEX } from "./constants.ts";
@@ -11,7 +12,7 @@ export function editBotStatus(
) { ) {
basicShards.forEach((shard) => { basicShards.forEach((shard) => {
sendWS({ sendWS({
op: GatewayOpcode.StatusUpdate, op: DiscordGatewayOpcodes.StatusUpdate,
d: { d: {
since: null, since: null,
afk: false, afk: false,
+9 -8
View File
@@ -5,6 +5,7 @@ import {
DiscordHello, DiscordHello,
DiscordIdentify, DiscordIdentify,
} from "../types/gateway.ts"; } from "../types/gateway.ts";
import { DiscordGatewayOpcodes } from "../types/mod.ts";
import { Collection } from "../util/collection.ts"; import { Collection } from "../util/collection.ts";
import { delay } from "../util/utils.ts"; import { delay } from "../util/utils.ts";
import { decompressWith } from "./deps.ts"; import { decompressWith } from "./deps.ts";
@@ -72,7 +73,7 @@ export function createShard(
const messageData = JSON.parse(message); const messageData = JSON.parse(message);
if (!messageData.t) eventHandlers.rawGateway?.(messageData); if (!messageData.t) eventHandlers.rawGateway?.(messageData);
switch (messageData.op) { switch (messageData.op) {
case GatewayOpcode.Hello: case DiscordGatewayOpcodes.Hello:
if (!heartbeating.has(basicShard.id)) { if (!heartbeating.has(basicShard.id)) {
await heartbeat( await heartbeat(
basicShard, basicShard,
@@ -82,17 +83,17 @@ export function createShard(
); );
} }
break; break;
case GatewayOpcode.HeartbeatACK: case DiscordGatewayOpcodes.HeartbeatACK:
heartbeating.set(shardID, true); heartbeating.set(shardID, true);
break; break;
case GatewayOpcode.Reconnect: case DiscordGatewayOpcodes.Reconnect:
eventHandlers.debug?.( eventHandlers.debug?.(
{ type: "gatewayReconnect", data: { shardID: basicShard.id } }, { type: "gatewayReconnect", data: { shardID: basicShard.id } },
); );
basicShard.needToResume = true; basicShard.needToResume = true;
await resumeConnection(data, identifyPayload, basicShard.id); await resumeConnection(data, identifyPayload, basicShard.id);
break; break;
case GatewayOpcode.InvalidSession: case DiscordGatewayOpcodes.InvalidSession:
eventHandlers.debug?.( eventHandlers.debug?.(
{ {
type: "gatewayInvalidSession", type: "gatewayInvalidSession",
@@ -166,14 +167,14 @@ function identify(shard: BasicShard, payload: DiscordIdentify) {
); );
sendWS({ sendWS({
op: GatewayOpcode.Identify, op: DiscordGatewayOpcodes.Identify,
d: { ...payload, shard: [shard.id, payload.shard[1]] }, d: { ...payload, shard: [shard.id, payload.shard[1]] },
}, shard.id); }, shard.id);
} }
function resume(shard: BasicShard, payload: DiscordIdentify) { function resume(shard: BasicShard, payload: DiscordIdentify) {
sendWS({ sendWS({
op: GatewayOpcode.Resume, op: DiscordGatewayOpcodes.Resume,
d: { d: {
token: payload.token, token: payload.token,
session_id: shard.sessionID, session_id: shard.sessionID,
@@ -219,7 +220,7 @@ async function heartbeat(
heartbeating.set(shard.id, false); heartbeating.set(shard.id, false);
sendWS( sendWS(
{ op: GatewayOpcode.Heartbeat, d: shard.previousSequenceNumber }, { op: DiscordGatewayOpcodes.Heartbeat, d: shard.previousSequenceNumber },
shard.id, shard.id,
); );
eventHandlers.debug?.( eventHandlers.debug?.(
@@ -291,7 +292,7 @@ export async function requestGuildMembers(
} }
sendWS({ sendWS({
op: GatewayOpcode.RequestGuildMembers, op: DiscordGatewayOpcodes.RequestGuildMembers,
d: { d: {
guild_id: guildID, guild_id: guildID,
// If a query is provided use it, OR if a limit is NOT provided use "" // If a query is provided use it, OR if a limit is NOT provided use ""
+3 -2
View File
@@ -2,6 +2,7 @@ import { eventHandlers } from "../bot.ts";
import { cache } from "../cache.ts"; import { cache } from "../cache.ts";
import { handlers } from "../handlers/mod.ts"; import { handlers } from "../handlers/mod.ts";
import { Member } from "../structures/mod.ts"; import { Member } from "../structures/mod.ts";
import { DiscordGatewayOpcodes } from "../types/mod.ts";
import { Collection } from "../util/collection.ts"; import { Collection } from "../util/collection.ts";
import { delay } from "../util/utils.ts"; import { delay } from "../util/utils.ts";
import { createShard, requestGuildMembers } from "./mod.ts"; import { createShard, requestGuildMembers } from "./mod.ts";
@@ -67,10 +68,10 @@ export async function handleDiscordPayload(
await eventHandlers.dispatchRequirements?.(data, shardID); await eventHandlers.dispatchRequirements?.(data, shardID);
switch (data.op) { switch (data.op) {
case GatewayOpcode.HeartbeatACK: case DiscordGatewayOpcodes.HeartbeatACK:
// In case the user wants to listen to heartbeat responses // In case the user wants to listen to heartbeat responses
return eventHandlers.heartbeat?.(); return eventHandlers.heartbeat?.();
case GatewayOpcode.Dispatch: case DiscordGatewayOpcodes.Dispatch:
if (!data.t) return; if (!data.t) return;
// Run the appropriate handler for this event. // Run the appropriate handler for this event.
return handlers[data.t]?.(data, shardID); return handlers[data.t]?.(data, shardID);