From 551dec6c08ba4d7f8d32314dc768ca2cf125e2d5 Mon Sep 17 00:00:00 2001 From: tavo-wasd Date: Sun, 31 Mar 2024 12:06:33 -0600 Subject: [PATCH] test stop --- main.py | 39 +++++++++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/main.py b/main.py index bf9e985..9cf01e8 100644 --- a/main.py +++ b/main.py @@ -11,6 +11,7 @@ intents.message_content = True intents.voice_states = True bot = commands.Bot(command_prefix='!', intents=intents) queued_songs = [] +voice_client = None # Global variable to track voice client def find_song(query, songs_dir): best_match = None @@ -20,8 +21,7 @@ def find_song(query, songs_dir): for file in files: if file.endswith(".mp3"): song_path = os.path.join(root, file) - #song_name = os.path.relpath(song_path, start=songs_dir) - song_name = os.path.splitext(file)[0] + song_name = os.path.relpath(song_path, start=songs_dir) confidence = process.extractOne(query, [song_name.replace("_", " ")])[1] if confidence > best_confidence: @@ -32,13 +32,31 @@ def find_song(query, songs_dir): @bot.command() async def play(ctx, *, query): - voice_client = discord.utils.get(bot.voice_clients, guild=ctx.guild) + global voice_client if voice_client and voice_client.is_connected(): await add_to_queue(ctx, query) else: await connect_and_play(ctx, query) +@bot.command() +async def skip(ctx): + global voice_client + if voice_client and voice_client.is_playing(): + voice_client.stop() + +@bot.command() +async def stop(ctx): + global voice_client + if voice_client and voice_client.is_connected(): + queued_songs.clear() + if voice_client.is_playing(): + voice_client.stop() + await voice_client.disconnect() + voice_client = None + await ctx.send("Stopped playback and cleared queue.") + async def add_to_queue(ctx, query): + global voice_client songs_dir = os.getenv("SONGS_DIR") # Find the closest match to the query song_path, confidence = find_song(query, songs_dir) @@ -48,9 +66,10 @@ async def add_to_queue(ctx, query): queued_songs.append((song_path, song_name)) await ctx.send(f"**Added to queue:** `{song_name}`") else: - await ctx.send("Sorry, couldn't find a match for that song.") + await ctx.send("Can't find a match for that song.") async def connect_and_play(ctx, query): + global voice_client voice_channel = ctx.author.voice.channel if voice_channel: songs_dir = os.getenv("SONGS_DIR") @@ -60,20 +79,20 @@ async def connect_and_play(ctx, query): if confidence >= 70 and song_path: # Adjust confidence threshold as needed song_name = os.path.relpath(song_path, start=songs_dir) # Get the song name relative to songs_dir queued_songs.append((song_path, song_name)) - await play_queued_songs(ctx, voice_channel) + voice_client = await play_queued_songs(ctx, voice_channel) else: - await ctx.send("Sorry, couldn't find a match for that song.") - else: - await ctx.send("You need to be in a voice channel to use this command!") + await ctx.send("Can't find a match for that song.") async def play_queued_songs(ctx, voice_channel): + global voice_client voice_client = await voice_channel.connect() while queued_songs: song_path, song_name = queued_songs.pop(0) # Get the first song in the queue await ctx.send(f"**Now playing:** `{song_name}`") voice_client.play(discord.FFmpegPCMAudio(song_path)) - while voice_client.is_playing(): + while voice_client and voice_client.is_playing(): # Check if voice_client is not None await asyncio.sleep(1) - await voice_client.disconnect() + if voice_client: + await voice_client.disconnect() bot.run(os.getenv("DISCORD_TOKEN"))