From 2521c1f5b958f860c70af48b99c7a5507f5b5c66 Mon Sep 17 00:00:00 2001 From: Samlon Date: Fri, 22 Aug 2025 11:49:50 +0200 Subject: fix tts --- cogs/tts.py | 107 ++++++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 68 insertions(+), 39 deletions(-) diff --git a/cogs/tts.py b/cogs/tts.py index 74fc12c..26cb715 100644 --- a/cogs/tts.py +++ b/cogs/tts.py @@ -1,10 +1,11 @@ -import datetime import os import json import discord +import tempfile +import logging from discord.ext import commands from gtts import gTTS -import logging +import asyncio logger = logging.getLogger(__name__) @@ -18,46 +19,74 @@ class TTS(commands.Cog): output: str = discord.Option(str, "Wybierz sposób odtwarzania", choices=["file", "vc"], default="vc") ): await ctx.defer() - tts = gTTS(text=text, lang='pl') - tts.save('tts.mp3') + # Create a temporary file for TTS output + with tempfile.NamedTemporaryFile(suffix='.mp3', delete=False) as temp_file: + temp_path = temp_file.name + + try: + # Generate and save TTS to temp file + tts = gTTS(text=text, lang='pl') + tts.save(temp_path) - if os.path.exists('settings.json'): - with open('settings.json') as f: - data = json.load(f) - volume = data['volume'] - else: - logger.error("settings.json not found") + # Load settings if available + volume = 1.0 # Default volume + if os.path.exists('settings.json'): + try: + with open('settings.json') as f: + data = json.load(f) + volume = data.get('volume', 1.0) + except (json.JSONDecodeError, KeyError) as e: + logger.error(f"Error loading settings: {e}") - if output == "file": - await ctx.respond(file=discord.File('tts.mp3')) - logger.info(f"sent {text}") - os.remove('tts.mp3') - elif output == "vc": - await ctx.respond(content="\u200b", ephemeral=True) - await ctx.interaction.delete_original_response() + if output == "file": + try: + await ctx.respond(file=discord.File(temp_path, filename='tts.mp3')) + logger.info(f"Sent TTS for text: {text}") + except Exception as e: + logger.error(f"Error sending file: {e}") + await ctx.respond("eror :c", ephemeral=True) + + elif output == "vc": + await ctx.respond(content="\u200b", ephemeral=True) + await ctx.interaction.delete_original_response() - # Check if the user is in a voice channel - if not (ctx.author.voice and ctx.author.voice.channel): - logger.info("User is not in a voice channel") - os.remove('tts.mp3') - return - channel = ctx.author.voice.channel - # Connect to the voice channel if not already connected - voice_client = discord.utils.get(self.bot.voice_clients, guild=channel.guild) - if not voice_client or not voice_client.is_connected(): - voice_client = await channel.connect() - elif voice_client.channel != channel: - await voice_client.move_to(channel) - # Play the TTS audio - if not voice_client.is_playing(): - audio_source = discord.FFmpegPCMAudio('tts.mp3', options=f'-filter:a "volume={volume}"') - logger.info(f"playing {text}") - voice_client.play(audio_source) - os.remove('tts.mp3') - else: - await ctx.respond("Nieznany sposób odtwarzania.", ephemeral=True) - logger.info("Unknown playback method") - os.remove('tts.mp3') + # Check if the user is in a voice channel + 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: + # Connect to the voice channel if not already connected + voice_client = discord.utils.get(self.bot.voice_clients, guild=channel.guild) + if not voice_client or not voice_client.is_connected(): + voice_client = await channel.connect() + elif voice_client.channel != channel: + await voice_client.move_to(channel) + + # Play the TTS audio with volume adjustment + audio_source = discord.FFmpegPCMAudio( + temp_path, + options=f'-filter:a "volume={volume}"' + ) + 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) + + finally: + # Clean up the temporary file + try: + if temp_path and os.path.exists(temp_path): + os.unlink(temp_path) + except Exception as e: + logger.error(f"Error cleaning up temp file {temp_path}: {e}") def setup(bot): bot.add_cog(TTS(bot)) \ No newline at end of file -- cgit v1.2.3