A flexible and resilient Discord bot focused on playing online radio streams 24/7. Built with Python, discord.py
, and packaged for easy deployment using Docker. Its modular Cog architecture allows for easy extension and customization.
- Plays online radio stream URLs (HTTP/HTTPS).
- Supports a predefined list of radio streams (easily configurable).
- Paginated list command (
/list
or,,list
) with descriptions for easy browsing. - Displays currently playing track metadata (song title/artist) if the stream provides it (ICY metadata).
- Control playback with both Slash Commands (
/
) and Prefix Commands (default:,,
). - Displays a "Now Playing" embed with stream info and metadata.
- Stop playback using commands or reacting with ⏹️ to the Now Playing message.
- Persistent State: Remembers which stream was playing in which channel.
- Auto-Resume: Automatically attempts to rejoin and resume playback after bot restarts.
- Robust Reconnection: Actively tries to reconnect and resume if voice connection or gateway connection drops unexpectedly.
- Modular Cog System: Functionality is separated into Cogs (
cogs/
directory) for easy maintenance and contribution. - Dockerized: Simple setup and deployment using Docker and Docker Compose.
- Language: Python 3.11+
- Library:
discord.py
(v2.x) - HTTP Requests:
aiohttp
(for metadata fetching) - Containerization: Docker & Docker Compose
- Audio Processing: FFmpeg (installed within Docker image)
- Docker: Install Docker Desktop (Windows, macOS) or Docker Engine (Linux). Get it from https://www.docker.com/get-started.
- Docker Compose: Usually included with Docker Desktop. For Linux Server, you might need to install it separately (follow Docker's official documentation).
- Git: Required to clone this repository.
-
Clone the Repository:
git clone https://github.com/shizuo-x/Yuzu-radio.git cd discord-radio-bot-modular
(Replace
your-repository-url
with the actual URL of your Git repo) -
Create the
.env
File:- This file stores your secret bot token.
- Copy the example file:
- Linux/macOS:
cp .env.example .env
- Windows (Command Prompt):
copy .env.example .env
- Windows (PowerShell):
Copy-Item .env.example .env
- Linux/macOS:
-
Get Your Discord Bot Token:
- Go to the Discord Developer Portal.
- Click "New Application" (or select an existing one). Give it a name (e.g., "My Radio Bot").
- Navigate to the "Bot" tab (left menu).
- Click "Add Bot" and confirm.
- Under the bot's username, find the "Token" section. Click "Reset Token" (or "View Token") and COPY the token string. Treat this token like a password! Do not share it!
- VERY IMPORTANT: Scroll down on the Bot page to "Privileged Gateway Intents". You MUST ENABLE these intents:
SERVER MEMBERS INTENT
(Needed for user info)MESSAGE CONTENT INTENT
(Needed for prefix commands)- (Presence Intent is usually not needed)
- Click "Save Changes" at the bottom.
-
Configure
.env
:- Open the
.env
file you created in Step 2 with a text editor. - Replace the placeholder
YOUR_BOT_TOKEN_GOES_HERE
with the actual bot token you copied. - The line should look like:
DISCORD_TOKEN=AbCdEfGhIjKlMnOpQrStUvWxYz.aBcDeF.abcdefghijklmnopqrstuvwxyz123456
(but with your real token). - Save and close the
.env
file.
- Open the
-
Build and Run the Bot:
- Make sure Docker Desktop or Docker Engine is running.
- In your terminal (inside the
discord-radio-bot-modular
folder), run:
docker-compose up -d
- This command will:
- Build the Docker image based on
Dockerfile
(if not already built 7801 ). - Create and start a container named
discord-radio-bot-modular
based ondocker-compose.yml
. - Run the bot in the background (
-d
).
- Build the Docker image based on
-
Invite Your Bot:
- Go back to the Discord Developer Portal -> Your Application -> OAuth2 -> URL Generator.
- Select the following Scopes:
bot
applications.commands
(Required for Slash Commands)
- In the "Bot Permissions" section that appears below, select these permissions:
- General:
Read Messages/View Channels
,Send Messages
,Embed Links
,Read Message History
,Add Reactions
- Voice:
Connect
,Speak
- Recommended:
Manage Messages
(Allows removing reactions cleanly for pagination).
- General:
- Copy the Generated URL at the bottom.
- Paste the URL into your web browser, select the server you want to add the bot to, and click Authorize.
- Default Prefix:
,,
- Help Command:
,,help
or/help
- List Stations:
,,list
or/list
(Paginated with reactions) - Play Station:
,,play <URL or Predefined Name>
or/play stream:<URL or Predefined Name>
- Stop Playback:
,,stop
or/stop
or react with ⏹️ on the Now Playing message. - Show Current Info:
,,now
or/now
- Leave Voice Channel:
,,leave
or,,dc
- Bot Token: Set in the
.env
file (see Setup). - Predefined Radio Streams: Modify the
PREDEFINED_STREAMS
dictionary inconfig.py
to add, remove, or change the default radio stations, their URLs, and descriptions. - Other Settings: Adjust constants like
COMMAND_PREFIX
,METADATA_FETCH_INTERVAL
, etc., directly inconfig.py
. Remember to rebuild the Docker image (docker-compose build
) if you changeconfig.py
.
- Start:
docker-compose up -d
- Stop:
docker-compose down
(Stops and removes the container) - Restart:
docker-compose restart
- View Logs:
docker-compose logs -f
(Follow logs in real-time. PressCtrl+C
to stop following). - Update Bot Code:
- Pull the latest changes if using Git (
git pull
). - Rebuild the image:
docker-compose build
- Restart the container:
docker-compose up -d
(Compose automatically replaces the old container with the one using the new image).
- Pull the latest changes if using Git (
Error loading state: Expecting value...
: This is normal on the first run or if thestate.json
file is empty/invalid. The bot will safely start with an empty state.- Bot Not Coming Online / Login Failed / Privileged Intents Required: Double-check your
DISCORD_TOKEN
in.env
. Ensure you enabled Server Members and Message Content intents in the Discord Developer Portal. Check logs (docker-compose logs -f
). - Slash Commands Not Appearing: Allow up to an hour for global commands to register after the bot first starts/syncs. If they persist in not showing, check logs for sync errors during startup.
- No Audio / Playback Issues: Verify the radio stream URLs are correct and working. Check bot logs for FFmpeg errors. Ensure the bot has
Connect
andSpeak
permissions in the voice channel. - Permission Errors (e.g., Cannot Add/Remove Reactions): Ensure the bot has the required permissions listed in the "Invite Your Bot" section (specifically
Add Reactions
andManage Messages
).
Contributions are welcome! Due to the modular Cog structure, adding features is straightforward:
- Create a New Cog: If adding a distinct set of features (e.g., Search, Favorites), create a new
.py
file in thecogs/
directory. - Develop: Write your Cog class inheriting from
commands.Cog
. Useself.bot
to access shared resources like thehttp_session
orguild_states
. - Add Setup Function: Include the standard
async def setup(bot): await bot.add_cog(YourCogName(bot))
at the end of your Cog file. - Submit a Pull Request: Explain your changes and features.
Please report any bugs or suggest features using the GitHub Issues tab.