Skip to content

Gateway ​

Config.Gateway.enabled = true opens a WebSocket to Discord. It is the one part of np_discord written in JavaScript (server/gateway.js), because Lua has no WebSocket; it uses the WebSocket built into Node 22, so it needs a server artifact with the Node 22 runtime.

Everything else works without it. The gateway brings:

Feature Without the gateway
Slash commands, buttons, modals Not available
Role changes in real time (np_discord:playerRolesChanged within a second) The Config.Sync poll: one online player every 5 s
Guilds, roles, channels and emojis kept current by Discord's events Refreshed in the background after Config.Cache.*
Live voice states Asked for when needed
Discord → game chat Game → Discord only
The bot's activity line Not available
Member, ban, message and reaction events Not fired

Intents ​

Config.Gateway.intents lists what the gateway receives. Default: { 'GUILDS', 'GUILD_MEMBERS', 'GUILD_MODERATION', 'GUILD_VOICE_STATES', 'GUILD_MESSAGES' }.

Intent Brings Privileged
GUILDS Guilds, roles, channels, threads; needed for everything
GUILD_MEMBERS Live role and nickname changes, member join / leave events, the long member cache (Config.Cache.memberLive) yes
GUILD_MODERATION np_discord:memberBanned / memberUnbanned
GUILD_VOICE_STATES Live voice states, np_discord:voiceStateUpdated
GUILD_MESSAGES np_discord:message, the chat bridge
MESSAGE_CONTENT The text of messages (the chat bridge needs it) yes
DIRECT_MESSAGES Messages players send to the bot in DMs, as np_discord:message without a guild_id. The text is always included (no MESSAGE_CONTENT needed)
GUILD_MESSAGE_REACTIONS np_discord:reactionAdded / reactionRemoved
GUILD_PRESENCES Presence dispatches (raw np_discord:gateway only) yes

Also accepted: GUILD_EXPRESSIONS, GUILD_INTEGRATIONS, GUILD_WEBHOOKS, GUILD_INVITES, GUILD_MESSAGE_TYPING, DIRECT_MESSAGE_REACTIONS, DIRECT_MESSAGE_TYPING, GUILD_SCHEDULED_EVENTS, AUTO_MODERATION_CONFIGURATION, AUTO_MODERATION_EXECUTION. Their dispatches reach you through the raw np_discord:gateway (type, data) event. An unknown name is reported in the console.

Privileged intents must be switched on in the developer portal (Bot → Privileged Gateway Intents) first.

Connection ​

  • The gateway identifies, keeps the heartbeat, resumes after a short drop and reconnects on its own.
  • It stops for good, with a console message, when reconnecting cannot help until the configuration changes: an invalid token, disallowed intents, invalid intents, or a bot in too many guilds for one connection (sharding is not supported).
  • np_discord:gatewayReady (botUser) fires on every (re)connect; np_discord:gatewayClosed (reason) when it drops.
  • After every (re)connect all online players are read once, so a role taken away during the gap does not stay in game. Slash commands are synced again.
  • While it is down, cached members and voice states are dropped and guild data falls back to the background refresh.

IsGatewayConnected() tells you whether it is up, HasIntent('DIRECT_MESSAGES') whether Config.Gateway.intents asks for an intent (false with the gateway off). SetBotActivity(text, kind?, status?) sets the bot's status line (kind: playing watching listening competing custom; status: online idle dnd invisible).

The events between gateway.js and the Lua side (np_discord:gw:*) are server-local. None is a net event, so clients cannot reach them.