User Tools

Site Tools


musicbot

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
musicbot [2025/09/19 09:17] – [📂 Folder Structure] mikomusicbot [2025/09/23 09:25] (current) – [🔐 Insert Your Bot Token] miko
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: 
-<code>`bot.run("your-bot-token-here")`</code>+<code>`your-bot-token-here`</code>
 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]]
- +
-<code> +
-sudo adduser musikbot +
-sudo chown -R musikbot:musikbot /var/www/mbot +
-</code> +
-Autostart on server start +
-<code> +
-sudo nano /etc/systemd/system/musicbot.service +
-</code> +
- +
-<code> +
-[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 +
-</code> +
- +
-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 ===== +
-<code> +
-/var/www/mbot/ +
-├── bot.py +
-├── .env +
-├── music/ +
-│   â”œâ”€â”€ track1.mp3 +
-│   â”œâ”€â”€ track2.mp3 +
-</code> +
-===== 📂 bot.py ===== +
-<file bot.py> +
-import discord +
-from discord.ext import commands +
-import os +
-import random +
-from dotenv import load_dotenv +
- +
-# 🔐 Token laden +
-load_dotenv() +
-TOKEN = os.getenv("DISCORD_TOKEN") +
- +
-# 🎯 Gezielte Intents +
-intents = discord.Intents.default() +
-intents.message_content = True +
-intents.voice_states = True +
- +
-# 🤖 Bot-Setup +
-bot = commands.Bot(command_prefix="!", intents=intents, help_command=None) +
- +
-# 📁 Musikverzeichnis & Steuerung +
-MUSIC_DIR = "music" +
-playlist = [] +
-current_index = -1 +
-autoplay_enabled = True +
- +
-# 🚀 Bot ist bereit +
-@bot.event +
-async def on_ready(): +
-    print(f"🎵 Bot ist online als {bot.user}") +
- +
-# 🔁 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, autoplay_enabled +
-    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, playlist[current_index]) +
-    source = discord.FFmpegPCMAudio(filepath, options="-filter:a volume=1.0") +
-    vc.play(source, after=lambda e: play_next_track_wrapper(ctx)) +
- +
-    embed = discord.Embed( +
-        title="▶️ Nächster Track", +
-        description=playlist[current_index], +
-        color=discord.Color.blue() +
-    ) +
-    await ctx.send(embed=embed) +
- +
-# 🔀 Shuffle starten +
-async def start_shuffle(ctx): +
-    global playlist, current_index, autoplay_enabled +
-    vc = ctx.voice_client +
-    if not vc or not vc.is_connected(): +
-        await ctx.send("❌ Bot ist nicht im Sprachkanal.") +
-        return +
- +
-    autoplay_enabled = True +
-    playlist = [f for f in os.listdir(MUSIC_DIR) if f.endswith(".mp3")] +
-    if not playlist: +
-        await ctx.send("📁 Keine Musikdateien vorhanden.") +
-        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") +
-    vc.play(source, after=lambda e: play_next_track_wrapper(ctx)) +
- +
-    embed = discord.Embed( +
-        title="🔀 Shuffle gestartet", +
-        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"✅ Verbunden mit {channel.name}") +
-        await start_shuffle(ctx) +
-    else: +
-        await ctx.send("❌ Du bist in keinem Sprachkanal.") +
- +
-# 👋 Sprachkanal verlassen +
-@bot.command() +
-async def leave(ctx): +
-    if ctx.voice_client: +
-        await ctx.voice_client.disconnect() +
-        await ctx.send("👋 Bot hat den Sprachkanal verlassen.") +
-    else: +
-        await ctx.send("❌ Bot ist nicht verbunden.") +
- +
-# 📃 Liste der Musikdateien +
-@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("📁 Keine Musikdateien gefunden.") +
-    else: +
-        msg = "\n".join(f"{i+1}. {f}" for i, f in enumerate(files)) +
-        await ctx.send(f"🎶 VerfĂźgbare Tracks:\n{msg}") +
- +
-# ▶️ Musik abspielen +
-@bot.command() +
-async def play(ctx, filename=None): +
-    global playlist, current_index, autoplay_enabled +
-    vc = ctx.voice_client +
-    if not vc or not vc.is_connected(): +
-        await ctx.send("❌ Bot ist nicht im Sprachkanal. Nutze zuerst !join.") +
-        return +
- +
-    autoplay_enabled = True +
-    if filename: +
-        filepath = os.path.join(MUSIC_DIR, filename) +
-        if not os.path.isfile(filepath): +
-            await ctx.send("❌ Datei nicht gefunden.") +
-            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("📁 Keine Musikdateien vorhanden.") +
-            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") +
-    vc.play(source, after=lambda e: play_next_track_wrapper(ctx)) +
- +
-    embed = discord.Embed( +
-        title="▶️ Spiele", +
-        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("❌ Bot ist nicht im Sprachkanal.") +
-        return +
- +
-    autoplay_enabled = True +
-    vc.stop() +
-    await ctx.send("⏭️ Nächster Track wird gespielt.") +
- +
-# ⏹️ 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("⏹️ Wiedergabe gestoppt und Autoplay deaktiviert.") +
-    else: +
-        await ctx.send("❌ Keine Wiedergabe aktiv.") +
- +
-# ⏸️ Wiedergabe pausieren +
-@bot.command() +
-async def pause(ctx): +
-    vc = ctx.voice_client +
-    if vc and vc.is_playing(): +
-        vc.pause() +
-        await ctx.send("⏸️ Wiedergabe pausiert.") +
-    else: +
-        await ctx.send("❌ Keine Wiedergabe aktiv.") +
- +
-# ▶️ Wiedergabe fortsetzen +
-@bot.command() +
-async def resume(ctx): +
-    vc = ctx.voice_client +
-    if vc and vc.is_paused(): +
-        vc.resume() +
-        await ctx.send("▶️ Wiedergabe fortgesetzt.") +
-    else: +
-        await ctx.send("❌ Nichts zum Fortsetzen.") +
- +
-# ℹ️ Hilfe anzeigen +
-@bot.command() +
-async def help(ctx): +
-    embed = discord.Embed( +
-        title="🎵 Musikbot Befehle", +
-        color=discord.Color.purple() +
-    ) +
-    embed.add_field(name="!join", value="Bot tritt deinem Sprachkanal bei und startet Shuffle", inline=False) +
-    embed.add_field(name="!leave", value="Bot verlässt den Sprachkanal", inline=False) +
-    embed.add_field(name="!list", value="Zeigt alle verfĂźgbaren MP3-Dateien", inline=False) +
-    embed.add_field(name="!play [Dateiname]", value="Spielt eine bestimmte Datei oder zufällig", inline=False) +
-    embed.add_field(name="!next", value="Spielt den nächsten Track", inline=False) +
-    embed.add_field(name="!shuffle", value="Mischt die Playlist und startet Wiedergabe", inline=False) +
-    embed.add_field(name="!pause", value="Pausiert die Wiedergabe", inline=False) +
-    embed.add_field(name="!resume", value="Setzt die Wiedergabe fort", inline=False) +
-    embed.add_field(name="!stop", value="Stoppt die Wiedergabe und deaktiviert Autoplay", inline=False) +
-    await ctx.send(embed=embed) +
- +
-# 🚀 Bot starten +
-bot.run(TOKEN) +
-</file> +
-===== 🛟 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.1758273445.txt.gz ¡ Last modified: by miko

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki