[11/16/24]

1. Prerequisites

  1. Update Your Raspberry Pi Ensure your Raspberry Pi is up to date:

    sudo apt update && sudo apt upgrade
  2. Install Python Python 3 should already be installed on Raspbian (or Raspberry Pi OS). Verify the installation:

    python3 --version

    If Python 3 isn’t installed, install it:

    sudo apt install python3 python3-pip
  3. Install discord.py Use pip to install the Discord library for Python:

    pip3 install discord.py
  4. Create a Discord Developer Account

    • Go to the Discord Developer Portal.
    • Create a new application and name it.
    • Navigate to the Bot section, click Add Bot, and confirm.

2. Coding Your Bot

  1. Create a Bot Token
    • Under your bot’s settings in the Discord Developer Portal, copy the Token. This is needed to authenticate your bot.
  • Create a directory for your bot:
mkdir discord-bot && cd discord-bot
  • Create a Virtual Environment (see venv)
python3 -m venv discord-bot-env
  • Activate the Virtual Environment:
source discord-bot-env/bin/activate
  • Install discord.py in the Virtual Environment:
pip install -U git+https://github.com/Rapptz/discord.py
  • Once installed, you can run your bot with the virtual environment activated. To deactivate, use:
deactivate
  1. Write the Bot Code
  • Create a new file for your bot script:

    nano bot.py
  • Paste the following starter code into bot.py:

    import discord
    from discord.ext import commands
     
    # Set up the bot
    intents = discord.Intents.default()
    bot = commands.Bot(command_prefix="!", intents=intents)
     
    @bot.event
    async def on_ready():
        print(f"Logged in as {bot.user}")
     
    @bot.command()
    async def hello(ctx):
        await ctx.send("Hello, world!")
     
    # Run the bot
    bot.run("YOUR_TOKEN_HERE")

    Replace YOUR_TOKEN_HERE with the token from the Developer Portal.

    Save and exit by pressing CTRL+O, Enter, and CTRL+X.


3. Running the Bot

Run the bot:

python3 bot.py

If everything is set up correctly, you should see:

Logged in as <bot_name>

4. Invite the Bot to Your Server

  1. In the Discord Developer Portal:

    • Navigate to the OAuth2 tab.
    • Under Scopes, check bot.
    • Under Bot Permissions, select the necessary permissions for your bot (e.g., Send Messages).
  2. Copy the generated URL and paste it into your browser. Add the bot to your server.


5. Keeping the Bot Running

To keep the bot running continuously, use one of the following methods:

  • screen Command:
  • install screen
  sudo apt install screen
  • Start discord bot inside a screen session
screen -S discord-bot
python3 bot.py
  • Check if you are in the screen (returns session ID)
screen -ls
# echo $STY
  • To detach, press CTRL+A, then D. Reattach later with:
screen -r discord-bot
  • tmux Command:

    sudo apt install tmux
    tmux new -s discord-bot
    python3 bot.py

    Detach with CTRL+B, then D. Reattach later with:

    tmux attach -t discord-bot
  • Systemd Service: Create a systemd service file:

    sudo nano /etc/systemd/system/discord-bot.service

    Add the following:

    [Unit]
    Description=Discord Bot
    After=network.target
     
    [Service]
    ExecStart=/usr/bin/python3 /path/to/your/discord-bot/bot.py
    WorkingDirectory=/path/to/your/discord-bot
    Restart=always
    User=pi
     
    [Install]
    WantedBy=multi-user.target

    Enable and start the service:

    sudo systemctl enable discord-bot
    sudo systemctl start discord-bot

Next Steps

  1. Add more commands and features to your bot using the discord.py documentation.
  2. Use .env files to securely store your bot token.
  3. Explore hosting alternatives like using Docker for better portability.