From 3fdc6a54aa57338474c8f638d26fddd2c367d4d9 Mon Sep 17 00:00:00 2001 From: Rory& Date: Fri, 13 Mar 2026 03:45:26 +0100 Subject: [PATCH] Gateway: add SB_SESSION_CLOSE and SB_SESSION_INVALIDATE events --- src/gateway/listener/listener.ts | 40 ++++++++++++++++++++++++-------- src/gateway/util/Constants.ts | 2 +- src/util/interfaces/Event.ts | 3 ++- 3 files changed, 33 insertions(+), 12 deletions(-) diff --git a/src/gateway/listener/listener.ts b/src/gateway/listener/listener.ts index 96eac986f..90f7e4b17 100644 --- a/src/gateway/listener/listener.ts +++ b/src/gateway/listener/listener.ts @@ -17,25 +17,24 @@ */ import { - getPermission, - Permissions, - RabbitMQ, - listenEvent, + Ban, + EVENTEnum, EventOpts, + getPermission, + listenEvent, ListenEventOpts, Member, - EVENTEnum, - Relationship, Message, NewUrlUserSignatureData, - GuildMemberAddEvent, - Ban, + Permissions, + RabbitMQ, + Recipient, + Relationship, } from "@spacebar/util"; -import { OPCODES } from "../util/Constants"; +import { CLOSECODES, OPCODES } from "../util/Constants"; import { Send } from "../util/Send"; import { WebSocket } from "@spacebar/gateway"; import { Channel as AMQChannel } from "amqplib"; -import { Recipient } from "@spacebar/util"; import * as console from "node:console"; import { PublicMember, RelationshipType } from "@spacebar/schemas"; import { bgRedBright } from "picocolors"; @@ -208,6 +207,27 @@ async function consume(this: WebSocket, opts: EventOpts) { opts.acknowledge?.(); // console.log("event", event); + // special codes + switch (event) { + case "SB_SESSION_CLOSE": + // TODO: what do we even send here? + await Send(this, { + op: OPCODES.Reconnect, + s: this.sequence++, + d: opts.reconnect_delay ?? opts.data ?? 1000, + }); + this.close(1000); // not a discord close code, standard WS "Normal Closure" + return; + case "SB_SESSION_REMOVE": + // TODO: what do we even send here? + await Send(this, { + op: OPCODES.Invalid_Session, + s: this.sequence++, + }); + this.close(CLOSECODES.Invalid_session); // TODO: this is deprecated? + return; + } + // subscription managment switch (event) { case "GUILD_MEMBER_REMOVE": diff --git a/src/gateway/util/Constants.ts b/src/gateway/util/Constants.ts index af5fa1a58..3e4599c65 100644 --- a/src/gateway/util/Constants.ts +++ b/src/gateway/util/Constants.ts @@ -42,7 +42,7 @@ export enum OPCODES { Stream_Set_Paused = 22, Request_Application_Commands = 24, // We don't know the names for these: - ThirtySix = 36, // this is just a guild id? + Request_Channel_Statuses = 36, // this is just a guild id? Guild_Subscriptions_Bulk = 37, // Already implemented it seems? SetQoS = 40, ClientInitSession = 41, diff --git a/src/util/interfaces/Event.ts b/src/util/interfaces/Event.ts index 6971a61a1..28ae0b54f 100644 --- a/src/util/interfaces/Event.ts +++ b/src/util/interfaces/Event.ts @@ -60,6 +60,7 @@ export interface Event { event: EVENT; // eslint-disable-next-line @typescript-eslint/no-explicit-any data?: any; + reconnect_delay?: number; origin?: string; } @@ -848,4 +849,4 @@ export type EVENT = | "THREAD_MEMBERS_UPDATE" | CUSTOMEVENTS; -export type CUSTOMEVENTS = "INVALIDATED" | "RATELIMIT"; +export type CUSTOMEVENTS = "INVALIDATED" | "RATELIMIT" | "SB_SESSION_REMOVE" | "SB_SESSION_CLOSE";