~igemnace/treble-discord

The Treble Discord bot, responsible for communicating with a running Project Zomboid server.
Link SETUP.md from README.md
Tweak README sentence

refs

main
browse  log 

clone

read-only
https://git.sr.ht/~igemnace/treble-discord
read/write
git@git.sr.ht:~igemnace/treble-discord

You can also use your local clone with git send-email.

#Treble

Personal Discord bot for controlling a Project Zomboid server running on Arch using the AUR package, with rcon-cli and a custom pzsh wrapper around rcon-cli (so you don't need to specify credentials every time).

#Rationale

There is already an excellent Discord example app that you can use as a template (the docs even assume you're using it).

I wanted to make my own to test out a few new things:

  • Fastify over Express, to test out a simple toy server implementation.
  • Unit tests with the builtin node:test, to see how far I can get. Gist if you're interested: tests and assertions are already pretty good, but mocking imported modules will still need a library.
  • Native ESM, hence all the *.mjs files.
  • A bunch of other experimental Node features, such as import assertions and --watch.

#Usage

#Discord Slash Commands

The Discord bot defines these slash commands:

Command Description
/server_status Pings the server over RCON and lets you know if the server is up.
/restart Restarts the server safely with the quit admin command.
/pzsh <command> Runs an admin command over RCON.

#/pzsh Admin Commands

All admin commands are available for use with /pzsh, but some of them (e.g. help and showoptions) show copious output, and the response won't fit in the Discord message that the bot will respond with, so keep that in mind (for now, at least).

Here's a table of some useful commands, but for a full list, refer to the wiki page.

Note: mind the usage of double quotes!

Command Description
/pzsh save Save The World™.
/pzsh setaccesslevel "<username>" "admin" Make <username> an admin, so they can run admin commands in-game.
/pzsh servermsg "<message>" Send a message to everyone on the server.

#Development

#Setup

This uses Node 20. nvm install if you're using NVM.

For development, make sure to fill up .env using template.env. If running in production, make sure environment variables are set up.

Commands are defined in utils/commands.mjs. Install these with:

npm run discord:install

These are the same commands handled on POST /interactions, with the handlers defined in routes/interactions/post.command.mjs.

To run the development server:

npm run dev:watch

#Testing

There is a full testing suite with Node's builtin node:test, as mentioned.

npm run test

The tests for utils/spawnCommand.mjs rely on POSIX true, false, echo, and sh. These will fail on a non-Unix system. I should probably fix that, but the above are such an easy way to test exit codes, stdout, and stderr...

#Deployment

All that's really needed is to:

  • Run this server on a machine with access to the Project Zomboid server via rcon-cli and the pzsh wrapper script
  • Expose port 3000 to the public Internet (so it can be configured as Discord endpoints)

For a specific example use case, see SETUP.md.

#Licensing

This project is licensed under the GNU General Public License v3.

Copyright 2023 Ian Emnace