musicbot
This is an old revision of the document!
Table of Contents
π΅ Ambient Lo-Fi Discord Music Bot β Installation & Usage
This bot plays ambient lo-fi music automatically in your Discord voice channel. It starts with shuffle, continues playing without stopping, and can be controlled via simple commands.
π¦ Requirements
- Python 3.10 or newer
- FFmpeg installed (`sudo apt install ffmpeg`)
- A Discord Bot Token
- MP3 files placed in the `music/` directory
- Optional: systemd for autostart on boot
π οΈ Installation
- Clone or copy the bot code into a directory, e.g. `/var/www/mbot/`
- Create a `music/` folder and add your MP3 tracks
- Install required Python packages:
`pip install discord`
π Insert Your Bot Token
Open `bot.py` and replace: `bot.run(βyour-bot-token-hereβ)` with your actual Discord bot token.
π Starting the Bot
To start manually: `python3 ./bot.py`
Or set up a systemd service:
sudo adduser musikbot sudo chown -R musikbot:musikbot /var/www/mbot
Autostart on server start
sudo nano /etc/systemd/system/musicbot.service
[Unit] Description=Discord Music Bot After=network.target [Service] ExecStart=/usr/bin/python3 /var/www/mbot/bot.py WorkingDirectory=/var/www/mbot Restart=always User=musikbot [Install] WantedBy=multi-user.target
Enable and start the service: `sudo systemctl daemon-reload` `sudo systemctl enable musicbot` `sudo systemctl start musicbot`
π§ Discord Commands
- `!join` β Bot joins your voice channel and starts shuffle playback
- `!leave` β Bot leaves the voice channel
- `!list` β Lists all available MP3 files
- `!play [filename]` β Plays a specific file or a random one if none is given
- `!next` β Skips to the next track
- `!shuffle` β Shuffles the playlist and starts playback
- `!pause` β Pauses playback
- `!resume` β Resumes playback
- `!stop` β Stops playback
- `!help` β Displays all available commands
π§ Notes
- The bot plays continuously without stopping after each track
- Volume is set to 100%
- Make sure the bot has βSpeakβ permissions in the voice channel
- Works best in standard voice channels (not Stage Channels)
π Folder Structure
/var/www/mbot/ βββ bot.py βββ music/ β βββ track1.mp3 β βββ track2.mp3
import discord from discord.ext import commands import os import random intents = discord.Intents.default() intents.message_content = True intents.members = True intents.guilds = True bot = commands.Bot(command_prefix="!", intents=intents, help_command=None) MUSIC_DIR = "music" playlist = [] current_index = -1 @bot.event async def on_ready(): print(f"π΅ Bot is online as {bot.user}") def play_next_track(ctx): global playlist, current_index vc = ctx.voice_client if not vc or not playlist: return current_index = (current_index + 1) % len(playlist) filepath = os.path.join(MUSIC_DIR, playlist[current_index]) source = discord.FFmpegPCMAudio(filepath, options="-filter:a volume=1.0") def after_playing(error): if error: print(f"Playback error: {error}") else: play_next_track(ctx) vc.play(source, after=after_playing) async def start_shuffle(ctx): global playlist, current_index vc = ctx.voice_client playlist = [f for f in os.listdir(MUSIC_DIR) if f.endswith(".mp3")] if not playlist: await ctx.send("π No music files found.") return random.shuffle(playlist) current_index = 0 filepath = os.path.join(MUSIC_DIR, playlist[current_index]) source = discord.FFmpegPCMAudio(filepath, options="-filter:a volume=1.0") def after_playing(error): if error: print(f"Playback error: {error}") else: play_next_track(ctx) vc.stop() vc.play(source, after=after_playing) await ctx.send(f"π Shuffle started: {playlist[current_index]}") @bot.command() async def join(ctx): member = ctx.guild.get_member(ctx.author.id) if member and member.voice and member.voice.channel: channel = member.voice.channel await channel.connect() await ctx.send(f"β Connected to {channel.name}") await start_shuffle(ctx) else: await ctx.send("β You're not in a voice channel.") @bot.command() async def leave(ctx): if ctx.voice_client: await ctx.voice_client.disconnect() await ctx.send("π Bot has left the voice channel.") else: await ctx.send("β Bot is not connected.") @bot.command() async def list(ctx): files = [f for f in os.listdir(MUSIC_DIR) if f.endswith(".mp3")] if not files: await ctx.send("π No music files found.") else: msg = "\n".join(f"{i+1}. {f}" for i, f in enumerate(files)) await ctx.send(f"πΆ Available Tracks:\n{msg}") @bot.command() async def play(ctx, filename=None): vc = ctx.voice_client global playlist, current_index if not vc: await ctx.send("β Bot is not in a voice channel. Use !join first.") return if filename: filepath = os.path.join(MUSIC_DIR, filename) if not os.path.isfile(filepath): await ctx.send("β File not found.") return playlist = [filename] current_index = 0 else: playlist = [f for f in os.listdir(MUSIC_DIR) if f.endswith(".mp3")] if not playlist: await ctx.send("π No music files found.") return current_index = random.randint(0, len(playlist) - 1) filepath = os.path.join(MUSIC_DIR, playlist[current_index]) source = discord.FFmpegPCMAudio(filepath, options="-filter:a volume=1.0") def after_playing(error): if error: print(f"Playback error: {error}") else: play_next_track(ctx) vc.stop() vc.play(source, after=after_playing) await ctx.send(f"βΆοΈ Now playing: {playlist[current_index]}") @bot.command() async def next(ctx): if ctx.voice_client: play_next_track(ctx) await ctx.send("βοΈ Skipping to next track.") else: await ctx.send("β Bot is not in a voice channel.") @bot.command() async def shuffle(ctx): if ctx.voice_client: await start_shuffle(ctx) else: await ctx.send("β Bot is not in a voice channel.") @bot.command() async def pause(ctx): if ctx.voice_client and ctx.voice_client.is_playing(): ctx.voice_client.pause() await ctx.send("βΈοΈ Playback paused.") else: await ctx.send("β Nothing is playing.") @bot.command() async def resume(ctx): if ctx.voice_client and ctx.voice_client.is_paused(): ctx.voice_client.resume() await ctx.send("βΆοΈ Playback resumed.") else: await ctx.send("β Nothing to resume.") @bot.command() async def stop(ctx): if ctx.voice_client and ctx.voice_client.is_playing(): ctx.voice_client.stop() await ctx.send("βΉοΈ Playback stopped.") else: await ctx.send("β Nothing is playing.") @bot.command() async def help(ctx): help_text = ( "**π΅ Music Bot Commands:**\n" "`!join` β Bot joins your voice channel and starts shuffle\n" "`!leave` β Bot leaves the voice channel\n" "`!list` β Lists all available MP3 files\n" "`!play [filename]` β Plays a specific file or random if none given\n" "`!next` β Plays the next track\n" "`!shuffle` β Shuffles the playlist and starts playback\n" "`!pause` β Pauses playback\n" "`!resume` β Resumes playback\n" "`!stop` β Stops playback\n" ) await ctx.send(help_text) bot.run("YOUR_BOT_TOKEN_HERE")
π Support & Extensions
You can expand the bot anytime β with volume control, queue management, or even a web interface. For questions or ideas, feel free to reach out!
musicbot.1758238363.txt.gz Β· Last modified: by miko