From 4c4c15f63e8576cb68226024f8e4c9b3333fbed1 Mon Sep 17 00:00:00 2001 From: SimolZimol <70102430+SimolZimol@users.noreply.github.com> Date: Fri, 29 Aug 2025 17:44:11 +0200 Subject: [PATCH] modified: Dockerfile modified: bot.py --- Dockerfile | 9 +++ bot.py | 169 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 175 insertions(+), 3 deletions(-) diff --git a/Dockerfile b/Dockerfile index 79d9b38..9af013c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,6 +4,15 @@ FROM python:3.10-slim # Arbeitsverzeichnis erstellen WORKDIR /app +# System-Dependencies installieren (inkl. FFmpeg fรผr Audio-Support) +RUN apt-get update && apt-get install -y \ + ffmpeg \ + libffi-dev \ + libnacl-dev \ + python3-dev \ + && apt-get clean \ + && rm -rf /var/lib/apt/lists/* + # Kopiere die requirements-Datei und installiere die Abhรคngigkeiten COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt diff --git a/bot.py b/bot.py index b6d7b46..0ac4138 100644 --- a/bot.py +++ b/bot.py @@ -2359,9 +2359,21 @@ async def process_ai_queue(): await channel.send(embed=embed) if ctx.voice_client: # If bot is in a voice channel - tts = gTTS(assistant_message, lang="en") - tts.save("response.mp3") - ctx.voice_client.play(discord.FFmpegPCMAudio("response.mp3")) + try: + import shutil + # Check if FFmpeg is available + if not shutil.which("ffmpeg"): + await channel.send("โš ๏ธ FFmpeg is not available. Audio playback disabled. (This should not happen in Docker deployment)") + else: + tts = gTTS(assistant_message, lang="en") + tts.save("response.mp3") + + # Check if bot is still connected before playing + if ctx.voice_client and ctx.voice_client.is_connected(): + ctx.voice_client.play(discord.FFmpegPCMAudio("response.mp3")) + await channel.send("๐Ÿ”Š Playing TTS audio...") + except Exception as e: + await channel.send(f"โš ๏ธ TTS audio playback failed: {str(e)}") user_history.append({"role": "assistant", "content": assistant_message}) @@ -5940,6 +5952,157 @@ async def contact_status(ctx): await ctx.send(f"Error getting contact status: {e}") logger.error(f"Error in contact_status: {e}") +@client.hybrid_command() +async def join_voice(ctx): + """Join the voice channel that the user is currently in.""" + # Check if it's a slash command and defer if needed + is_slash_command = hasattr(ctx, 'interaction') and ctx.interaction + if is_slash_command: + await ctx.defer() + + # Helper function for sending responses + async def send_response(content=None, embed=None, ephemeral=False): + try: + if is_slash_command and hasattr(ctx, 'followup'): + if embed: + return await ctx.followup.send(embed=embed, ephemeral=ephemeral) + else: + return await ctx.followup.send(content, ephemeral=ephemeral) + else: + # Fallback for regular commands or if followup isn't available + if embed: + return await ctx.send(embed=embed) + else: + return await ctx.send(content) + except Exception as e: + # Final fallback + if embed: + return await ctx.send(embed=embed) + else: + return await ctx.send(content) + + if ctx.author.voice is None: + await send_response("โŒ You need to be in a voice channel for me to join!") + return + + voice_channel = ctx.author.voice.channel + + if ctx.voice_client is not None: + if ctx.voice_client.channel == voice_channel: + await send_response("๐Ÿ”Š I'm already in your voice channel!") + return + else: + await ctx.voice_client.move_to(voice_channel) + await send_response(f"๐Ÿ”Š Moved to **{voice_channel.name}**!") + return + + try: + await voice_channel.connect() + await send_response(f"โœ… Joined **{voice_channel.name}**! You can now use TTS features.") + except Exception as e: + await send_response(f"โŒ Failed to join voice channel: {str(e)}") + +@client.hybrid_command() +async def leave_voice(ctx): + """Leave the current voice channel.""" + # Check if it's a slash command and defer if needed + is_slash_command = hasattr(ctx, 'interaction') and ctx.interaction + if is_slash_command: + await ctx.defer() + + # Helper function for sending responses + async def send_response(content=None, embed=None, ephemeral=False): + try: + if is_slash_command and hasattr(ctx, 'followup'): + if embed: + return await ctx.followup.send(embed=embed, ephemeral=ephemeral) + else: + return await ctx.followup.send(content, ephemeral=ephemeral) + else: + # Fallback for regular commands or if followup isn't available + if embed: + return await ctx.send(embed=embed) + else: + return await ctx.send(content) + except Exception as e: + # Final fallback + if embed: + return await ctx.send(embed=embed) + else: + return await ctx.send(content) + + if ctx.voice_client is None: + await send_response("โŒ I'm not in a voice channel!") + return + + try: + await ctx.voice_client.disconnect() + await send_response("๐Ÿ‘‹ Left the voice channel!") + except Exception as e: + await send_response(f"โŒ Failed to leave voice channel: {str(e)}") + +@client.hybrid_command() +async def test_tts(ctx, *, text: str = "Hello! This is a TTS test."): + """Test TTS functionality with custom text.""" + # Check if it's a slash command and defer if needed + is_slash_command = hasattr(ctx, 'interaction') and ctx.interaction + if is_slash_command: + await ctx.defer() + + # Helper function for sending responses + async def send_response(content=None, embed=None, ephemeral=False): + try: + if is_slash_command and hasattr(ctx, 'followup'): + if embed: + return await ctx.followup.send(embed=embed, ephemeral=ephemeral) + else: + return await ctx.followup.send(content, ephemeral=ephemeral) + else: + # Fallback for regular commands or if followup isn't available + if embed: + return await ctx.send(embed=embed) + else: + return await ctx.send(content) + except Exception as e: + # Final fallback + if embed: + return await ctx.send(embed=embed) + else: + return await ctx.send(content) + + if ctx.voice_client is None: + await send_response("โŒ I need to be in a voice channel first! Use `/join_voice` to get me connected.") + return + + if len(text) > 500: + await send_response("โŒ Text is too long! Please keep it under 500 characters.") + return + + try: + import shutil + from gtts import gTTS + + # Check if FFmpeg is available + if not shutil.which("ffmpeg"): + await send_response("โš ๏ธ FFmpeg is not available. (This should not happen in Docker deployment)") + return + + # Generate TTS audio + await send_response("๐Ÿ”„ Generating TTS audio...") + + tts = gTTS(text, lang="en") + tts.save("tts_test.mp3") + + # Check if bot is still connected before playing + if ctx.voice_client and ctx.voice_client.is_connected(): + ctx.voice_client.play(discord.FFmpegPCMAudio("tts_test.mp3")) + await send_response(f"๐Ÿ”Š Playing TTS: \"{text[:100]}{'...' if len(text) > 100 else ''}\"") + else: + await send_response("โŒ Lost connection to voice channel!") + + except Exception as e: + await send_response(f"โŒ TTS test failed: {str(e)}") + try: # Initialize database tables create_warnings_table()