Intro

How to create a discord bot hosted on a Raspberry Pi running Raspbian OS.

Create Bot on Discord Developer Portal
  • Create a new application on Discord Developer Portal
    • Navigate to the Bot section, click Add Bot, and confirm.
    • Under the bot’s settings in Discord Developer Portal, copy the Token (needed to authenticate the bot)
    • Turn on Gateway Intents
Environment Setup
  • Create Python environment
python3 -m venv discord-bot-env
  • Activate environment
source discord-bot-env/bin/activate
  • Create requirements.txt file
aiohttp==3.11.11
discord.py @ git+https://github.com/Rapptz/discord.py@db7b2d9058e4e7f357a6a9f02aad41e8b053a1bd
python-dotenv==1.1.0
  • Install dependencies
python3 -m pip install -r requirements.txt
  • Create .env file and replace <bot_token> with the token from the Developer Portal
DISCORD_BOT_TOKEN=<bot_token>
Create bot script
  • Create a new file
nano bot.py
  • Add starter code
from dotenv import load_dotenv
import os
import discord
from discord.ext import commands
 
# Set up the bot
intents = discord.Intents.default()
bot = commands.Bot(command_prefix="!", intents=intents)
intents.message_content = True  # Required for reading user messages
 
@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
load_dotenv()
bot.run(os.environ["DISCORD_BOT_TOKEN"])
  • Run the bot:
python3 bot.py

If everything is set up correctly, you should see:

Logged in as <bot_name>
Invite the Bot to Your Server
  • 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).
  • Copy the generated URL and paste it into your browser. Add the bot to your server.