Bafflux Docs Developer Portal

Start here

Build your first bot

From nothing to a bot that answers /ping, in about ten minutes and 40 lines of JavaScript.

You need a Bafflux account with a verified email, a server where you have Manage Server (make one if you don't), and Node.js 22 or newer. The code uses no packages.

1. Make an application

Open the Developer Portal and press New Application. Give it a name: that's also your bot's display name. You can change it and add an icon later.

Note the Application ID on the General Information page. You'll need it below.

2. Get the token

Go to the Bot page and press Make Token. Copy it right away: it's only shown once. If you lose it, reset it and use the new one.

The token is a password for your bot. Don't put it in your code or commit it. The examples read it from an environment variable.

3. Add the bot to your server

Go to Installation. The page builds an install link with the permissions your bot asks for. The defaults (View Channels, Send Messages, Add Reactions) are fine for now. Open the link, pick your server and press Add Bot. It joins with the BOT tag and a role named after it.

4. Write the bot

Save this as bot.js:

js
// Answers /ping with "Pong!".
// Run with: BOT_TOKEN=... APP_ID=... node bot.js
const API = 'https://bafflux.com/api';
const GATEWAY = 'wss://bafflux.com/api/gateway';
const TOKEN = process.env.BOT_TOKEN;
const APP_ID = process.env.APP_ID;
const auth = { Authorization: `Bot ${TOKEN}`, 'Content-Type': 'application/json' };

// Tell Bafflux which slash commands this bot has (replaces the whole list).
await fetch(`${API}/applications/${APP_ID}/commands`, {
	method: 'PUT',
	headers: auth,
	body: JSON.stringify([{ name: 'ping', description: 'Checks the bot is awake' }])
});

function connect() {
	const ws = new WebSocket(GATEWAY, { headers: { Authorization: `Bot ${TOKEN}` } });

	ws.onmessage = async (event) => {
		const { t, d } = JSON.parse(event.data);
		if (t === 'READY') console.log(`Logged in as ${d.user.username}`);

		if (t === 'INTERACTION_CREATE' && d.data.name === 'ping') {
			// Answer within 3 seconds.
			await fetch(`${API}/interactions/${d.id}/${d.token}/callback`, {
				method: 'POST',
				headers: { 'Content-Type': 'application/json' },
				body: JSON.stringify({ type: 'message', content: 'Pong!' })
			});
		}
	};

	// Connections drop now and then (deploys, network blips). Come back.
	ws.onclose = () => setTimeout(connect, 5000);
}

connect();

Run it:

bash
BOT_TOKEN=bfx_your_token APP_ID=your_application_id node bot.js

On Windows PowerShell, set the variables first: $env:BOT_TOKEN="bfx_..."; $env:APP_ID="..."; node bot.js.

5. Try it

In your server, type / in the message box. Your bot's commands are listed under its name. Pick /ping and press Enter. The reply shows up with "your name used /ping" above it.

While the bot is running, its status dot is green in the member list. If it's not running, people who use its commands are told it's offline.

Where next

  • Commands: options (/say text: hello times: 3), types and per-server commands.
  • Interactions: private replies, "thinking..." for slow work, follow-ups.
  • Gateway: every event your bot can react to, like new messages for prefix commands.
  • Messages: posting on your own, not just in answer to a command.