Orbit Developers

Node.js

A complete bot in plain JavaScript with no dependencies. It replies pong to !ping and waves at hello. You'll need Node.js 20 or newer.

Run it

git clone https://github.com/z0roday/OrbitApi.git
cd OrbitApi/examples/node
export ORBIT_TOKEN='1234567890123456789.Qm9...'
npm start

Then type !ping in a server your bot is in.

How it works

orbit.js is a tiny API client. It adds your token to every request, waits and retries on 429, and throws an OrbitError with status and code when something fails:

import { Orbit } from './orbit.js';

const orbit = new Orbit(process.env.ORBIT_TOKEN);
const me = await orbit.me();
await orbit.send(channelId, 'Hello', { replyTo: messageId });
await orbit.react(channelId, messageId, '๐Ÿ‘‹');

orbit.events() reads the event stream and gives you { name, data } objects. bot.js remembers the last message it handled in each channel and only fetches what's new:

for await (const event of orbit.events()) {
  if (event.name === 'hello') await track();
  else if (event.name === 'messages') await catchUp(event.data.channel_id);
  else if (event.name === 'guild' || event.name === 'guilds') await track();
}

It also offers /ping and /roll. It registers them when it starts and answers each interaction event with a card:

await orbit.setCommands([{ name: 'ping', description: 'Checks that the bot is awake' }]);

const asked = await orbit.interaction(event.data.interaction_id);
await orbit.reply(asked.id, '', { embeds: [{ title: 'Pong', color: 0x5b8def }] });

When the connection drops, the bot reconnects, waiting a little longer each time, and catches up on what it missed. On 401 it stops, since that means the token was replaced.

Tests

npm test

The tests run the client against a small fake API.

Keep it running

On an Ubuntu server, a systemd service keeps the bot up. Put the token in a file only root can read and make a user for the bot:

sudo install -m 600 /dev/null /etc/orbit-bot.env
echo "ORBIT_TOKEN=1234567890123456789.Qm9..." | sudo tee /etc/orbit-bot.env >/dev/null
sudo useradd --system --no-create-home orbitbot

Then create /etc/systemd/system/orbit-bot.service:

[Unit]
Description=Orbit bot
After=network-online.target
Wants=network-online.target

[Service]
User=orbitbot
WorkingDirectory=/opt/OrbitApi/examples/node
EnvironmentFile=/etc/orbit-bot.env
ExecStart=/usr/bin/node bot.js
Restart=always
RestartSec=5
NoNewPrivileges=true
ProtectSystem=strict
ProtectHome=true
PrivateTmp=true

[Install]
WantedBy=multi-user.target
sudo systemctl enable --now orbit-bot
journalctl -u orbit-bot -f