55 lines
1.8 KiB
Python
55 lines
1.8 KiB
Python
import discord
|
|
from discord.ext import commands
|
|
import asyncio
|
|
import os
|
|
from fuzzywuzzy import process
|
|
|
|
intents = discord.Intents.default()
|
|
intents.message_content = True
|
|
intents.voice_states = True
|
|
|
|
bot = commands.Bot(command_prefix='!', intents=intents)
|
|
|
|
def find_song(query):
|
|
# Define the base directory
|
|
base_dir = "songs/"
|
|
|
|
# Initialize variables to store the best match and confidence
|
|
best_match = None
|
|
best_confidence = 0
|
|
|
|
# Recursively search through directories for song files
|
|
for root, dirs, files in os.walk(base_dir):
|
|
for file in files:
|
|
if file.endswith(".mp3"):
|
|
song_path = os.path.join(root, file)
|
|
song_name = os.path.splitext(file)[0] # Get the song name without extension
|
|
confidence = process.extractOne(query, [song_name])[1]
|
|
|
|
# Update best match if confidence is higher
|
|
if confidence > best_confidence:
|
|
best_match = song_path
|
|
best_confidence = confidence
|
|
|
|
return best_match, best_confidence
|
|
|
|
@bot.command()
|
|
async def play(ctx, *, query):
|
|
voice_channel = ctx.author.voice.channel
|
|
if voice_channel:
|
|
voice_client = await voice_channel.connect()
|
|
|
|
# Find the closest match to the query
|
|
song_path, confidence = find_song(query)
|
|
|
|
if confidence >= 70: # Adjust confidence threshold as needed
|
|
voice_client.play(discord.FFmpegPCMAudio(song_path))
|
|
while voice_client.is_playing():
|
|
await asyncio.sleep(1)
|
|
await voice_client.disconnect()
|
|
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!")
|
|
|
|
bot.run('TOKEN')
|