Categorygithub.com/daucf23/GoDiscordBot
repository
0.0.0-20241023032400-88ec7e5d00b6
Repository: https://github.com/daucf23/godiscordbot.git
Documentation: pkg.go.dev

# Packages

No description provided by the author
No description provided by the author

# 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

  • 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.

  1. Set the Go Environment Variables:

    export GOOS=linux
    export GOARCH=arm64
    
  2. 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 your main.go file.
  3. 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
    
  4. 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

  1. Create an Environment File:

    sudo vim /home/pi/discord-bot.env
    
  2. Add Your Environment Variables:

    OPENAI_API_KEY=your_api_key_here
    
  3. Secure the Environment File:

    sudo chown pi:pi /home/pi/discord-bot.env
    sudo chmod 600 /home/pi/discord-bot.env
    
  4. 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
    
  5. Reload Systemd and Restart the Service:

    sudo systemctl daemon-reload
    sudo systemctl restart discord-bot.service
    

Commands Summary

CommandDescriptionUsage
clearDeletes a specified number of recent messages!clear [number], e.g., !clear 5
muteMutes a user for a specified duration!mute [@user] [duration], e.g., !mute @user 10m
moveMoves a user to another voice channel!move [@user] [channel], e.g., !move @random afk
pingChecks the bot's latency or responsiveness!ping
rollRolls a random number!roll
helpProvides help information for commands!help
weatherShows the current weather for a specified location!weather [location], e.g., !weather London
jokeTells a random joke!joke
factProvides a random fact!fact
8ballGives a magic 8-ball style answer to a question!8ball [question], e.g., !8ball Will I be rich?
reverseReverses the input text!reverse [text], e.g., !reverse Hello World
mockMocks the input text in a playful way!mock [text], e.g., !mock Hello
askAsk OpenAI ChatGPT o1 model!ask [question], e.g., !ask When is the Singularity
pollCreates a poll with options!poll [question] [options], e.g., !poll Best pet? | Dog | Cat
quoteProvides 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
  • 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.