Bafflux Docs Developer Portal

Building bots

Gateway

One WebSocket that tells your bot what's happening, as it happens.

Connect to wss://bafflux.com/api/gateway with your token in the Authorization header. While at least one connection is open, your bot shows as online.

js
const ws = new WebSocket('wss://bafflux.com/api/gateway', { headers: { Authorization: `Bot ${process.env.BOT_TOKEN}` } });
ws.onmessage = (event) => {
	const { t, d } = JSON.parse(event.data);
	console.log(t, d);
};

In Python, the websockets package takes the header as additional_headers={"Authorization": f"Bot {token}"}.

Messages

Every message from the gateway is JSON with two fields:

FieldMeaning
tThe event name, like MESSAGE_CREATE
dThe event's data

The gateway only talks: you never need to send anything on it. The server sends a WebSocket ping every 25 seconds, which WebSocket libraries answer on their own.

Staying connected

  • The first event is always READY, with your bot's account in d.user.
  • Connections can drop (server restarts, network trouble). Reconnect with a growing delay: 1 s, 2 s, 4 s... up to 30 s.
  • Events that happened while you were disconnected aren't replayed. After reconnecting, fetch whatever you need to catch up on (for example the newest messages of a channel).
  • A bot that stops reading falls behind: after 256 unread events, new ones are dropped for that connection.

Events

EventWhend
READYYou connected{ user }
SERVER_CREATEYour bot was added to a serverThe server, with channels, roles and myRoles
SERVER_UPDATEA server's name or icon changed{ id, name, iconUrl }
SERVER_REMOVEYour bot was kicked or banned, or the server was deleted{ serverId, reason }
SERVER_CHANNELS_UPDATEChannels were added, changed, moved or deleted{ serverId, channels } (the whole list you can see)
SERVER_ROLES_UPDATERoles changed{ serverId, roles }
THREAD_UPDATEA thread got a message, was renamed, hidden or shown{ serverId, channel }
MESSAGE_CREATEA new messageA message, plus serverId
MESSAGE_UPDATEA message was edited, or its link previews or embed image sizes arrivedA message
MESSAGE_DELETEA message was deleted{ id, channelId }
MESSAGE_REACTION_ADD / MESSAGE_REACTION_REMOVESomeone reacted or took it back{ channelId, messageId, userId, emoji }
MESSAGE_PINA message was pinned or unpinned{ channelId, messageId, pinned }
TYPING_STARTSomeone started typing{ channelId, userId }
MENTIONA message mentions your bot (directly, a role it has, or @everyone){ channelId, messageId, serverId }
MEMBER_ADDSomeone joined a server{ serverId, member }
MEMBER_REMOVESomeone left or was removed{ serverId, userId }
MEMBER_UPDATESomeone's roles changed{ serverId, userId, roles }
PRESENCE_UPDATESomeone's status changed{ userId, status, customStatus }
USER_UPDATESomeone changed their name or pictures{ id, username, displayName, avatarUrl, bannerUrl }
DM_OPENA DM with your bot openedThe DM channel
INTERACTION_CREATESomeone used one of your slash commandsAn interaction

You only get events for servers your bot is in and channels it can see.

Example: a prefix command

Prefix commands (like !help) are ordinary messages. Watch MESSAGE_CREATE, skip your own messages, and reply:

js
if (t === 'MESSAGE_CREATE' && d.content === '!help' && d.author.id !== myId) {
	await fetch(`https://bafflux.com/api/channels/${d.channelId}/messages`, {
		method: 'POST',
		headers: { Authorization: `Bot ${token}`, 'Content-Type': 'application/json' },
		body: JSON.stringify({ content: 'Try /ping or /say', replyTo: d.id })
	});
}

Take myId from the READY event (d.user.id). Register prefix commands too (see Commands), so they're listed in the command picker.