From 72abc43db8f732743c65d6f6a820369ab8665766 Mon Sep 17 00:00:00 2001 From: salmonik Date: Wed, 4 Feb 2026 16:19:22 +0100 Subject: more asynchronous work and other minor improvements --- cogs/vclogs.py | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) (limited to 'cogs/vclogs.py') diff --git a/cogs/vclogs.py b/cogs/vclogs.py index 40a618c..bc317a4 100644 --- a/cogs/vclogs.py +++ b/cogs/vclogs.py @@ -10,6 +10,7 @@ import os import discord from discord.ext import commands from gtts import gTTS +import aiofiles logger = logging.getLogger(__name__) @@ -20,15 +21,14 @@ class VcLogs(commands.Cog): self.bot = bot self.settings_check() + settings_file = 'settings.json' + @staticmethod def settings_check(): - if not os.path.exists('settings.json'): - with open('settings.json', 'w') as f: + if not os.path.exists(settings_file): + with open(settings_file, 'w') as f: json.dump({'volume': '5'}, f, indent=4) logger.info("settings.json created") - with open('settings.json', 'r') as f: - settings = json.load(f) - volume = settings.get('volume', '5') @staticmethod async def handle_state_change(member, channel, state_type, is_enabled, current_time, log_channel): @@ -62,12 +62,13 @@ class VcLogs(commands.Cog): return tts_text, channel async def play_tts(self, channel, text): + tts_file = "temp_tts.mp3" # Generate TTS audio tts = gTTS(text=text, lang='pl') - tts.save('temp_tts.mp3') + tts.save(tts_file) - if os.path.exists('settings.json'): - with open('settings.json') as f: + if os.path.exists(settings_file): + async with aiofiles.open(settings_file) as f: data = json.load(f) volume = data['volume'] else: @@ -82,23 +83,23 @@ class VcLogs(commands.Cog): # Play the TTS audio if not voice_client.is_playing(): - audio_source = discord.FFmpegPCMAudio('temp_tts.mp3', options=f'-filter:a "volume={volume}"') + audio_source = discord.FFmpegPCMAudio(tts_file, options=f'-filter:a "volume={volume}"') voice_client.play(audio_source) while voice_client.is_playing(): await discord.utils.sleep_until(datetime.datetime.now() + datetime.timedelta(seconds=1)) - if os.path.exists('temp_tts.mp3'): - os.remove('temp_tts.mp3') + if os.path.exists(tts_file): + os.remove(tts_file) @commands.slash_command( name="log_volume" ) async def volume(self, ctx: discord.ApplicationContext, volume: int): try: - if not os.path.exists('settings.json'): - with open('settings.json', 'w') as f: + if not os.path.exists(settings_file): + async with aiofiles.open(settings_file, 'w') as f: json.dump({'volume': '5'}, f, indent=4) logger.info("settings.json created") - with open('settings.json', 'w') as f: + async with aiofiles.open(settings_file, 'w') as f: json.dump({"volume": volume}, f, indent=4) await ctx.followup.send(f"Volume has been set to: {volume}") logger.info(f"Volume changed to: {volume}") -- cgit v1.2.3