diff options
| author | salmonik <raisersoft@proton.me> | 2026-04-18 00:24:50 +0200 |
|---|---|---|
| committer | salmonik <raisersoft@proton.me> | 2026-04-18 00:24:50 +0200 |
| commit | a2b5b10625126b7e98643af9fe40569c9e004047 (patch) | |
| tree | 2b59a929d068bc28bbc21e8c8d8836e4d29270c0 /cogs/tts.py | |
| parent | add mathematical plotting (diff) | |
| download | senator-sperma-a2b5b10625126b7e98643af9fe40569c9e004047.tar.gz senator-sperma-a2b5b10625126b7e98643af9fe40569c9e004047.zip | |
refactor + preparing bot for public use
Signed-off-by: salmonik <raisersoft@proton.me>
Diffstat (limited to 'cogs/tts.py')
| -rw-r--r-- | cogs/tts.py | 21 |
1 files changed, 10 insertions, 11 deletions
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): | |||
| 46 | if not (ctx.author.voice and ctx.author.voice.channel): | 46 | if not (ctx.author.voice and ctx.author.voice.channel): |
| 47 | logger.info("User is not in a voice channel") | 47 | logger.info("User is not in a voice channel") |
| 48 | return | 48 | return |
| 49 | 49 | ||
| 50 | channel = ctx.author.voice.channel | 50 | channel = ctx.author.voice.channel |
| 51 | try: | 51 | try: |
| 52 | voice_client = await self._get_or_connect_voice_client(channel) | 52 | voice_client = await self._get_or_connect_voice_client(channel) |
| 53 | 53 | ||
| 54 | if not voice_client: | 54 | if not voice_client: |
| 55 | await ctx.respond("Failed to connect to voice channel", ephemeral=True) | 55 | await ctx.respond("Failed to connect to voice channel", ephemeral=True) |
| 56 | return | 56 | return |
| 57 | 57 | ||
| 58 | # Play the TTS audio with volume adjustment | 58 | # Play the TTS audio with volume adjustment |
| 59 | audio_source = discord.FFmpegPCMAudio( | 59 | audio_source = discord.FFmpegPCMAudio( |
| 60 | temp_path, | 60 | temp_path, |
| @@ -62,11 +62,11 @@ class TTS(commands.Cog): | |||
| 62 | ) | 62 | ) |
| 63 | voice_client.play(audio_source) | 63 | voice_client.play(audio_source) |
| 64 | logger.info(f"Playing TTS: {text}") | 64 | logger.info(f"Playing TTS: {text}") |
| 65 | 65 | ||
| 66 | # Wait for the audio to finish playing | 66 | # Wait for the audio to finish playing |
| 67 | while voice_client.is_playing(): | 67 | while voice_client.is_playing(): |
| 68 | await asyncio.sleep(0.1) | 68 | await asyncio.sleep(0.1) |
| 69 | 69 | ||
| 70 | except Exception as e: | 70 | except Exception as e: |
| 71 | logger.error(f"Error in voice channel handling: {e}") | 71 | logger.error(f"Error in voice channel handling: {e}") |
| 72 | await ctx.respond("eror :c", ephemeral=True) | 72 | await ctx.respond("eror :c", ephemeral=True) |
| @@ -85,11 +85,10 @@ class TTS(commands.Cog): | |||
| 85 | output: str = discord.Option(str, "Wybierz sposób odtwarzania", choices=["file", "vc"], default="vc") | 85 | output: str = discord.Option(str, "Wybierz sposób odtwarzania", choices=["file", "vc"], default="vc") |
| 86 | ): | 86 | ): |
| 87 | await ctx.defer() | 87 | await ctx.defer() |
| 88 | 88 | ||
| 89 | # Create temporary file in temp folder | 89 | # Create temporary file in temp folder |
| 90 | temp_dir = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'temp') | 90 | from cogs.utils.temp import get_temp_dir |
| 91 | os.makedirs(temp_dir, exist_ok=True) | 91 | temp_path = os.path.join(get_temp_dir(), f"tts_{os.getpid()}_{asyncio.get_event_loop().time()}.mp3") |
| 92 | temp_path = os.path.join(temp_dir, f"tts_{os.getpid()}_{asyncio.get_event_loop().time()}.mp3") | ||
| 93 | 92 | ||
| 94 | try: | 93 | try: |
| 95 | # Generate and save TTS to temp file | 94 | # Generate and save TTS to temp file |
| @@ -105,7 +104,7 @@ class TTS(commands.Cog): | |||
| 105 | await self._handle_file_output(ctx, temp_path, text) | 104 | await self._handle_file_output(ctx, temp_path, text) |
| 106 | elif output == "vc": | 105 | elif output == "vc": |
| 107 | await self._handle_voice_output(ctx, temp_path, text, volume) | 106 | await self._handle_voice_output(ctx, temp_path, text, volume) |
| 108 | 107 | ||
| 109 | except Exception as e: | 108 | except Exception as e: |
| 110 | logger.error(f"TTS generation error: {e}") | 109 | logger.error(f"TTS generation error: {e}") |
| 111 | await ctx.respond("Error generating TTS", ephemeral=True) | 110 | await ctx.respond("Error generating TTS", ephemeral=True) |
| @@ -113,4 +112,4 @@ class TTS(commands.Cog): | |||
| 113 | await self._cleanup_temp_file(temp_path) | 112 | await self._cleanup_temp_file(temp_path) |
| 114 | 113 | ||
| 115 | def setup(bot): | 114 | def setup(bot): |
| 116 | bot.add_cog(TTS(bot)) \ No newline at end of file | 115 | bot.add_cog(TTS(bot)) |
