diff options
Diffstat (limited to 'cogs/tts.py')
| -rw-r--r-- | cogs/tts.py | 111 |
1 files changed, 0 insertions, 111 deletions
diff --git a/cogs/tts.py b/cogs/tts.py deleted file mode 100644 index ca81d3a..0000000 --- a/cogs/tts.py +++ /dev/null | |||
| @@ -1,111 +0,0 @@ | |||
| 1 | # This file is part of Senator Sperma | ||
| 2 | # Licensed under the GPL-3.0 License | ||
| 3 | # Copyright (C) 2026 Salmonik @ salmonik@tuta.io | ||
| 4 | # https://codeberg.org/salmonik/senator-sperma | ||
| 5 | |||
| 6 | import os | ||
| 7 | import json | ||
| 8 | import discord | ||
| 9 | import logging | ||
| 10 | import tempfile | ||
| 11 | from discord.ext import commands | ||
| 12 | from gtts import gTTS | ||
| 13 | import asyncio | ||
| 14 | from cogs.utils.settings import get_volume | ||
| 15 | |||
| 16 | logger = logging.getLogger(__name__) | ||
| 17 | |||
| 18 | class TTS(commands.Cog): | ||
| 19 | def __init__(self, bot): | ||
| 20 | self.bot = bot | ||
| 21 | |||
| 22 | async def _handle_file_output(self, ctx, temp_path, text): | ||
| 23 | """Handle TTS file output""" | ||
| 24 | try: | ||
| 25 | await ctx.respond(file=discord.File(temp_path, filename='tts.mp3')) | ||
| 26 | logger.info(f"Sent TTS for text: {text}") | ||
| 27 | except Exception as e: | ||
| 28 | logger.error(f"Error sending file: {e}") | ||
| 29 | await ctx.respond("eror :c", ephemeral=True) | ||
| 30 | |||
| 31 | def _get_voice_manager(self): | ||
| 32 | from cogs.voice import get_voice_manager | ||
| 33 | return get_voice_manager(self.bot) | ||
| 34 | |||
| 35 | async def _get_or_connect_voice_client(self, channel): | ||
| 36 | """Get existing voice client or connect to new channel""" | ||
| 37 | voice_mgr = self._get_voice_manager() | ||
| 38 | return await voice_mgr.join_channel(channel) | ||
| 39 | |||
| 40 | async def _handle_voice_output(self, ctx, temp_path, text, volume): | ||
| 41 | """Handle TTS voice channel output""" | ||
| 42 | await ctx.respond(content="\u200b", ephemeral=True) | ||
| 43 | await ctx.interaction.delete_original_response() | ||
| 44 | |||
| 45 | if not (ctx.author.voice and ctx.author.voice.channel): | ||
| 46 | logger.info("User is not in a voice channel") | ||
| 47 | return | ||
| 48 | |||
| 49 | channel = ctx.author.voice.channel | ||
| 50 | try: | ||
| 51 | voice_mgr = self._get_voice_manager() | ||
| 52 | |||
| 53 | if voice_mgr.get_voice_client and await voice_mgr.get_client(channel.guild): | ||
| 54 | vc = await voice_mgr.get_client(channel.guild) | ||
| 55 | if vc and vc.is_playing(): | ||
| 56 | logger.info("Audio already playing, skipping TTS") | ||
| 57 | return | ||
| 58 | |||
| 59 | audio_source = discord.FFmpegPCMAudio( | ||
| 60 | temp_path, | ||
| 61 | options=f'-filter:a "volume={volume}"' | ||
| 62 | ) | ||
| 63 | logger.info(f"Playing TTS: {text}") | ||
| 64 | await voice_mgr.play_source(channel, audio_source) | ||
| 65 | |||
| 66 | except Exception as e: | ||
| 67 | logger.error(f"Error in voice channel handling: {e}") | ||
| 68 | await ctx.respond("eror :c", ephemeral=True) | ||
| 69 | |||
| 70 | async def _cleanup_temp_file(self, temp_path): | ||
| 71 | """Clean up temporary file""" | ||
| 72 | try: | ||
| 73 | if temp_path and os.path.exists(temp_path): | ||
| 74 | os.unlink(temp_path) | ||
| 75 | except Exception as e: | ||
| 76 | logger.error(f"Error cleaning up temp file {temp_path}: {e}") | ||
| 77 | |||
| 78 | @commands.slash_command(name="tts", description="Generuje TTS dla danego tekstu") | ||
| 79 | async def tts(self, ctx, | ||
| 80 | text: str = discord.Option(str, "Tekst do przeczytania", required=True), | ||
| 81 | output: str = discord.Option(str, "Wybierz sposób odtwarzania", choices=["file", "vc"], default="vc") | ||
| 82 | ): | ||
| 83 | await ctx.defer() | ||
| 84 | |||
| 85 | # Create temporary file in temp folder | ||
| 86 | from cogs.utils.temp import get_temp_dir | ||
| 87 | temp_path = os.path.join(get_temp_dir(), f"tts_{os.getpid()}_{asyncio.get_event_loop().time()}.mp3") | ||
| 88 | |||
| 89 | try: | ||
| 90 | # Generate and save TTS to temp file | ||
| 91 | tts = gTTS(text=text, lang='pl') | ||
| 92 | loop = asyncio.get_event_loop() | ||
| 93 | await loop.run_in_executor(None, tts.save, temp_path) | ||
| 94 | |||
| 95 | # Load settings | ||
| 96 | volume = get_volume() | ||
| 97 | |||
| 98 | # Handle output based on user choice | ||
| 99 | if output == "file": | ||
| 100 | await self._handle_file_output(ctx, temp_path, text) | ||
| 101 | elif output == "vc": | ||
| 102 | await self._handle_voice_output(ctx, temp_path, text, volume) | ||
| 103 | |||
| 104 | except Exception as e: | ||
| 105 | logger.error(f"TTS generation error: {e}") | ||
| 106 | await ctx.respond("Error generating TTS", ephemeral=True) | ||
| 107 | finally: | ||
| 108 | await self._cleanup_temp_file(temp_path) | ||
| 109 | |||
| 110 | def setup(bot): | ||
| 111 | bot.add_cog(TTS(bot)) | ||
