modified: bot.py
This commit is contained in:
62
bot.py
62
bot.py
@@ -4176,8 +4176,12 @@ async def download_and_save_profile_image(user_id, discord_url):
|
|||||||
# Cache-Ordner für Notizen
|
# Cache-Ordner für Notizen
|
||||||
|
|
||||||
@client.hybrid_command()
|
@client.hybrid_command()
|
||||||
async def addnotes(ctx, type: str, *, source: str):
|
async def addnotes(ctx, type: str, source: str = None, attachment: discord.Attachment = None):
|
||||||
"""Adds a note that can be consulted later. Use 'txt' for text files or 'url' for website URLs."""
|
"""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
|
await ctx.defer() # Signalisiert, dass die Bearbeitung des Befehls begonnen hat
|
||||||
|
|
||||||
user_id = ctx.author.id
|
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")
|
note_file = os.path.join(user_cache_dir, "notes.txt")
|
||||||
|
|
||||||
if type.lower() == "txt":
|
if type.lower() == "txt":
|
||||||
if ctx.message.attachments:
|
# Check for attachment parameter first (Slash Command)
|
||||||
attachment = ctx.message.attachments[0]
|
if attachment:
|
||||||
await attachment.save(note_file)
|
if attachment.content_type and attachment.content_type.startswith('text/'):
|
||||||
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.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:
|
else:
|
||||||
await ctx.send("No text file attached.")
|
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]
|
||||||
|
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.followup.send("❌ No text file attached. Please use the attachment parameter for Slash Commands.")
|
||||||
|
|
||||||
elif type.lower() == "url":
|
elif type.lower() == "url":
|
||||||
|
if not source:
|
||||||
|
await ctx.followup.send("❌ Please provide a URL: `/addnotes url https://example.com`")
|
||||||
|
return
|
||||||
|
|
||||||
try:
|
try:
|
||||||
response = requests.get(source)
|
response = requests.get(source)
|
||||||
if response.status_code == 200:
|
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()])
|
cleaned_text = "\n".join([line.strip() for line in text.splitlines() if line.strip()])
|
||||||
|
|
||||||
with open(note_file, "a", encoding="utf-8") as file:
|
with open(note_file, "a", encoding="utf-8") as file:
|
||||||
|
file.write(f"\n--- From URL: {source} ---\n")
|
||||||
file.write(cleaned_text + "\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:
|
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:
|
except Exception as e:
|
||||||
await ctx.send(f"Error fetching website: {e}")
|
await ctx.followup.send(f"❌ Error fetching website: {e}")
|
||||||
else:
|
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()
|
@client.hybrid_command()
|
||||||
async def asknotes(ctx, *, question: str):
|
async def asknotes(ctx, *, question: str):
|
||||||
|
|||||||
Reference in New Issue
Block a user