musicbot
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| musicbot [2025/09/19 09:16] â [đ bot.py] miko | musicbot [2025/11/25 16:37] (current) â external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== đľ Ambient Lo-Fi Discord Music Bot â Installation & Usage ====== | + | ====== đľ 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. | 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. | ||
| Line 17: | Line 17: | ||
| ===== đ Insert Your Bot Token ===== | ===== đ Insert Your Bot Token ===== | ||
| - | Open `bot.py` and replace:Â | + | Open `.env` and replace:Â |
| - | < | + | < |
| with your actual Discord bot token. | with your actual Discord bot token. | ||
| Line 41: | Line 41: | ||
| Copy the generated URL and open it in your browser to invite the bot. | Copy the generated URL and open it in your browser to invite the bot. | ||
| - | ===== đ Starting the Bot =====Â | + | ===== đ Bot =====Â |
| - | To start manually:Â | + | Now available on github! |
| - | `python3 ./bot.py` | + | |
| - | Or set up a systemd service:Â | + | [[https://github.com/JoranJix/discord-musicbot|discord-musicbot]] |
| - | Â | + | |
| - | < | + | |
| - | sudo adduser musikbot | + | |
| - | sudo chown -R musikbot: | + | |
| - | </ | + | |
| - | Autostart on server start | + | |
| - | < | + | |
| - | sudo nano / | + | |
| - | </ | + | |
| - | Â | + | |
| - | < | + | |
| - | [Unit]Â | + | |
| - | Description=Discord Music Bot | + | |
| - | After=network.target | + | |
| - | Â | + | |
| - | [Service]Â | + | |
| - | ExecStart=/ | + | |
| - | WorkingDirectory=/ | + | |
| - | 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/Â | + | |
| - | â | + | |
| - | â | + | |
| - | </code>Â | + | |
| - | ===== đ bot.py =====Â | + | |
| - | <file bot.py> | + | |
| - | import | + | |
| - | from discord.ext import commands | + | |
| - | import os | + | |
| - | import random | + | |
| - | from dotenv import load_dotenv | + | |
| - | Â | + | |
| - | # đ Token laden | + | |
| - | load_dotenv()Â | + | |
| - | TOKEN = os.getenv(" | + | |
| - | Â | + | |
| - | # đŻ Gezielte Intents | + | |
| - | intents = discord.Intents.default()Â | + | |
| - | intents.message_content = True | + | |
| - | intents.voice_states = True | + | |
| - | Â | + | |
| - | # đ¤ Bot-Setup | + | |
| - | bot = commands.Bot(command_prefix=" | + | |
| - | Â | + | |
| - | # đ Musikverzeichnis & Steuerung | + | |
| - | MUSIC_DIR = " | + | |
| - | playlist = []Â | + | |
| - | current_index = -1Â | + | |
| - | autoplay_enabled = True | + | |
| - | Â | + | |
| - | # đ Bot ist bereit | + | |
| - | @bot.event | + | |
| - | async def on_ready(): | + | |
| - | print(f" | + | |
| - | Â | + | |
| - | # đ Wrapper fĂźr asynchrone Wiedergabe | + | |
| - | def play_next_track_wrapper(ctx): | + | |
| - | async def inner():Â | + | |
| - | await play_next_track(ctx)Â | + | |
| - | bot.loop.create_task(inner())Â | + | |
| - | Â | + | |
| - | # âśď¸ Nächsten Track abspielen | + | |
| - | async def play_next_track(ctx): | + | |
| - | global playlist, current_index, | + | |
| - | vc = ctx.voice_client | + | |
| - | if not vc or not vc.is_connected() or not playlist or not autoplay_enabled: | + | |
| - | return | + | |
| - | Â | + | |
| - | current_index = (current_index + 1) % len(playlist)Â | + | |
| - | filepath = os.path.join(MUSIC_DIR, | + | |
| - | source = discord.FFmpegPCMAudio(filepath, | + | |
| - | vc.play(source, | + | |
| - | Â | + | |
| - | embed = discord.Embed(Â | + | |
| - | title=" | + | |
| - | description=playlist[current_index], | + | |
| - | color=discord.Color.blue()Â | + | |
| - | )Â | + | |
| - | await ctx.send(embed=embed)Â | + | |
| - | Â | + | |
| - | # đ Shuffle starten | + | |
| - | async def start_shuffle(ctx): | + | |
| - | global playlist, current_index, | + | |
| - | vc = ctx.voice_client | + | |
| - | if not vc or not vc.is_connected(): | + | |
| - | await ctx.send(" | + | |
| - | return | + | |
| - | Â | + | |
| - | autoplay_enabled = True | + | |
| - | playlist = [f for f in os.listdir(MUSIC_DIR) if f.endswith(" | + | |
| - | if not playlist:Â | + | |
| - | await ctx.send(" | + | |
| - | return | + | |
| - | Â | + | |
| - | random.shuffle(playlist)Â | + | |
| - | current_index = 0Â | + | |
| - | filepath = os.path.join(MUSIC_DIR, | + | |
| - | source = discord.FFmpegPCMAudio(filepath, | + | |
| - | vc.play(source, | + | |
| - | Â | + | |
| - | embed = discord.Embed(Â | + | |
| - | title=" | + | |
| - | description=playlist[current_index], | + | |
| - | color=discord.Color.green()Â | + | |
| - | )Â | + | |
| - | await ctx.send(embed=embed)Â | + | |
| - | Â | + | |
| - | # đĄ Sprachkanal beitreten | + | |
| - | @bot.command()Â | + | |
| - | async def join(ctx): | + | |
| - | member = ctx.author | + | |
| - | if member.voice and member.voice.channel: | + | |
| - | channel = member.voice.channel | + | |
| - | if not ctx.voice_client: | + | |
| - | await channel.connect()Â | + | |
| - | await ctx.send(f" | + | |
| - | await start_shuffle(ctx)Â | + | |
| - | else:Â | + | |
| - | await ctx.send(" | + | |
| - | Â | + | |
| - | # đ Sprachkanal verlassen | + | |
| - | @bot.command()Â | + | |
| - | async def leave(ctx): | + | |
| - | if ctx.voice_client: | + | |
| - | await ctx.voice_client.disconnect()Â | + | |
| - | await ctx.send(" | + | |
| - | else:Â | + | |
| - | await ctx.send(" | + | |
| - | Â | + | |
| - | # đ Liste der Musikdateien | + | |
| - | @bot.command()Â | + | |
| - | async def list(ctx): | + | |
| - | files = [f for f in os.listdir(MUSIC_DIR) if f.endswith(" | + | |
| - | if not files:Â | + | |
| - | await ctx.send(" | + | |
| - | else:Â | + | |
| - | msg = " | + | |
| - | await ctx.send(f" | + | |
| - | Â | + | |
| - | # âśď¸ Musik abspielen | + | |
| - | @bot.command()Â | + | |
| - | async def play(ctx, filename=None): | + | |
| - | global playlist, current_index, | + | |
| - | vc = ctx.voice_client | + | |
| - | if not vc or not vc.is_connected(): | + | |
| - | await ctx.send(" | + | |
| - | return | + | |
| - | Â | + | |
| - | autoplay_enabled = True | + | |
| - | if filename:Â | + | |
| - | filepath = os.path.join(MUSIC_DIR, | + | |
| - | if not os.path.isfile(filepath): | + | |
| - | await ctx.send(" | + | |
| - | return | + | |
| - | playlist = [filename]Â | + | |
| - | current_index = 0Â | + | |
| - | else:Â | + | |
| - | playlist = [f for f in os.listdir(MUSIC_DIR) if f.endswith(" | + | |
| - | if not playlist:Â | + | |
| - | await ctx.send(" | + | |
| - | return | + | |
| - | current_index = random.randint(0, | + | |
| - | Â | + | |
| - | filepath = os.path.join(MUSIC_DIR, | + | |
| - | source = discord.FFmpegPCMAudio(filepath, | + | |
| - | vc.play(source, | + | |
| - | Â | + | |
| - | embed = discord.Embed(Â | + | |
| - | title=" | + | |
| - | description=playlist[current_index], | + | |
| - | color=discord.Color.orange()Â | + | |
| - | )Â | + | |
| - | await ctx.send(embed=embed)Â | + | |
| - | Â | + | |
| - | # âď¸ Nächsten Track manuell starten | + | |
| - | @bot.command()Â | + | |
| - | async def next(ctx): | + | |
| - | global autoplay_enabled | + | |
| - | vc = ctx.voice_client | + | |
| - | if not vc or not vc.is_connected(): | + | |
| - | await ctx.send(" | + | |
| - | return | + | |
| - | Â | + | |
| - | autoplay_enabled = True | + | |
| - | vc.stop()Â | + | |
| - | await ctx.send(" | + | |
| - | Â | + | |
| - | # âšď¸ Wiedergabe stoppen und Autoplay deaktivieren | + | |
| - | @bot.command()Â | + | |
| - | async def stop(ctx): | + | |
| - | global autoplay_enabled | + | |
| - | vc = ctx.voice_client | + | |
| - | if vc and vc.is_playing(): | + | |
| - | autoplay_enabled = False | + | |
| - | vc.stop()Â | + | |
| - | await ctx.send(" | + | |
| - | else:Â | + | |
| - | await ctx.send(" | + | |
| - | Â | + | |
| - | # â¸ď¸ Wiedergabe pausieren | + | |
| - | @bot.command()Â | + | |
| - | async def pause(ctx): | + | |
| - | vc = ctx.voice_client | + | |
| - | if vc and vc.is_playing(): | + | |
| - | vc.pause()Â | + | |
| - | await ctx.send(" | + | |
| - | else:Â | + | |
| - | await ctx.send(" | + | |
| - | Â | + | |
| - | # âśď¸ Wiedergabe fortsetzen | + | |
| - | @bot.command()Â | + | |
| - | async def resume(ctx): | + | |
| - | vc = ctx.voice_client | + | |
| - | if vc and vc.is_paused(): | + | |
| - | vc.resume()Â | + | |
| - | await ctx.send(" | + | |
| - | else:Â | + | |
| - | await ctx.send(" | + | |
| - | Â | + | |
| - | # âšď¸ Hilfe anzeigen | + | |
| - | @bot.command()Â | + | |
| - | async def help(ctx): | + | |
| - | embed = discord.Embed(Â | + | |
| - | title=" | + | |
| - | color=discord.Color.purple()Â | + | |
| - | )Â | + | |
| - | embed.add_field(name=" | + | |
| - | embed.add_field(name=" | + | |
| - | embed.add_field(name=" | + | |
| - | embed.add_field(name=" | + | |
| - | embed.add_field(name=" | + | |
| - | embed.add_field(name=" | + | |
| - | embed.add_field(name=" | + | |
| - | embed.add_field(name=" | + | |
| - | embed.add_field(name=" | + | |
| - | await ctx.send(embed=embed)Â | + | |
| - | Â | + | |
| - | # đ Bot starten | + | |
| - | bot.run(TOKEN)Â | + | |
| - | </ | + | |
| - | ===== đ 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.1758273418.txt.gz ¡ Last modified: (external edit)
