Skip to content

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 { Gateway } from "@lunibee/ws";
// or via the main package:
import { Client } from "lunibee"; // access as client.gateway / client.ws
new Gateway(options: GatewayOptions)
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).
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.

Establishes the WebSocket connection and identifies with Discord.

await gateway.connect();

Closes the WebSocket connection cleanly.

gateway.close();

Sends an opcode 3 Presence Update.

gateway.setPresence({
status: "dnd",
activities: [{ name: "Maintenance Mode", type: 0 }],
});

Sends an opcode 4 Voice State Update.

gateway.setVoiceState({ guild_id: guildId, channel_id: channelId, self_mute: false, self_deaf: false });

Sends an opcode 8 Request Guild Members.

gateway.requestGuildMembers({ guild_id: guildId, 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);
});
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.
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 Client class instead of Gateway directly. Client handles structure creation, caching, and event dispatch automatically.