DJWoodZ / FS22-Discord-Bot

A Discord bot that posts Farming Simulator 22 Dedicated Server status information such as online players.
MIT License
10 stars 2 forks source link
discord-bot farming-simulator farming-simulator-22 farming-simulator-game fs22

FS22 Discord Bot

A bot that monitors a Farming Simulator 22 Dedicated Server and posts status information in a Discord channel.

What does this bot do?

It will show information such as the server's status, the number of players online and players who have joined or left the game. It can also purge its own messages to keep the channel tidy.

Channel updates

This bot works with a single Farming Simulator 22 Dedicated Server and will post updates like this as the status changes:

FS22 [BOT] Today at 10:00
:tractor: The server is back online!
:tractor: The server FS22 has been updated: Farming Simulator 22 (1.12.0.0), Map: Elmcreek, DLCs: 2, Mods: 20
:joystick: The server has 1 new mod:
    :star2: Anti-AFK 1.0.1.0 by DJ WoodZ (d6f656fdd79f073fd9557d8acf847946)

FS22 [BOT] Today at 10:30
:farmer: 1 of 4 players online: Player 1 (1 January 2023 10:30).
    :arrow_right: Player 1 just joined the server.

FS22 [BOT] Today at 10:40
:farmer: 2 of 4 players online: Player 1 and Player 2 (1 January 2023 10:40).
    :arrow_right: Player 2 just joined the server.

FS22 [BOT] Today at 12:45
:farmer: 1 of 4 players online: Player 2 (1 January 2023 12:45).
    :arrow_left: Player 1 just left the server after playing for 2 hours and 15 minutes.

FS22 [BOT] Today at 12:50
:farmer: 0 of 4 players online (1 January 2023 12:50).
    :arrow_left: Player 2 just left the server after playing for 2 hours and 10 minutes.

FS22 [BOT] Today at 13:00
:moneybag: Savegame Money: :arrow_up_small: 123,456 (+1,234).
:watch: Savegame Play Time: 11 days, 22 hours and 33 minutes.

Activity updates

It will also update its current activity (status) to show the Dedicated Server's current status and number of online players:

FS22 [BOT]
Playing online: 2/4

How does it work?

The bot is fully automatic. It does not require or respond to Discord user commands.

It retrieves the status of the server by polling the server's XML feed endpoints (/feed/dedicated-server-savegame.html and /feed/dedicated-server-stats.xml). By default it will poll once per minute.

Permissions

The only permission this bot requires is the Send Messages permission, which can be found in the Discord Developer Portal under: bot -> Text Permission -> Send Messages

Environment Variables

Copy the .env file and create an .env.local file for your environment variables. You will then need to edit the .env.local file.

Can I just edit the .env file?

It would work but you shouldn't do that. It is good practice to create and edit an .env.local file because it will be excluded from commits by the .gitignore file, whereas the .env file will not be excluded from commits.

Which variables must I edit?

As a minimum you will need to specify a FS22_BOT_DISCORD_TOKEN value, which you obtain via the Discord Developer Portal. You will also need to verify that the FS22_BOT_URL_CAREER_SAVEGAME and FS22_BOT_URL_SERVER_STATS values are correct.

Anything else to be aware of?

By default the bot will post to all channels it has access to on all servers it has been added to.

You should use Discord's roles and permissions to control which channels it can post to, but you can also specify the FS22_BOT_DISCORD_SERVER_NAME and FS22_BOT_DISCORD_CHANNEL_NAME values to further ensure it only posts to your chosen Discord server and/or channel. This is recommended.

Be sure to spell the server and channel names exactly as written in Discord.

What about purging?

By default purging is disabled. When enabled, it will only purge its own messages and it will only purge from the specified channel on the specified server, both of which must be set using FS22_BOT_PURGE_DISCORD_CHANNEL_SERVER_NAME and FS22_BOT_PURGE_DISCORD_CHANNEL_NAME.

Be sure to spell the server and channel names exactly as written in Discord.

If you do not want the bot to purge its old messages, simply leave these values blank.

Environment Variables

Installation

You can either run this project directly on a host machine, or you can run it in a docker container. If you are going to be running it directly, the dependencies will need to be installed.

Running directly on host machine

You need git and Node.js to be installed, then you must clone this repository and install the dependencies:

git clone https://github.com/DJWoodZ/FS22-Discord-Bot.git
cd FS22-Discord-Bot
npm install
cp .env .env.local

You will need edit the .env.local file. See the Environment Variables section for details.

CLI Commands

* Install nodemon globally first with npm install nodemon -g
** Install PM2 globally first (see below)

Installing as a service (with PM2)

The npm run pm2:start and npm run pm2:stop scripts use a global PM2 NPM dependency.

Installing PM2 globally
npm install pm2@latest -g
Run as a service (Linux, etc.)

To ensure the Discord bot service starts automatically following a system reboot:

npm run pm2:start
pm2 startup
pm2 save

See the PM2 Process Management Quick Start for details.

Run as a service (Windows)

To run as a service on Windows, you will need to use pm2-installer.

To ensure the Discord bot service starts automatically following a system reboot:

npm run pm2:start
pm2 save

See the PM2 Process Management Quick Start for details.

Running with Docker Compose

This project comes pre-configured ready for use with Docker Compose.

The default Docker Compose configuration (compose.yaml) will use the .env.local file on the host machine.

The FS22_BOT_DB_PATH value in .env.local will be ignored by the Docker Compose configuration. The default Docker Compose configuration will create and use a database JSON file located on the host machine at: .\docker-volumes\db\db.json.

You need git, Docker and Docker Compose to be installed and then you must clone this repository:

git clone https://github.com/DJWoodZ/FS22-Discord-Bot.git
cd FS22-Discord-Bot
cp .env .env.local

You will need edit the .env.local file. See the Environment Variables section for details.

Running normally

docker compose up

Running in detached mode

To run as a service, use detached mode:

docker compose up -d

Force build and recreation

If you need to force Docker Compose to build the image and recreate the container, you can use the --build and --force-recreate options:

docker compose up --build --force-recreate

This can also be used with detached mode:

docker compose up -d --build --force-recreate

For a full list of options see the docker compose up documentation.

Interacting with the container

For development only.

If you need to interact with the container, you can use this command (assuming the container is named fs22-discord-bot-server-1):

docker exec -it fs22-discord-bot-server-1 /bin/sh