queue
This commit is contained in:
parent
8829c6e360
commit
a00a006995
4 changed files with 49 additions and 15 deletions
2
.env.example
Normal file
2
.env.example
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
DISCORD_TOKEN=abcdefg
|
||||||
|
SONGS_DIR=/path/to/songs
|
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -2,3 +2,4 @@ bin/
|
||||||
lib/
|
lib/
|
||||||
lib64
|
lib64
|
||||||
pyvenv.cfg
|
pyvenv.cfg
|
||||||
|
.env
|
||||||
|
|
|
@ -6,4 +6,5 @@
|
||||||
pip install discord ;
|
pip install discord ;
|
||||||
pip install PyNaCl ;
|
pip install PyNaCl ;
|
||||||
pip install fuzzywuzzy ;
|
pip install fuzzywuzzy ;
|
||||||
|
pip install python-dotenv ;
|
||||||
)
|
)
|
||||||
|
|
60
main.py
60
main.py
|
@ -3,6 +3,9 @@ from discord.ext import commands
|
||||||
import asyncio
|
import asyncio
|
||||||
import os
|
import os
|
||||||
from fuzzywuzzy import process
|
from fuzzywuzzy import process
|
||||||
|
from dotenv import load_dotenv
|
||||||
|
|
||||||
|
load_dotenv() # Load environment variables from .env file
|
||||||
|
|
||||||
intents = discord.Intents.default()
|
intents = discord.Intents.default()
|
||||||
intents.message_content = True
|
intents.message_content = True
|
||||||
|
@ -10,20 +13,17 @@ intents.voice_states = True
|
||||||
|
|
||||||
bot = commands.Bot(command_prefix='!', intents=intents)
|
bot = commands.Bot(command_prefix='!', intents=intents)
|
||||||
|
|
||||||
def find_song(query):
|
def find_song(query, songs_dir):
|
||||||
# Define the base directory
|
|
||||||
base_dir = "songs/"
|
|
||||||
|
|
||||||
# Initialize variables to store the best match and confidence
|
# Initialize variables to store the best match and confidence
|
||||||
best_match = None
|
best_match = None
|
||||||
best_confidence = 0
|
best_confidence = 0
|
||||||
|
|
||||||
# Recursively search through directories for song files
|
# Recursively search through directories for song files
|
||||||
for root, dirs, files in os.walk(base_dir):
|
for root, dirs, files in os.walk(songs_dir):
|
||||||
for file in files:
|
for file in files:
|
||||||
if file.endswith(".mp3"):
|
if file.endswith(".mp3"):
|
||||||
song_path = os.path.join(root, file)
|
song_path = os.path.join(root, file)
|
||||||
song_name = os.path.splitext(file)[0] # Get the song name without extension
|
song_name = os.path.relpath(song_path, start=songs_dir) # Get the song name relative to songs_dir
|
||||||
confidence = process.extractOne(query, [song_name])[1]
|
confidence = process.extractOne(query, [song_name])[1]
|
||||||
|
|
||||||
# Update best match if confidence is higher
|
# Update best match if confidence is higher
|
||||||
|
@ -33,23 +33,53 @@ def find_song(query):
|
||||||
|
|
||||||
return best_match, best_confidence
|
return best_match, best_confidence
|
||||||
|
|
||||||
|
# Define a global list to store the queued songs
|
||||||
|
queued_songs = []
|
||||||
|
|
||||||
@bot.command()
|
@bot.command()
|
||||||
async def play(ctx, *, query):
|
async def play(ctx, *, query):
|
||||||
|
voice_client = discord.utils.get(bot.voice_clients, guild=ctx.guild)
|
||||||
|
if voice_client and voice_client.is_connected():
|
||||||
|
await add_to_queue(ctx, query)
|
||||||
|
else:
|
||||||
|
await connect_and_play(ctx, query)
|
||||||
|
|
||||||
|
async def add_to_queue(ctx, query):
|
||||||
|
songs_dir = os.getenv("SONGS_DIR")
|
||||||
|
# Find the closest match to the query
|
||||||
|
song_path, confidence = find_song(query, songs_dir)
|
||||||
|
|
||||||
|
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 ctx.send(f"Added to queue: {song_name}")
|
||||||
|
else:
|
||||||
|
await ctx.send("Sorry, couldn't find a match for that song.")
|
||||||
|
|
||||||
|
async def connect_and_play(ctx, query):
|
||||||
voice_channel = ctx.author.voice.channel
|
voice_channel = ctx.author.voice.channel
|
||||||
if voice_channel:
|
if voice_channel:
|
||||||
voice_client = await voice_channel.connect()
|
songs_dir = os.getenv("SONGS_DIR")
|
||||||
|
|
||||||
# Find the closest match to the query
|
# Find the closest match to the query
|
||||||
song_path, confidence = find_song(query)
|
song_path, confidence = find_song(query, songs_dir)
|
||||||
|
|
||||||
if confidence >= 70: # Adjust confidence threshold as needed
|
if confidence >= 70 and song_path: # Adjust confidence threshold as needed
|
||||||
voice_client.play(discord.FFmpegPCMAudio(song_path))
|
song_name = os.path.relpath(song_path, start=songs_dir) # Get the song name relative to songs_dir
|
||||||
while voice_client.is_playing():
|
queued_songs.append((song_path, song_name))
|
||||||
await asyncio.sleep(1)
|
await play_queued_songs(ctx, voice_channel)
|
||||||
await voice_client.disconnect()
|
|
||||||
else:
|
else:
|
||||||
await ctx.send("Sorry, couldn't find a match for that song.")
|
await ctx.send("Sorry, couldn't find a match for that song.")
|
||||||
else:
|
else:
|
||||||
await ctx.send("You need to be in a voice channel to use this command!")
|
await ctx.send("You need to be in a voice channel to use this command!")
|
||||||
|
|
||||||
bot.run('TOKEN')
|
async def play_queued_songs(ctx, voice_channel):
|
||||||
|
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():
|
||||||
|
await asyncio.sleep(1)
|
||||||
|
await voice_client.disconnect()
|
||||||
|
|
||||||
|
bot.run(os.getenv("DISCORD_TOKEN"))
|
||||||
|
|
Loading…
Reference in a new issue