diff options
| -rw-r--r-- | cogs/tts.py | 107 |
1 files 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 @@ | |||
| 1 | import datetime | ||
| 2 | import os | 1 | import os |
| 3 | import json | 2 | import json |
| 4 | import discord | 3 | import discord |
| 4 | import tempfile | ||
| 5 | import logging | ||
| 5 | from discord.ext import commands | 6 | from discord.ext import commands |
| 6 | from gtts import gTTS | 7 | from gtts import gTTS |
| 7 | import logging | 8 | import asyncio |
| 8 | 9 | ||
| 9 | logger = logging.getLogger(__name__) | 10 | logger = logging.getLogger(__name__) |
| 10 | 11 | ||
| @@ -18,46 +19,74 @@ class TTS(commands.Cog): | |||
| 18 | output: str = discord.Option(str, "Wybierz sposób odtwarzania", choices=["file", "vc"], default="vc") | 19 | output: str = discord.Option(str, "Wybierz sposób odtwarzania", choices=["file", "vc"], default="vc") |
| 19 | ): | 20 | ): |
| 20 | await ctx.defer() | 21 | await ctx.defer() |
| 21 | tts = gTTS(text=text, lang='pl') | 22 | # Create a temporary file for TTS output |
| 22 | tts.save('tts.mp3') | 23 | with tempfile.NamedTemporaryFile(suffix='.mp3', delete=False) as temp_file: |
| 24 | temp_path = temp_file.name | ||
| 25 | |||
| 26 | try: | ||
| 27 | # Generate and save TTS to temp file | ||
| 28 | tts = gTTS(text=text, lang='pl') | ||
| 29 | tts.save(temp_path) | ||
| 23 | 30 | ||
| 24 | if os.path.exists('settings.json'): | 31 | # Load settings if available |
| 25 | with open('settings.json') as f: | 32 | volume = 1.0 # Default volume |
| 26 | data = json.load(f) | 33 | if os.path.exists('settings.json'): |
| 27 | volume = data['volume'] | 34 | try: |
| 28 | else: | 35 | with open('settings.json') as f: |
| 29 | logger.error("settings.json not found") | 36 | data = json.load(f) |
| 37 | volume = data.get('volume', 1.0) | ||
| 38 | except (json.JSONDecodeError, KeyError) as e: | ||
| 39 | logger.error(f"Error loading settings: {e}") | ||
| 30 | 40 | ||
| 31 | if output == "file": | 41 | if output == "file": |
| 32 | await ctx.respond(file=discord.File('tts.mp3')) | 42 | try: |
| 33 | logger.info(f"sent {text}") | 43 | await ctx.respond(file=discord.File(temp_path, filename='tts.mp3')) |
| 34 | os.remove('tts.mp3') | 44 | logger.info(f"Sent TTS for text: {text}") |
| 35 | elif output == "vc": | 45 | except Exception as e: |
| 36 | await ctx.respond(content="\u200b", ephemeral=True) | 46 | logger.error(f"Error sending file: {e}") |
| 37 | await ctx.interaction.delete_original_response() | 47 | await ctx.respond("eror :c", ephemeral=True) |
| 48 | |||
| 49 | elif output == "vc": | ||
| 50 | await ctx.respond(content="\u200b", ephemeral=True) | ||
| 51 | await ctx.interaction.delete_original_response() | ||
| 38 | 52 | ||
| 39 | # Check if the user is in a voice channel | 53 | # Check if the user is in a voice channel |
| 40 | if not (ctx.author.voice and ctx.author.voice.channel): | 54 | if not (ctx.author.voice and ctx.author.voice.channel): |
| 41 | logger.info("User is not in a voice channel") | 55 | logger.info("User is not in a voice channel") |
| 42 | os.remove('tts.mp3') | 56 | return |
| 43 | return | 57 | |
| 44 | channel = ctx.author.voice.channel | 58 | channel = ctx.author.voice.channel |
| 45 | # Connect to the voice channel if not already connected | 59 | try: |
| 46 | voice_client = discord.utils.get(self.bot.voice_clients, guild=channel.guild) | 60 | # Connect to the voice channel if not already connected |
| 47 | if not voice_client or not voice_client.is_connected(): | 61 | voice_client = discord.utils.get(self.bot.voice_clients, guild=channel.guild) |
| 48 | voice_client = await channel.connect() | 62 | if not voice_client or not voice_client.is_connected(): |
| 49 | elif voice_client.channel != channel: | 63 | voice_client = await channel.connect() |
| 50 | await voice_client.move_to(channel) | 64 | elif voice_client.channel != channel: |
| 51 | # Play the TTS audio | 65 | await voice_client.move_to(channel) |
| 52 | if not voice_client.is_playing(): | 66 | |
| 53 | audio_source = discord.FFmpegPCMAudio('tts.mp3', options=f'-filter:a "volume={volume}"') | 67 | # Play the TTS audio with volume adjustment |
| 54 | logger.info(f"playing {text}") | 68 | audio_source = discord.FFmpegPCMAudio( |
| 55 | voice_client.play(audio_source) | 69 | temp_path, |
| 56 | os.remove('tts.mp3') | 70 | options=f'-filter:a "volume={volume}"' |
| 57 | else: | 71 | ) |
| 58 | await ctx.respond("Nieznany sposób odtwarzania.", ephemeral=True) | 72 | voice_client.play(audio_source) |
| 59 | logger.info("Unknown playback method") | 73 | logger.info(f"Playing TTS: {text}") |
| 60 | os.remove('tts.mp3') | 74 | |
| 75 | # Wait for the audio to finish playing | ||
| 76 | while voice_client.is_playing(): | ||
| 77 | await asyncio.sleep(0.1) | ||
| 78 | |||
| 79 | except Exception as e: | ||
| 80 | logger.error(f"Error in voice channel handling: {e}") | ||
| 81 | await ctx.respond("eror :c", ephemeral=True) | ||
| 82 | |||
| 83 | finally: | ||
| 84 | # Clean up the temporary file | ||
| 85 | try: | ||
| 86 | if temp_path and os.path.exists(temp_path): | ||
| 87 | os.unlink(temp_path) | ||
| 88 | except Exception as e: | ||
| 89 | logger.error(f"Error cleaning up temp file {temp_path}: {e}") | ||
| 61 | 90 | ||
| 62 | def setup(bot): | 91 | def setup(bot): |
| 63 | bot.add_cog(TTS(bot)) \ No newline at end of file | 92 | bot.add_cog(TTS(bot)) \ No newline at end of file |
