From 8feac73258dd6eb40f7babc2a5a38f2415ae3895 Mon Sep 17 00:00:00 2001 From: lamzaone Date: Wed, 24 Apr 2024 01:48:49 +0300 Subject: [PATCH] Added !volume command --- config/__pycache__/config.cpython-310.pyc | Bin 814 -> 869 bytes config/config.py | 4 +- config/serversettings.json | 5 +++ main.py | 46 +++++++++++++++------- 4 files changed, 38 insertions(+), 17 deletions(-) diff --git a/config/__pycache__/config.cpython-310.pyc b/config/__pycache__/config.cpython-310.pyc index ab975de2555cc3013fd2a1128a243ed76b274cb0..1b81e453339dde7c49a49f05f4e1a15298aa5e97 100644 GIT binary patch delta 214 zcmZ3-_LPk`pO=@50SGiLG}62#^2(YN068fPDU3M`Q4pFjiZO*LiYb*bi+KS{D&qpy zg$yao3z?$WQdm-0Q`qJ(MX^uxkeB61Wys=Oz_pMeiaUiPl{=mXO!9(BzKJd298$^o zd1;yH`Wx@KGwSi*Vo%8|PR=h%xy6)Ue2X(Fza+jSKRY$=7E3{5NyaUfto+QpTWsm6 zCCTL}wX{7m1q8L(`q8L+{qnJ`yQdm>i<}gJu zPmGh7Wl3eoVqL(tkRggag(H", ), status=discord.Status.do_not_disturb) + + for guild in bot.guilds: + if str(guild.id) not in config.serversettings: + # write the guild id to the settings file + with open(config.serversettings, 'w') as f: + json.dump({guild.id: { + 'volume': 0.5 + }}, f, indent=4) + + with open(config.serversettings, 'r') as f: + settings = json.load(f) + settings = settings[str(guild.id)] + @bot.command(name='play', help='Play music from YouTube using a search term or URL') async def play(ctx, *, query: str): + # retrieve settings for the guild from the settings file + with open(config.serversettings, 'r') as f: + settings = json.load(f) + settings = settings[str(ctx.guild.id)] + print(settings) # Connect to the voice channel if not ctx.author.voice: await ctx.send("You must be in a voice channel to play music.") @@ -52,7 +71,7 @@ async def play(ctx, *, query: str): ctx.bot.video_url = video_url # Play the audio using FFmpeg audio_source = discord.FFmpegPCMAudio(video_url, executable=FFMPEG_PATH, **config.ffmpeg_options) - audio_source = discord.PCMVolumeTransformer(audio_source, volume=0.5) + audio_source = discord.PCMVolumeTransformer(audio_source, settings['volume']) voice_client.play(audio_source) await ctx.send(f":notes: Now playing: {info['title']} from {info['original_url']}") @@ -81,25 +100,22 @@ async def pause(ctx): ctx.voice_client.resume() await ctx.send("Music resumed.") -@bot.command(name='volume', help='Adjust the volume level.') -async def volume(ctx, volume: float): - # Ensure the input volume is a percentage (e.g., 50 for 50%) - volume = volume / 100 # Convert to a float from a percentage +@bot.command(name='volume', help='Set the volume of the music') +async def volume(ctx, volume: int): + #change the settings for the guild in the settings file + with open(config.serversettings, 'r') as f: + settings = json.load(f) + settings = settings[str(ctx.guild.id)] if ctx.voice_client and ctx.voice_client.is_playing(): - # Re-create the audio source with the new volume - if bot.original_source is None: - bot.original_source = ctx.voice_client.source # Original audio source - new_source = discord.PCMVolumeTransformer(bot.original_source, volume=volume) # Create a new volume transformer + settings['volume'] = volume / 100 + with open(config.serversettings, 'w') as f: + json.dump({ctx.guild.id: settings}, f, indent=4) + ctx.voice_client.source.volume = volume / 100 + await ctx.send(f"Volume set to {volume}%") - # Stop current playback - ctx.voice_client.source = new_source - await ctx.send(f"Volume set to {volume * 100}%.") - else: - await ctx.send("No music is currently playing.") - @bot.command(name='ping', help='Check the bot\'s latency') async def ping(ctx): await ctx.send(f'Pong! Latency: {round(bot.latency * 1000)}ms, websocket latency: {round(bot.ws.latency * 1000)}ms')