Added repeat feature. Fixed the join when playing a song function.
This commit is contained in:
parent
d53887dd1a
commit
4f38a1a89e
36
audio.py
36
audio.py
@ -13,6 +13,7 @@ voice_clients = {} # Track active voice connections
|
|||||||
music_queues = {} # Per-guild song queue
|
music_queues = {} # Per-guild song queue
|
||||||
current_tracks = {} # Currently playing tracks
|
current_tracks = {} # Currently playing tracks
|
||||||
volumes = {} # Volume levels per guild
|
volumes = {} # Volume levels per guild
|
||||||
|
repeat_modes = {} # Tracks repeat mode per guild: "one", "all", or "off"
|
||||||
default_volume = 0.05 # Default volume (5%)
|
default_volume = 0.05 # Default volume (5%)
|
||||||
USER_DATA_FILE = 'user_data.json'
|
USER_DATA_FILE = 'user_data.json'
|
||||||
YOUTUBE_API_KEY = os.getenv("YOUTUBE_API_KEY") # Replace with your API key
|
YOUTUBE_API_KEY = os.getenv("YOUTUBE_API_KEY") # Replace with your API key
|
||||||
@ -52,7 +53,12 @@ async def join_voice(interaction: discord.Interaction):
|
|||||||
|
|
||||||
if guild_id not in voice_clients:
|
if guild_id not in voice_clients:
|
||||||
voice_clients[guild_id] = await channel.connect()
|
voice_clients[guild_id] = await channel.connect()
|
||||||
await interaction.followup.send(f"✅ **Joined {channel.name}.**")
|
|
||||||
|
# Ensure the response is completed
|
||||||
|
if interaction.response.is_done():
|
||||||
|
await interaction.followup.send(f"✅ **Joined {channel.name}.**")
|
||||||
|
else:
|
||||||
|
await interaction.response.send_message(f"✅ **Joined {channel.name}.**")
|
||||||
return voice_clients[guild_id]
|
return voice_clients[guild_id]
|
||||||
|
|
||||||
|
|
||||||
@ -61,13 +67,16 @@ async def play_audio(interaction: discord.Interaction, query: str):
|
|||||||
|
|
||||||
# Ensure Amber is connected to a voice channel
|
# Ensure Amber is connected to a voice channel
|
||||||
if guild_id not in voice_clients or not voice_clients[guild_id].is_connected():
|
if guild_id not in voice_clients or not voice_clients[guild_id].is_connected():
|
||||||
|
await interaction.response.defer() # Defer the response early
|
||||||
voice_client = await join_voice(interaction)
|
voice_client = await join_voice(interaction)
|
||||||
if voice_client is None: # If join failed
|
if voice_client is None: # If join failed
|
||||||
return
|
return
|
||||||
else:
|
else:
|
||||||
voice_client = voice_clients[guild_id]
|
voice_client = voice_clients[guild_id]
|
||||||
|
|
||||||
await interaction.response.defer()
|
# Defer response if not already done
|
||||||
|
if not interaction.response.is_done():
|
||||||
|
await interaction.response.defer()
|
||||||
|
|
||||||
# Search for the song on YouTube
|
# Search for the song on YouTube
|
||||||
ydl_opts = {
|
ydl_opts = {
|
||||||
@ -103,13 +112,30 @@ async def play_audio(interaction: discord.Interaction, query: str):
|
|||||||
async def play_next(interaction: discord.Interaction):
|
async def play_next(interaction: discord.Interaction):
|
||||||
guild_id = interaction.guild.id
|
guild_id = interaction.guild.id
|
||||||
|
|
||||||
if guild_id not in music_queues or not music_queues[guild_id]:
|
if guild_id not in music_queues:
|
||||||
await interaction.followup.send("❌ **No songs in the queue.**")
|
music_queues[guild_id] = []
|
||||||
|
|
||||||
|
# Handle Repeat One
|
||||||
|
if repeat_modes.get(guild_id) == "one" and current_tracks.get(guild_id):
|
||||||
|
song_url, title = current_tracks[guild_id]
|
||||||
|
music_queues[guild_id].insert(0, (song_url, title)) # Re-add the current song to the front of the queue
|
||||||
|
|
||||||
|
# Handle Repeat All
|
||||||
|
elif repeat_modes.get(guild_id) == "all" and not music_queues[guild_id]:
|
||||||
|
# Move completed songs back to the queue
|
||||||
|
song_url, title = current_tracks.get(guild_id, (None, None))
|
||||||
|
if song_url and title:
|
||||||
|
music_queues[guild_id].append((song_url, title))
|
||||||
|
music_queues[guild_id].extend(current_tracks.values())
|
||||||
|
|
||||||
|
# Proceed to play the next song
|
||||||
|
if not music_queues[guild_id]:
|
||||||
|
await interaction.followup.send("❌ **No more songs in the queue.**")
|
||||||
return
|
return
|
||||||
|
|
||||||
voice_client = voice_clients[guild_id]
|
voice_client = voice_clients[guild_id]
|
||||||
song_url, title = music_queues[guild_id].pop(0)
|
song_url, title = music_queues[guild_id].pop(0)
|
||||||
current_tracks[guild_id] = title
|
current_tracks[guild_id] = (song_url, title)
|
||||||
|
|
||||||
# Prepare FFmpeg options
|
# Prepare FFmpeg options
|
||||||
ffmpeg_options = {
|
ffmpeg_options = {
|
||||||
|
24
commands.py
24
commands.py
@ -1,6 +1,16 @@
|
|||||||
import discord
|
import discord
|
||||||
from discord import app_commands
|
from discord import app_commands
|
||||||
from audio import play_audio, stop_audio, set_volume, join_voice, leave_voice, pause_audio, resume_audio, generate_recommendations
|
from audio import (
|
||||||
|
play_audio,
|
||||||
|
stop_audio,
|
||||||
|
set_volume,
|
||||||
|
join_voice,
|
||||||
|
leave_voice,
|
||||||
|
pause_audio,
|
||||||
|
resume_audio,
|
||||||
|
repeat_modes,
|
||||||
|
generate_recommendations
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
async def setup_commands(client, guild_id=None):
|
async def setup_commands(client, guild_id=None):
|
||||||
@ -28,6 +38,18 @@ async def setup_commands(client, guild_id=None):
|
|||||||
async def resume(interaction: discord.Interaction):
|
async def resume(interaction: discord.Interaction):
|
||||||
await resume_audio(interaction)
|
await resume_audio(interaction)
|
||||||
|
|
||||||
|
@client.tree.command(name="repeat", description="Set the repeat mode for playback.")
|
||||||
|
@app_commands.describe(mode="Repeat mode: 'one', 'all', or 'off'")
|
||||||
|
async def repeat(interaction: discord.Interaction, mode: str):
|
||||||
|
guild_id = interaction.guild.id
|
||||||
|
valid_modes = ["one", "all", "off"]
|
||||||
|
|
||||||
|
if mode not in valid_modes:
|
||||||
|
await interaction.response.send_message(f"❌ **Invalid mode. Use one of: {', '.join(valid_modes)}.**")
|
||||||
|
return
|
||||||
|
|
||||||
|
repeat_modes[guild_id] = mode
|
||||||
|
await interaction.response.send_message(f"🔄 **Repeat mode set to:** {mode.capitalize()}.")
|
||||||
|
|
||||||
@client.tree.command(name="volume", description="Set playback volume.")
|
@client.tree.command(name="volume", description="Set playback volume.")
|
||||||
async def volume(interaction: discord.Interaction, level: int):
|
async def volume(interaction: discord.Interaction, level: int):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user