diff --git a/bot.py b/bot.py index 926f8b0..2c796a8 100644 --- a/bot.py +++ b/bot.py @@ -4182,7 +4182,34 @@ async def addnotes(ctx, type: str, source: str = None, attachment: discord.Attac For text files: /addnotes txt [attach file] For websites: /addnotes url https://example.com """ - await ctx.defer() # Signalisiert, dass die Bearbeitung des Befehls begonnen hat + # 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: + if embed: + await ctx.followup.send(embed=embed, ephemeral=ephemeral) + else: + await ctx.followup.send(content, ephemeral=ephemeral) + else: + if embed: + await ctx.send(embed=embed) + else: + await ctx.send(content) + except Exception as e: + logger.error(f"Error sending response: {e}") + # Fallback to regular send if followup fails + try: + if embed: + await ctx.send(embed=embed) + else: + await ctx.send(content) + except: + pass user_id = ctx.author.id guild_id = ctx.guild.id @@ -4205,13 +4232,13 @@ async def addnotes(ctx, type: str, source: str = None, attachment: discord.Attac file.write(f"\n--- From file: {attachment.filename} ---\n") file.write(text_content + "\n") - await ctx.followup.send(f"✅ Text file `{attachment.filename}` added as notes for user {ctx.author.name}.") + await send_response(f"✅ Text file `{attachment.filename}` added as notes for user {ctx.author.name}.") except UnicodeDecodeError: - await ctx.followup.send("❌ Error: File is not a valid text file.") + await send_response("❌ Error: File is not a valid text file.") except Exception as e: - await ctx.followup.send(f"❌ Error reading file: {e}") + await send_response(f"❌ Error reading file: {e}") else: - await ctx.followup.send("❌ Please attach a text file (.txt, .md, etc.)") + await send_response("❌ Please attach a text file (.txt, .md, etc.)") # Fallback for prefix commands with message attachments elif hasattr(ctx, 'message') and ctx.message and ctx.message.attachments: attachment = ctx.message.attachments[0] @@ -4223,17 +4250,17 @@ async def addnotes(ctx, type: str, source: str = None, attachment: discord.Attac file.write(f"\n--- From file: {attachment.filename} ---\n") file.write(text_content + "\n") - await ctx.send(f"✅ Text file `{attachment.filename}` added as notes for user {ctx.author.name}.") + await send_response(f"✅ Text file `{attachment.filename}` added as notes for user {ctx.author.name}.") except UnicodeDecodeError: - await ctx.send("❌ Error: File is not a valid text file.") + await send_response("❌ Error: File is not a valid text file.") except Exception as e: - await ctx.send(f"❌ Error reading file: {e}") + await send_response(f"❌ Error reading file: {e}") else: - await ctx.followup.send("❌ No text file attached. Please use the attachment parameter for Slash Commands.") + await send_response("❌ No text file attached. Please use the attachment parameter for Slash Commands.") elif type.lower() == "url": if not source: - await ctx.followup.send("❌ Please provide a URL: `/addnotes url https://example.com`") + await send_response("❌ Please provide a URL: `/addnotes url https://example.com`") return try: @@ -4254,13 +4281,13 @@ async def addnotes(ctx, type: str, source: str = None, attachment: discord.Attac with open(note_file, "a", encoding="utf-8") as file: file.write(f"\n--- From URL: {source} ---\n") file.write(cleaned_text + "\n") - await ctx.followup.send(f"✅ Website content from `{source}` added as notes for user {ctx.author.name}.") + await send_response(f"✅ Website content from `{source}` added as notes for user {ctx.author.name}.") else: - await ctx.followup.send(f"❌ Failed to retrieve the website from {source}. HTTP {response.status_code}") + await send_response(f"❌ Failed to retrieve the website from {source}. HTTP {response.status_code}") except Exception as e: - await ctx.followup.send(f"❌ Error fetching website: {e}") + await send_response(f"❌ Error fetching website: {e}") else: - await ctx.followup.send("❌ Invalid type. Use 'txt' for text files or 'url' for website URLs.") + await send_response("❌ Invalid type. Use 'txt' for text files or 'url' for website URLs.") @client.hybrid_command() async def asknotes(ctx, *, question: str):