Gateway
Gateway is Lunibee’s low-level Discord WebSocket client. It is automatically managed by Client. You can also use it directly for advanced use cases like custom event routing.
Import
Section titled “Import”import { Gateway } from "@lunibee/ws";// or via the main package:import { Client } from "lunibee"; // access as client.gateway / client.wsimport { Gateway } from "@lunibee/ws";// or via the main package:import { Client } from "lunibee"; // access as client.gateway / client.wsConstructor
Section titled “Constructor”new Gateway(options: GatewayOptions)new Gateway(options )| Option | Type | Description |
|---|---|---|
token |
string |
Discord bot token. |
intents |
GatewayIntentResolvable |
Gateway intents. |
presence |
GatewayPresence |
Initial presence/status. |
properties |
GatewayProperties |
OS/browser/device identification. |
compress |
boolean |
Enable WebSocket compression. |
largeThreshold |
number |
Guild member threshold for large guilds (50–250). |
Properties
Section titled “Properties”| Property | Type | Description |
|---|---|---|
ping |
number |
Latest heartbeat round-trip latency in ms. |
state |
GatewayState |
Current connection state. |
sessionId |
string | undefined |
Active session ID. |
resumeURL |
string | undefined |
Discord’s recommended resume URL. |
sequence |
number | null |
Latest event sequence number. |
Methods
Section titled “Methods”connect()
Section titled “connect()”Establishes the WebSocket connection and identifies with Discord.
await gateway.connect();await gateway.connect();close()
Section titled “close()”Closes the WebSocket connection cleanly.
gateway.close();gateway.close();setPresence(data)
Section titled “setPresence(data)”Sends an opcode 3 Presence Update.
gateway.setPresence({ status: "dnd", activities: [{ name: "Maintenance Mode", type: 0 }],});gateway.setPresence({ status: "dnd", activities: [{ name: "Maintenance Mode", type: 0 }],});setVoiceState(data)
Section titled “setVoiceState(data)”Sends an opcode 4 Voice State Update.
gateway.setVoiceState({ guild_id: guildId, channel_id: channelId, self_mute: false, self_deaf: false });gateway.setVoiceState({ guild_id , channel_id , self_mute , self_deaf: false });requestGuildMembers(data)
Section titled “requestGuildMembers(data)”Sends an opcode 8 Request Guild Members.
gateway.requestGuildMembers({ guild_id: guildId, query: "", limit: 0 });gateway.requestGuildMembers({ guild_id , query: "", limit: 0 });on(event, listener) / off(event, listener)
Section titled “on(event, listener) / off(event, listener)”Subscribes to raw Gateway dispatch events:
gateway.on("MESSAGE_CREATE", (data) => { console.log("Raw message:", data);});
gateway.on("READY", (data) => { console.log("Session:", data.session_id);});gateway.on("MESSAGE_CREATE", (data) => { console.log("Raw message:", data);});
gateway.on("READY", (data) => { console.log("Session:", data.session_id);});GatewayState
Section titled “GatewayState”| Value | Description |
|---|---|
GatewayState.Connect |
Initial connection attempt. |
GatewayState.Hello |
HELLO received, heartbeating. |
GatewayState.Identify |
IDENTIFY in progress. |
GatewayState.Resume |
RESUME in progress. |
GatewayState.Ready |
Fully connected. |
GatewayState.Reconnect |
Reconnecting after disconnect. |
GatewayState.Closed |
Permanently closed. |
Direct Usage Example
Section titled “Direct Usage Example”import { Gateway } from "@lunibee/ws";import { IntentBits } from "lunibee";
const gateway = new Gateway({ token: process.env.DISCORD_TOKEN!, intents: [IntentBits.guilds, IntentBits.guildMessages],});
gateway.on("READY", (data) => { console.log(`Connected as ${(data as any).user.username}`);});
gateway.on("MESSAGE_CREATE", (data) => { console.log("Message received:", (data as any).content);});
await gateway.connect();import { Gateway } from "@lunibee/ws";import { IntentBits } from "lunibee";
const gateway = new Gateway({ token: process.env.DISCORD_TOKEN, intents: [IntentBits.guilds, IntentBits.guildMessages],});
gateway.on("READY", (data) => { console.log(`Connected as ${(data as any).user.username}`);});
gateway.on("MESSAGE_CREATE", (data) => { console.log("Message received:", (data as any).content);});
await gateway.connect();[!TIP] Most bots should use the higher-level
Clientclass instead ofGatewaydirectly.Clienthandles structure creation, caching, and event dispatch automatically.