FIX: Added Volume Control, Set to 25% by default

This commit is contained in:
Dan 2024-06-25 22:36:18 -04:00
parent f75564760e
commit 7f5aeb04c3

View File

@ -10,7 +10,7 @@ class Music:
self.bot = bot self.bot = bot
self.logger = logging.getLogger('Music') self.logger = logging.getLogger('Music')
self.logger.setLevel(logging.DEBUG) self.logger.setLevel(logging.DEBUG)
handler = logging.FileHandler(filename='music.log', encoding='utf-8', mode='w') handler = logging.FileHandler(filename='log/selena.log', encoding='utf-8', mode='w')
handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s:%(message)s')) handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s:%(message)s'))
self.logger.addHandler(handler) self.logger.addHandler(handler)
self.ydl_opts = { self.ydl_opts = {
@ -22,6 +22,7 @@ class Music:
}], }],
'quiet': True 'quiet': True
} }
self.volume = 0.25 # Default volume (25%)
async def search_youtube(self, query): async def search_youtube(self, query):
with youtube_dl.YoutubeDL(self.ydl_opts) as ydl: with youtube_dl.YoutubeDL(self.ydl_opts) as ydl:
@ -82,7 +83,7 @@ class Music:
title = info.get('title') title = info.get('title')
self.logger.debug(f'Playing URL: {url}') self.logger.debug(f'Playing URL: {url}')
try: try:
source = await discord.FFmpegOpusAudio.from_probe(url) source = discord.PCMVolumeTransformer(discord.FFmpegPCMAudio(url), volume=self.volume)
interaction.guild.voice_client.play(source) interaction.guild.voice_client.play(source)
embed = discord.Embed(description=f'Now playing: **{title}**', color=discord.Color.green()) embed = discord.Embed(description=f'Now playing: **{title}**', color=discord.Color.green())
await interaction.followup.send(embed=embed) await interaction.followup.send(embed=embed)
@ -124,6 +125,19 @@ class Music:
else: else:
await interaction.followup.send(embed=discord.Embed(description="I'm not playing anything right now.", color=discord.Color.red())) await interaction.followup.send(embed=discord.Embed(description="I'm not playing anything right now.", color=discord.Color.red()))
async def set_volume(self, interaction: discord.Interaction, volume: float):
self.logger.debug(f'User {interaction.user} is attempting to set volume to {volume}')
if 0 <= volume <= 1:
self.volume = volume
if interaction.guild.voice_client and interaction.guild.voice_client.source:
interaction.guild.voice_client.source.volume = volume
await interaction.followup.send(embed=discord.Embed(description=f"Volume set to {volume*100:.0f}%", color=discord.Color.green()))
self.logger.info(f'Volume set to {volume*100:.0f}%')
else:
await interaction.followup.send(embed=discord.Embed(description="Volume must be between 0 and 1.", color=discord.Color.red()))
self.logger.error('Invalid volume level attempted')
def setup(self, tree: app_commands.CommandTree): def setup(self, tree: 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):
@ -155,6 +169,11 @@ class Music:
await interaction.response.defer() # Defer the interaction response await interaction.response.defer() # Defer the interaction response
await self.stop(interaction) await self.stop(interaction)
@tree.command(name="volume", description="Set the volume (0 to 1)")
async def volume_command(interaction: discord.Interaction, volume: float):
await interaction.response.defer() # Defer the interaction response
await self.set_volume(interaction, volume)
def setup(bot): def setup(bot):
music = Music(bot) music = Music(bot)