From cd8ed8bc5793985df2c1692072a932ceee517b95 Mon Sep 17 00:00:00 2001 From: salmonik Date: Wed, 8 Jul 2026 02:13:35 +0200 Subject: OPEN THE RUST GATES Signed-off-by: salmonik --- cogs/tts.py | 111 ------------------------------------------------------------ 1 file changed, 111 deletions(-) delete mode 100644 cogs/tts.py (limited to 'cogs/tts.py') 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 @@ -# This file is part of Senator Sperma -# Licensed under the GPL-3.0 License -# Copyright (C) 2026 Salmonik @ salmonik@tuta.io -# https://codeberg.org/salmonik/senator-sperma - -import os -import json -import discord -import logging -import tempfile -from discord.ext import commands -from gtts import gTTS -import asyncio -from cogs.utils.settings import get_volume - -logger = logging.getLogger(__name__) - -class TTS(commands.Cog): - def __init__(self, bot): - self.bot = bot - - async def _handle_file_output(self, ctx, temp_path, text): - """Handle TTS file output""" - 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) - - def _get_voice_manager(self): - from cogs.voice import get_voice_manager - return get_voice_manager(self.bot) - - async def _get_or_connect_voice_client(self, channel): - """Get existing voice client or connect to new channel""" - voice_mgr = self._get_voice_manager() - return await voice_mgr.join_channel(channel) - - async def _handle_voice_output(self, ctx, temp_path, text, volume): - """Handle TTS voice channel output""" - await ctx.respond(content="\u200b", ephemeral=True) - await ctx.interaction.delete_original_response() - - 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_mgr = self._get_voice_manager() - - if voice_mgr.get_voice_client and await voice_mgr.get_client(channel.guild): - vc = await voice_mgr.get_client(channel.guild) - if vc and vc.is_playing(): - logger.info("Audio already playing, skipping TTS") - return - - audio_source = discord.FFmpegPCMAudio( - temp_path, - options=f'-filter:a "volume={volume}"' - ) - logger.info(f"Playing TTS: {text}") - await voice_mgr.play_source(channel, audio_source) - - except Exception as e: - logger.error(f"Error in voice channel handling: {e}") - await ctx.respond("eror :c", ephemeral=True) - - async def _cleanup_temp_file(self, temp_path): - """Clean up 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}") - - @commands.slash_command(name="tts", description="Generuje TTS dla danego tekstu") - async def tts(self, ctx, - text: str = discord.Option(str, "Tekst do przeczytania", required=True), - output: str = discord.Option(str, "Wybierz sposób odtwarzania", choices=["file", "vc"], default="vc") - ): - await ctx.defer() - - # Create temporary file in temp folder - 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 - tts = gTTS(text=text, lang='pl') - loop = asyncio.get_event_loop() - await loop.run_in_executor(None, tts.save, temp_path) - - # Load settings - volume = get_volume() - - # Handle output based on user choice - if output == "file": - 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) - finally: - await self._cleanup_temp_file(temp_path) - -def setup(bot): - bot.add_cog(TTS(bot)) -- cgit v1.2.3