FIX: Corrected an import issue in main.py

FEAT: Added a loop function to the music module.
This commit is contained in:
Dan 2024-08-14 13:52:20 -04:00
parent baade2c148
commit b18ab58876
2 changed files with 23 additions and 4 deletions

View File

@ -36,12 +36,12 @@ class Selena(discord.Client):
logging.info("Music module loaded") logging.info("Music module loaded")
if config['modules']['terms_privacy']['enabled']: if config['modules']['terms_privacy']['enabled']:
from modules.terms_privacy.terms_privacy import setup as terms_privacy_setup from modules.admin.terms_privacy import setup as terms_privacy_setup
terms_privacy_setup(self) terms_privacy_setup(self)
logging.info("Terms and Privacy module loaded") logging.info("Terms and Privacy module loaded")
if config['modules']['data_privacy']['enabled']: if config['modules']['data_privacy']['enabled']:
from modules.data_privacy.data_privacy import setup as data_privacy_setup from modules.admin.data_privacy import setup as data_privacy_setup
data_privacy_setup(self) data_privacy_setup(self)
logging.info("Data Privacy module loaded") logging.info("Data Privacy module loaded")

View File

@ -19,13 +19,14 @@ FFMPEG_OPTIONS = {
ytdl = YoutubeDL(YTDL_OPTIONS) ytdl = YoutubeDL(YTDL_OPTIONS)
class Music(commands.Cog): class Music(commands.Cog):
def __init__(self, bot): def __init__(self, bot):
self.bot = bot self.bot = bot
self.queue = [] self.queue = []
self.is_playing = False self.is_playing = False
self.volume = 0.3 self.volume = 0.3
self.loop = False # Initialize loop state
self.current_song = None # Track the current song
async def join(self, interaction: discord.Interaction): async def join(self, interaction: discord.Interaction):
channel = interaction.user.voice.channel channel = interaction.user.voice.channel
@ -53,14 +54,21 @@ class Music(commands.Cog):
self.queue.append((url, info['title'])) self.queue.append((url, info['title']))
await interaction.followup.send(f'Queued: {info["title"]}') await interaction.followup.send(f'Queued: {info["title"]}')
else: else:
self.current_song = (url, info['title'])
source = discord.PCMVolumeTransformer(discord.FFmpegPCMAudio(url, **FFMPEG_OPTIONS), volume=self.volume) source = discord.PCMVolumeTransformer(discord.FFmpegPCMAudio(url, **FFMPEG_OPTIONS), volume=self.volume)
interaction.guild.voice_client.play(source, after=lambda e: self.bot.loop.create_task(self.play_next(interaction))) interaction.guild.voice_client.play(source, after=lambda e: self.bot.loop.create_task(self.play_next(interaction)))
self.is_playing = True self.is_playing = True
await interaction.followup.send(f'Playing: {info["title"]}') await interaction.followup.send(f'Playing: {info["title"]}')
async def play_next(self, interaction: discord.Interaction): async def play_next(self, interaction: discord.Interaction):
if self.queue: if self.loop and self.current_song: # If loop is active, repeat the current song
url, title = self.current_song
source = discord.PCMVolumeTransformer(discord.FFmpegPCMAudio(url, **FFMPEG_OPTIONS), volume=self.volume)
interaction.guild.voice_client.play(source, after=lambda e: self.bot.loop.create_task(self.play_next(interaction)))
await interaction.followup.send(f'Repeating: {title}')
elif self.queue:
url, title = self.queue.pop(0) url, title = self.queue.pop(0)
self.current_song = (url, title)
source = discord.PCMVolumeTransformer(discord.FFmpegPCMAudio(url, **FFMPEG_OPTIONS), volume=self.volume) source = discord.PCMVolumeTransformer(discord.FFmpegPCMAudio(url, **FFMPEG_OPTIONS), volume=self.volume)
interaction.guild.voice_client.play(source, after=lambda e: self.bot.loop.create_task(self.play_next(interaction))) interaction.guild.voice_client.play(source, after=lambda e: self.bot.loop.create_task(self.play_next(interaction)))
await interaction.followup.send(f'Playing next: {title}') await interaction.followup.send(f'Playing next: {title}')
@ -85,6 +93,7 @@ class Music(commands.Cog):
if interaction.guild.voice_client.is_playing(): if interaction.guild.voice_client.is_playing():
interaction.guild.voice_client.stop() interaction.guild.voice_client.stop()
self.queue = [] self.queue = []
self.current_song = None
await interaction.followup.send("Stopped the song.") await interaction.followup.send("Stopped the song.")
else: else:
await interaction.followup.send("No song is currently playing.") await interaction.followup.send("No song is currently playing.")
@ -97,6 +106,11 @@ class Music(commands.Cog):
else: else:
await interaction.followup.send("No audio source found.") await interaction.followup.send("No audio source found.")
async def toggle_loop(self, interaction: discord.Interaction):
self.loop = not self.loop # Toggle the loop state
state = "enabled" if self.loop else "disabled"
await interaction.followup.send(f"Loop has been {state}.")
def setup(self, tree: discord.app_commands.CommandTree): def setup(self, tree: discord.app_commands.CommandTree):
@tree.command(name="join", description="Join the voice channel") @tree.command(name="join", description="Join the voice channel")
async def join_command(interaction: discord.Interaction): async def join_command(interaction: discord.Interaction):
@ -133,6 +147,11 @@ class Music(commands.Cog):
await interaction.response.defer() await interaction.response.defer()
await self.set_volume(interaction, volume) await self.set_volume(interaction, volume)
@tree.command(name="loop", description="Toggle loop for the current song")
async def loop_command(interaction: discord.Interaction):
await interaction.response.defer()
await self.toggle_loop(interaction)
def setup(bot): def setup(bot):
music = Music(bot) music = Music(bot)