From a2b5b10625126b7e98643af9fe40569c9e004047 Mon Sep 17 00:00:00 2001 From: salmonik Date: Sat, 18 Apr 2026 00:24:50 +0200 Subject: refactor + preparing bot for public use Signed-off-by: salmonik --- cogs/tts.py | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) (limited to 'cogs/tts.py') diff --git a/cogs/tts.py b/cogs/tts.py index 65f1fce..3dc4c09 100644 --- a/cogs/tts.py +++ b/cogs/tts.py @@ -46,15 +46,15 @@ class TTS(commands.Cog): if not (ctx.author.voice and ctx.author.voice.channel): logger.info("User is not in a voice channel") return - + channel = ctx.author.voice.channel try: voice_client = await self._get_or_connect_voice_client(channel) - + if not voice_client: await ctx.respond("Failed to connect to voice channel", ephemeral=True) return - + # Play the TTS audio with volume adjustment audio_source = discord.FFmpegPCMAudio( temp_path, @@ -62,11 +62,11 @@ class TTS(commands.Cog): ) voice_client.play(audio_source) logger.info(f"Playing TTS: {text}") - + # Wait for the audio to finish playing while voice_client.is_playing(): await asyncio.sleep(0.1) - + except Exception as e: logger.error(f"Error in voice channel handling: {e}") await ctx.respond("eror :c", ephemeral=True) @@ -85,11 +85,10 @@ class TTS(commands.Cog): output: str = discord.Option(str, "Wybierz sposób odtwarzania", choices=["file", "vc"], default="vc") ): await ctx.defer() - + # Create temporary file in temp folder - temp_dir = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'temp') - os.makedirs(temp_dir, exist_ok=True) - temp_path = os.path.join(temp_dir, f"tts_{os.getpid()}_{asyncio.get_event_loop().time()}.mp3") + from cogs.utils.temp import get_temp_dir + temp_path = os.path.join(get_temp_dir(), f"tts_{os.getpid()}_{asyncio.get_event_loop().time()}.mp3") try: # Generate and save TTS to temp file @@ -105,7 +104,7 @@ class TTS(commands.Cog): await self._handle_file_output(ctx, temp_path, text) elif output == "vc": await self._handle_voice_output(ctx, temp_path, text, volume) - + except Exception as e: logger.error(f"TTS generation error: {e}") await ctx.respond("Error generating TTS", ephemeral=True) @@ -113,4 +112,4 @@ class TTS(commands.Cog): await self._cleanup_temp_file(temp_path) def setup(bot): - bot.add_cog(TTS(bot)) \ No newline at end of file + bot.add_cog(TTS(bot)) -- cgit v1.2.3