Skip to content

Bot Setup

Roi Horowitz edited this page Dec 2, 2026 · 6 revisions

Bot Setup

1. Create the Discord application

  1. Go to https://discord.com/developers/applications and click New Application.

  2. On the Bot page, click Reset Token and copy it. This is DISCORD_TOKEN; keep it secret.

  3. On General Information, copy the Application ID. This is DISCORD_CLIENT_ID.

  4. Invite the bot to your server with this URL (put your application ID in):

    https://discord.com/oauth2/authorize?client_id=YOUR_APP_ID&scope=bot+applications.commands&permissions=274877975552
    

    That grants View Channel, Send Messages, Send Messages in Threads and Read Message History. The bot needs all four in every channel and thread where people roll, because it posts damage requests, reveals and spell-save results there. For a private channel or thread, also add the bot to it.

  5. In Discord, turn on User Settings → Advanced → Developer Mode, then:

    • right-click your server → Copy Server ID: this is DISCORD_GUILD_ID;
    • right-click your own name → Copy User ID: this goes in GM_DISCORD_IDS.

2. Configure

In your copy of the bot folder (see Installation):

cp .env.example .env      # Windows: Copy-Item .env.example .env

Fill in .env:

Variable What it is
DISCORD_TOKEN The bot token from step 1.
DISCORD_CLIENT_ID The application ID.
DISCORD_GUILD_ID Your server ID. Commands are registered there.
FOUNDRY_TOKEN Any long random string. Paste the same value into the module's Bot token in Foundry.
FOUNDRY_ORIGIN The exact address you open Foundry at, e.g. https://my-foundry.example.com:30000 or http://localhost:30000. Separate several with commas.
GM_DISCORD_IDS GM user IDs, comma-separated. They get the roll DMs and can use GM commands.
ALLOWED_CHANNEL_IDS Optional. Only allow rolling in these channels; threads inside them count.
PORT HTTP port for the module to reach, default 8787.
DATA_DIR Where the bot keeps store.json, default ./data.

3. Install, register commands, start

npm install
npm run register     # registers the slash commands in your server
npm start

The log should say HTTP API listening on :8787 and Logged in to Discord as ….

Run npm run register again whenever an update adds or changes commands, and npm install again when an update adds packages (the changelog says so). Since /map, the bot draws images with @napi-rs/canvas, which comes prebuilt for Windows, macOS, Linux and Raspberry Pi (64-bit).

Keeping it running

npm start stops when you close the window. To keep the bot running in the background, start it again after a crash, and start it when the computer starts, pick the method for your system below. Run every command in the bot folder (the one with .env), because the bot reads .env from the folder it starts in.

For Docker, The Forge, Cloudflare Tunnel, VPS, Railway or Fly.io, see Hosting. On Fly.io, Railway and similar hosts, mount a persistent volume at DATA_DIR, or the bot forgets rolls and snapshots on each deploy.

Windows

The bot folder comes with two scripts:

  • start-bot.ps1 runs the bot, restarts it 30 seconds after it stops, and writes everything to logs\bot.log;
  • start-bot-hidden.vbs starts that script without a window.

A scheduled task runs them when you log in. Open PowerShell in the bot folder.

Set up once (starts the bot now and at every logon):

$dir = (Get-Location).Path
$action = New-ScheduledTaskAction -Execute 'wscript.exe' -Argument "`"$dir\start-bot-hidden.vbs`"" -WorkingDirectory $dir
$trigger = New-ScheduledTaskTrigger -AtLogOn -User $env:USERNAME
$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries
Register-ScheduledTask -TaskName 'Squire' -Action $action -Trigger $trigger -Settings $settings
Start-ScheduledTask -TaskName 'Squire'

See the log (the last 20 lines; add -Wait to keep watching, Ctrl+C to stop watching):

Get-Content logs\bot.log -Tail 20

Restart the bot, for example after an update. This stops the bot, and the script starts it again within 30 seconds:

Get-CimInstance Win32_Process -Filter "Name='node.exe'" | Where-Object CommandLine -like '*src*index.js*' | ForEach-Object { Stop-Process -Id $_.ProcessId }

Stop it until the next logon (or until you run Start-ScheduledTask -TaskName 'Squire'). This stops the restart script first, then the bot:

Get-CimInstance Win32_Process -Filter "Name='powershell.exe'" | Where-Object CommandLine -like '*start-bot.ps1*' | ForEach-Object { Stop-Process -Id $_.ProcessId }
Get-CimInstance Win32_Process -Filter "Name='node.exe'" | Where-Object CommandLine -like '*src*index.js*' | ForEach-Object { Stop-Process -Id $_.ProcessId }

Remove the task (run the Stop commands too, to stop the bot now):

Unregister-ScheduledTask -TaskName 'Squire' -Confirm:$false

The bot runs only while you're logged in to Windows. If the PC sleeps, Discord commands fail until it wakes up.

Linux or Raspberry Pi: pm2

pm2 keeps Node apps running and starts them at boot.

Set up once:

sudo npm install -g pm2
pm2 start src/index.js --name squire
pm2 save
pm2 startup

pm2 startup prints one sudo … command. Run it, so pm2 starts at boot and brings the bot back.

To Run
See if it's running pm2 status
See the log (Ctrl+C to stop watching) pm2 logs squire
Restart it, e.g. after an update pm2 restart squire
Stop it pm2 stop squire
Remove it pm2 delete squire && pm2 save

Linux or Raspberry Pi: systemd

Without installing anything extra, a systemd service does the same.

Set up once. Create the service file:

sudo nano /etc/systemd/system/squire.service

Paste this, and change User, WorkingDirectory (the full path of the bot folder) and the path to node (which node prints it):

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

[Service]
User=pi
WorkingDirectory=/home/pi/squire-bot
ExecStart=/usr/bin/node src/index.js
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target

Then start it, now and at every boot:

sudo systemctl daemon-reload
sudo systemctl enable --now squire
To Run
See if it's running systemctl status squire
See the log (Ctrl+C to stop watching) journalctl -u squire -f
Restart it, e.g. after an update sudo systemctl restart squire
Stop it sudo systemctl stop squire
Stop starting it at boot sudo systemctl disable squire

Docker

restart: unless-stopped in the Hosting example already restarts the bot and starts it at boot. Run these in the folder with docker-compose.yml:

To Run
See if it's running docker compose ps
See the log (Ctrl+C to stop watching) docker compose logs -f bot
Restart it docker compose restart bot
Rebuild after an update docker compose up -d --build
Stop it docker compose stop bot

Reaching the bot from Foundry

The GM's browser talks to the bot directly, so the Bot URL you enter in Foundry must be reachable from that browser:

  • If Foundry is served over https, the bot URL must be https too, because browsers block mixed content. The one exception is a bot on http://localhost, which browsers allow.
  • If you open Foundry at a public address and the bot runs on localhost, Chrome may ask once to allow local network access. Allow it, or syncing fails with "Can't reach the bot".

Health check

GET /api/health returns { ok, worlds: [{ worldId, worldTitle, system, snapshotAgeDays }] }. It's useful for uptime monitors and for checking that a world has synced.

Next: Foundry Setup.

Clone this wiki locally