# README
Discord Bot Service Setup on Raspberry Pi 5
This guide provides step-by-step instructions to compile a Go-based Discord bot and set it up as a systemd service on a Raspberry Pi 5. By the end of this guide, your Discord bot will run automatically on startup and can be managed using standard systemctl
commands.
Table of Contents
- Prerequisites
- Compiling the Go Binary
- Setting Up the Systemd Service
- Environment Variables
- Commands Summary
- Conclusion
Prerequisites
- Raspberry Pi 5 with Raspberry Pi OS installed.
- Go Programming Language installed on your development machine.
- Go-based Discord bot source code.
- User account with
sudo
privileges on the Raspberry Pi. - Internet connection for the Raspberry Pi.
- OpenAI API Key (if your bot uses OpenAI services).
Compiling the Go Binary
To ensure your Go binary is compatible with the Raspberry Pi 5 architecture, you need to compile it for Linux ARM64.
-
Set the Go Environment Variables:
export GOOS=linux export GOARCH=arm64
-
Compile the Go Program:
Navigate to your Go project's root directory and run:
go build -o /home/pi/discord-bot /path/to/your/main.go
- Replace
/home/pi/discord-bot
with the desired output path on your Raspberry Pi. - Replace
/path/to/your/main.go
with the path to yourmain.go
file.
- Replace
-
Transfer the Binary to Raspberry Pi (if compiled elsewhere):
Use
scp
or another file transfer method:scp /path/to/discord-bot pi@raspberry_pi_ip:/home/pi/discord-bot
-
Set Executable Permissions on Raspberry Pi:
chmod +x /home/pi/discord-bot
Setting Up the Systemd Service
1. Create the Service File
On your Raspberry Pi, create a new service file:
sudo vim /etc/systemd/system/discord-bot.service
2. Configure the Service File
Paste the following content into the file:
[Unit]
Description=Discord Bot Service
After=network-online.target
Requires=network-online.target
[Service]
Type=simple
User=pi
WorkingDirectory=/home/pi/
ExecStart=/home/pi/discord-bot
Restart=on-failure
RestartSec=5s
[Install]
WantedBy=multi-user.target
- User: Replace
pi
with your username if different. - WorkingDirectory: The directory where your binary resides.
- ExecStart: The path to your Go binary.
3. Reload Systemd and Enable the Service
sudo systemctl daemon-reload
sudo systemctl enable discord-bot.service
4. Start and Test the Service
sudo systemctl start discord-bot.service
sudo systemctl status discord-bot.service
-
Verify that the service is active (running).
-
To view real-time logs:
sudo journalctl -u discord-bot.service -f
Environment Variables
If your Discord bot requires environment variables (e.g., OPENAI_API_KEY
), you need to provide them to the service.
Method 1: Define in Service File
Edit the service file:
sudo vim /etc/systemd/system/discord-bot.service
Add the Environment
directive under the [Service]
section:
Environment="OPENAI_API_KEY=your_api_key_here"
Replace your_api_key_here
with your actual API key.
Method 2: Use an Environment File
-
Create an Environment File:
sudo vim /home/pi/discord-bot.env
-
Add Your Environment Variables:
OPENAI_API_KEY=your_api_key_here
-
Secure the Environment File:
sudo chown pi:pi /home/pi/discord-bot.env sudo chmod 600 /home/pi/discord-bot.env
-
Modify the Service File to Include the Environment File:
sudo vim /etc/systemd/system/discord-bot.service
Add the following line under the
[Service]
section:EnvironmentFile=/home/pi/discord-bot.env
-
Reload Systemd and Restart the Service:
sudo systemctl daemon-reload sudo systemctl restart discord-bot.service
Commands Summary
Command | Description | Usage |
---|---|---|
clear | Deletes a specified number of recent messages | !clear [number] , e.g., !clear 5 |
mute | Mutes a user for a specified duration | !mute [@user] [duration] , e.g., !mute @user 10m |
move | Moves a user to another voice channel | !move [@user] [channel] , e.g., !move @random afk |
ping | Checks the bot's latency or responsiveness | !ping |
roll | Rolls a random number | !roll |
help | Provides help information for commands | !help |
weather | Shows the current weather for a specified location | !weather [location] , e.g., !weather London |
joke | Tells a random joke | !joke |
fact | Provides a random fact | !fact |
8ball | Gives a magic 8-ball style answer to a question | !8ball [question] , e.g., !8ball Will I be rich? |
reverse | Reverses the input text | !reverse [text] , e.g., !reverse Hello World |
mock | Mocks the input text in a playful way | !mock [text] , e.g., !mock Hello |
ask | Ask OpenAI ChatGPT o1 model | !ask [question] , e.g., !ask When is the Singularity |
poll | Creates a poll with options | !poll [question] [options] , e.g., !poll Best pet? | Dog | Cat |
quote | Provides a random inspirational quote | !quote |
Compiling the Go Binary
export GOOS=linux
export GOARCH=arm64
go build -o /home/pi/discord-bot /path/to/your/main.go
Setting Up the Service
sudo vim /etc/systemd/system/discord-bot.service
Service File Content:
[Unit]
Description=Discord Bot Service
After=network-online.target
Requires=network-online.target
[Service]
Type=simple
User=pi
WorkingDirectory=/home/pi/
ExecStart=/home/pi/discord-bot
EnvironmentFile=/home/pi/discord-bot.env # Only if using an environment file
Restart=on-failure
RestartSec=5s
[Install]
WantedBy=multi-user.target
Enabling and Starting the Service
sudo systemctl daemon-reload
sudo systemctl enable discord-bot.service
sudo systemctl start discord-bot.service
Checking Service Status and Logs
sudo systemctl status discord-bot.service
sudo journalctl -u discord-bot.service -f
Environment Variables
Create Environment File:
sudo vim /home/pi/discord-bot.env
Set Permissions:
sudo chown pi:pi /home/pi/discord-bot.env
sudo chmod 600 /home/pi/discord-bot.env
Conclusion
You have successfully compiled your Go-based Discord bot and set it up as a systemd service on your Raspberry Pi 5. The bot will now start automatically on boot and can be managed using systemctl
commands.
Tips:
-
Managing the Service:
- Stop the Service:
sudo systemctl stop discord-bot.service
- Restart the Service:
sudo systemctl restart discord-bot.service
- Disable the Service at Boot:
sudo systemctl disable discord-bot.service
- Stop the Service:
-
Security:
- Keep your API keys secure by limiting file permissions.
- Avoid hardcoding sensitive information into your code or service files.
-
Troubleshooting:
- Check logs using
journalctl
if the bot isn't working as expected. - Ensure the binary is executable and compiled for the correct architecture.
- Check logs using