A modular Twitch bot built with TwitchIO, featuring:
- OAuth-based multi-channel support
- EventSub integration
- Component-based command system
- SQLite-backed token storage
- Modern Python tooling (uv, Ruff, Pylance)
- π OAuth authentication for bot + broadcaster
- π‘ EventSub subscriptions (chat, stream online, channel points)
- π§© Component system for commands/plugins
- πΎ Persistent token storage using SQLite
- π¬ Chat β Discord logging via webhook
- π΅ Spotify song requests + now playing command
- β‘ Fast development setup with
uvand Ruff
-
Python 3.11+
-
A Twitch Developer Application
-
Two Twitch accounts:
- Bot account
- Main broadcaster account
Before running the bot, you need to create a Twitch application.
-
Go to the Twitch Developer Console: https://dev.twitch.tv/console
-
Click "Register Your Application"
-
Fill in the form:
-
Name: (e.g.
My Twitch Bot) -
OAuth Redirect URL:
http://localhost:4343/oauth/callback -
Category: Application Integration
-
-
Click Create
-
Once created:
- Copy your Client ID
- Generate and copy your Client Secret
- The redirect URL must match exactly:
http://localhost:4343/oauth/callback - Do not share your Client Secret
git clone <your-repo-url>
cd <repo-name>Using uv:
uv syncCreate your env files from the examples:
cp .env.example .env
cp .env.secret.example .env.secretFill in:
TWITCH_CLIENT_ID=your_client_id
TWITCH_CLIENT_SECRET=your_client_secret
CHAT_TO_DISCORD_LOGGER_WEBHOOK_URL=your_discord_webhook_url
# Optional: Spotify
SPOTIFY_CLIENT_ID=your_spotify_client_id
SPOTIFY_CLIENT_SECRET=your_spotify_client_secret
SPOTIFY_REDIRECT_URI=http://localhost:8888/callbackTWITCH_BOT_ID=your_bot_user_id
TWITCH_OWNER_ID=your_main_account_user_id
DATABASE_NAME=tokens.db
# Optional: Spotify
SPOTIFY_CHANNEL_REWARD_ID=your_channel_point_reward_id
SPOTIFY_TWITCH_REQUEST_PLAYLIST_ID=your_playlist_idYou need the numeric Twitch user IDs for both your bot account and your main account.
You can easily convert a username to a user ID using:
π https://streamscharts.com/tools/convert-username
- Enter your Twitch username
- Copy the returned User ID
If your username is:
my_twitch_name
You will get something like:
123456789
TWITCH_BOT_ID=bot_account_user_id
TWITCH_OWNER_ID=your_main_account_user_idTwitch APIs (including EventSub) require user IDs, not usernames.
uv run main.pyThis will:
- Start the bot
- Launch a local OAuth server at
http://localhost:4343
You must authorise both accounts.
Open a private/incognito browser.
Log in as the bot account, then visit:
Bot Account OAuth URL:
http://localhost:4343/oauth?scopes=user:read:chat%20user:write:chat%20user:bot%20channel:bot%20channel:read:ads%20channel:read:redemptions%20channel:manage:redemptions%20channel:manage:ads%20channel:manage:moderators%20channel:manage:polls%20channel:manage:predictions%20channel:manage:vips%20channel:edit:commercial%20channel:moderate%20moderator:manage:announcements%20moderator:read:banned_users%20moderator:manage:banned_users%20moderator:read:chat_messages%20moderator:manage:chat_messages&force_verify=true
In your normal browser (logged into your main account), visit:
Broadcaster OAuth URL:
http://localhost:4343/oauth?scopes=channel:bot&force_verify=true
http://localhost:4343/oauth/callback
To enable Spotify song requests:
- Go to: https://developer.spotify.com/dashboard
- Create an application
- Add redirect URI:
http://localhost:8888/callback
- Copy Client ID + Secret into
.env.secret
-
Users redeem a channel point reward with a Spotify link
-
Bot:
- Adds song to queue
- Saves to playlist
- Confirms in chat
Command:
!song
β Displays currently playing track
Before using the bot, make sure everything is set up correctly:
- Twitch application created in Developer Console
- Redirect URL set to
http://localhost:4343/oauth/callback -
.envand.env.secretcreated and filled in - Correct
CLIENT_IDandCLIENT_SECRETadded - Discord webhook URL added (
CHAT_TO_DISCORD_LOGGER_WEBHOOK_URL) - (Optional) Spotify credentials configured
- Correct
BOT_IDandOWNER_IDadded - Dependencies installed (
uv sync) - Bot is running (
uv run main.py) - OAuth completed for bot account
- OAuth completed for broadcaster account
- Bot account has moderator/VIP permissions in your channel (if required)
If everything is working correctly:
- Bot connects without errors
- OAuth completes successfully
- Bot responds to commands in chat
- Chat messages appear in your Discord webhook
- Spotify requests are added to queue (if enabled)
- Tokens are saved to
data/tokens.db
-
Stored in:
data/tokens.db -
Automatically created on startup
-
Stores:
- user_id
- access token
- refresh token
Commands are modular and live in:
bot/components/
Each component:
- Is auto-loaded on startup
- Must not start with
_ - Uses TwitchIOβs
commands.Component
(unchanged)
Handles Twitch channel point song requests.
- Accepts Spotify links from channel point rewards
- Adds songs to playback queue
- Saves songs to a playlist
- Provides
!songcommand
bot/components/spotify.py
- Requires Spotify Premium for queue control
- Requires valid Spotify OAuth setup
- Only accepts Spotify track links
uv run ruff check . --fix
uv run ruff format .- Python
- Pylance
- Ruff
bot/
βββ app.py # Bot implementation
βββ config.py # Environment + paths
βββ database.py # Token persistence
βββ components/ # Command modules
main.py # Entry point
pyproject.toml # Project config
- Do NOT commit
.envor.env.secret - Tokens are stored locally in SQLite
- First run requires OAuth setup
- Bot must remain running during OAuth
Built with:
- TwitchIO
- Spotipy
- asqlite
- Ruff
- uv