This project will cover how I created my discord bot along with helpful documentation.
DISCORD-BOT/
--|.gitignore
--|bot.py
--|requirements.txt
You will need to create a .env file in the root directory with the following values.
DISCORD_TOKEN={REPLACE_WITH_ACTUAL_VALUE}
TWITCH_CLIENT_ID={REPLACE_WITH_ACTUAL_VALUE}
TWITCH_CLIENT_SECRET={REPLACE_WITH_ACTUAL_VALUE}
TWITCH_USERNAME={REPLACE_WITH_ACTUAL_VALUE}
ANNOUNCE_CHANNEL_ID={REPLACE_WITH_ACTUAL_VALUE}
an example would look like this:
TWITCH_USERNAME=duida
The purpose is to keep account and channel information separate from pushed public code.
To integrate your bot with Discord, you'll need to register it in the Discord Developer Portal:
- Go to Discord Developer Portal
- Click "New Application" (top right).
- Give your bot a name, then click Create.
- Navigate to "Bot" (left sidebar) and click "Add Bot".
- Under the Bot Settings, enable:
Public Bot
(optional, if you want others to invite it)Presence Intent
,Server Members Intent
, andMessage Content Intent
(if needed for your bot’s functionality).
- Copy the Bot Token under "Click to Reveal Token"—you'll need this for your
.env
file.
- Go to "OAuth2" > "URL Generator" in the Developer Portal.
- Under Scopes, select
bot
. - Under Bot Permissions, check the permissions your bot needs (e.g.,
Send Messages
,Read Message History
). - Copy the generated URL and open it in your browser.
- Select a server and click Authorize to invite the bot.
Credential | Where to Find It |
---|---|
DISCORD_TOKEN |
Found in Discord Developer Portal under Bot → Click to Reveal Token |
TWITCH_CLIENT_ID |
Found in Twitch Developer Console under Applications → Register Your App |
TWITCH_CLIENT_SECRET |
After registering an app in the Twitch Developer Console, click Manage, then Generate Secret |
ANNOUNCE_CHANNEL_ID |
Right-click a Discord channel → Click Copy ID (Requires Developer Mode enabled in User Settings → Advanced) |
Ensure you have Python 3.8+ installed and pip updated:
python --version
pip install --upgrade pip
In a python environment
- Install dependencies
- pip install -r requirements.txt
- Make a .env file
- Run the bot:
python bot.py
If you want to run this bot on a Raspberry Pi, follow these steps:
Ensure your Raspberry Pi is up to date:
sudo apt update && sudo apt upgrade -y
Install Python 3 and pip if they are not already installed:
sudo apt install python3 python3-pip -y
Verify the installation:
python3 --version
pip3 --version
git clone https://github.com/your-repo/discord-bot.git
cd discord-bot
Use pip to install the required dependencies:
pip3 install -r requirements.txt
Follow the steps in prerequisites to create the .env
file
To start the bot manually:
python3 bot.py
To ensure the bot starts automatically after a reboot, create a systemd service:
- Open a new service file:
sudo nano /etc/systemd/system/discord-bot.service
- Add the following content:
[Unit]
Description=Discord Bot
After=network.target
[Service]
ExecStart=/usr/bin/python3 /home/pi/discord-bot/bot.py
WorkingDirectory=/home/pi/discord-bot
StandardOutput=inherit
StandardError=inherit
Restart=always
User=pi
[Install]
WantedBy=multi-user.target
Replace /home/pi/discord-bot/
with the actual path where your bot is stored.
- Enable and start the service:
sudo systemctl enable discord-bot
sudo systemctl start discord-bot
- Check the status:
sudo systemctl status discord-bot
You may also run the command below to get more detailed logs.
sudo journalctl -u discord-bot -f
Further reading if you'd like help imaging your raspberry pi, what you can do with your discord bot, and twitch api documentation:
- Raspberry Pi Imaging & Setup: Raspberry Pi Imager Guide
- Discord Bot Commands & API: Discord Developer Portal
- Twitch API & Authentication: Twitch Developer Documentation
- Systemd for Auto-Restarting Services: Systemd Service Configuration