From 1aabe681e20c34d1a4948646f28e1d9804b6ba99 Mon Sep 17 00:00:00 2001 From: SimolZimol <70102430+SimolZimol@users.noreply.github.com> Date: Wed, 20 Aug 2025 20:22:06 +0200 Subject: [PATCH] modified: bot.py --- bot.py | 60 ++++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 50 insertions(+), 10 deletions(-) diff --git a/bot.py b/bot.py index 3825210..926f8b0 100644 --- a/bot.py +++ b/bot.py @@ -4176,8 +4176,12 @@ async def download_and_save_profile_image(user_id, discord_url): # Cache-Ordner für Notizen @client.hybrid_command() -async def addnotes(ctx, type: str, *, source: str): - """Adds a note that can be consulted later. Use 'txt' for text files or 'url' for website URLs.""" +async def addnotes(ctx, type: str, source: str = None, attachment: discord.Attachment = None): + """Adds a note that can be consulted later. + + 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 user_id = ctx.author.id @@ -4190,13 +4194,48 @@ async def addnotes(ctx, type: str, *, source: str): note_file = os.path.join(user_cache_dir, "notes.txt") if type.lower() == "txt": - if ctx.message.attachments: + # Check for attachment parameter first (Slash Command) + if attachment: + if attachment.content_type and attachment.content_type.startswith('text/'): + try: + content = await attachment.read() + text_content = content.decode('utf-8') + + with open(note_file, "a", encoding="utf-8") as file: + 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}.") + except UnicodeDecodeError: + await ctx.followup.send("❌ Error: File is not a valid text file.") + except Exception as e: + await ctx.followup.send(f"❌ Error reading file: {e}") + else: + await ctx.followup.send("❌ 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] - await attachment.save(note_file) - await ctx.send(f"Text file added as notes for user {ctx.author.name}.") + try: + content = await attachment.read() + text_content = content.decode('utf-8') + + with open(note_file, "a", encoding="utf-8") as file: + 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}.") + except UnicodeDecodeError: + await ctx.send("❌ Error: File is not a valid text file.") + except Exception as e: + await ctx.send(f"❌ Error reading file: {e}") else: - await ctx.send("No text file attached.") + await ctx.followup.send("❌ 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`") + return + try: response = requests.get(source) if response.status_code == 200: @@ -4213,14 +4252,15 @@ async def addnotes(ctx, type: str, *, source: str): cleaned_text = "\n".join([line.strip() for line in text.splitlines() if line.strip()]) 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.send(f"Website content added as notes for user {ctx.author.name}.") + await ctx.followup.send(f"✅ Website content from `{source}` added as notes for user {ctx.author.name}.") else: - await ctx.send(f"Failed to retrieve the website from {source}.") + await ctx.followup.send(f"❌ Failed to retrieve the website from {source}. HTTP {response.status_code}") except Exception as e: - await ctx.send(f"Error fetching website: {e}") + await ctx.followup.send(f"❌ Error fetching website: {e}") else: - await ctx.send("Invalid type. Use 'txt' for text files or 'url' for website URLs.") + await ctx.followup.send("❌ Invalid type. Use 'txt' for text files or 'url' for website URLs.") @client.hybrid_command() async def asknotes(ctx, *, question: str):