From d44443ba11373245c8f1eead4bcb2f4e1d719e8e Mon Sep 17 00:00:00 2001 From: Jason <81298350+Deutscher775@users.noreply.github.com> Date: Thu, 31 Oct 2024 17:19:02 +0100 Subject: [PATCH] fix bugs - fix type check with attachment file types - fix url cutoff with discord bot (parameters in discord cdn url) - fix nerimity bot errors (seems like the framework has errors TwT ) --- src/Bot/discord.py | 5 +++-- src/Bot/nerimity_bot.py | 28 ++++++++++++++++------------ src/astroidapi/surrealdb_handler.py | 2 +- 3 files changed, 20 insertions(+), 15 deletions(-) diff --git a/src/Bot/discord.py b/src/Bot/discord.py index 2a12477..87f4010 100644 --- a/src/Bot/discord.py +++ b/src/Bot/discord.py @@ -150,15 +150,16 @@ async def on_message(message: nextcord.Message): if message.attachments: print(3) if len(message.attachments) == 1: + print(message.attachments[0].url) # Update the message content with attachment in the API - async with session.post(f"https://astroid.deutscher775.de/update/{message.channel.guild.id}?message_content={message.content}&message_author_name={message.author.name}&message_author_avatar={message.author.avatar.url if message.author.avatar.url else 'https://astroid.deutscher775.de/assets/Astroid PFP not found.png'}&message_author_id={message.author.id}&trigger=true&sender=discord&token={config.MASTER_TOKEN}&sender_channel={message.channel.id}&message_attachments={message.attachments[0].url}") as update_request: + async with session.post(f"https://astroid.deutscher775.de/update/{message.channel.guild.id}?message_content={message.content}&message_author_name={message.author.name}&message_author_avatar={message.author.avatar.url if message.author.avatar.url else 'https://astroid.deutscher775.de/assets/Astroid PFP not found.png'}&message_author_id={message.author.id}&trigger=true&sender=discord&token={config.MASTER_TOKEN}&sender_channel={message.channel.id}&message_attachments={message.attachments[0].url.replace('&', '%26')}") as update_request: pass else: attachments = "" for attachment in message.attachments: attachments += attachment.url # Update the message content with multiple attachments in the API - async with session.post(f"https://astroid.deutscher775.de/update/{message.channel.guild.id}?message_content={message.content}&message_author_name={message.author.name}&message_author_avatar={message.author.avatar.url if message.author.avatar.url else 'https://astroid.deutscher775.de/assets/Astroid PFP not found.png'}&message_author_id={message.author.id}&trigger=true&sender=discord&token={config.MASTER_TOKEN}&sender_channel={message.channel.id}&message_attachments={attachments[:-1]}") as update_request: + async with session.post(f"https://astroid.deutscher775.de/update/{message.channel.guild.id}?message_content={message.content}&message_author_name={message.author.name}&message_author_avatar={message.author.avatar.url if message.author.avatar.url else 'https://astroid.deutscher775.de/assets/Astroid PFP not found.png'}&message_author_id={message.author.id}&trigger=true&sender=discord&token={config.MASTER_TOKEN}&sender_channel={message.channel.id}&message_attachments={attachments[:-1].replace('&', '%26')}") as update_request: pass except KeyError: pass diff --git a/src/Bot/nerimity_bot.py b/src/Bot/nerimity_bot.py index f39ba65..ee0fe28 100644 --- a/src/Bot/nerimity_bot.py +++ b/src/Bot/nerimity_bot.py @@ -123,20 +123,24 @@ async def register(ctx: nerimity.Context, params: str): await ctx.send(f"Registered endpoint: https://api.astroid.cc/{endpoint}") else: await ctx.send(f"Oops, something went wrong: `{data['message']}`") + except AttributeError: + pass except Exception as e: await ctx.send(f"An error occurred while trying to register the endpoint. Please report this in the [Support Server](https://nerimity.com/i/fgE6q). \n\n`{e}`") - - try: - channel_id = ctx.channel.id - async with aiohttp.ClientSession() as session: - async with session.post(f"https://api.astroid.cc/update/{endpoint}?channel_nerimity={channel_id}&token={config.MASTER_TOKEN}") as response: - data = await response.json() - if response.ok: - await ctx.send(f"Updated endpoint: https://api.astroid.cc/{endpoint}") - else: - await ctx.send(f"Oops, something went wrong: `{data['message']}`") - except Exception as e: - await ctx.send(f"An error occurred while trying to update the endpoint. Please report this in the [Support Server](https://nerimity.com/i/fgE6q). \n\n`{e}`") + finally: + try: + channel_id = ctx.channel.id + async with aiohttp.ClientSession() as session: + async with session.post(f"https://api.astroid.cc/update/{endpoint}?channel_nerimity={channel_id}&token={config.MASTER_TOKEN}") as response: + data = await response.json() + if response.ok: + await ctx.send(f"Updated endpoint: https://api.astroid.cc/{endpoint}") + else: + await ctx.send(f"Oops, something went wrong: `{data['message']}`") + except AttributeError: + pass + except Exception as e: + await ctx.send(f"An error occurred while trying to update the endpoint. Please report this in the [Support Server](https://nerimity.com/i/fgE6q). \n\n`{e}`") diff --git a/src/astroidapi/surrealdb_handler.py b/src/astroidapi/surrealdb_handler.py index 21d9305..c9a1ca7 100644 --- a/src/astroidapi/surrealdb_handler.py +++ b/src/astroidapi/surrealdb_handler.py @@ -165,7 +165,7 @@ class AttachmentProcessor: async def create_attachment(cls, attachment_id: str, status: str, type: str, registeredPlatforms: list): if status and status not in ["downloading", "downloaded", "sent", "canDelete"]: raise errors.SurrealDBHandler.CreateAttachmentError(f"Invalid status: {status}") - if type and type not in [".mp4", ".mp3", ".png", ".jpg", ".jpeg", ".gif", ".webp", ".txt", ".json", ".csv", ".xml", ".html", ".css", ".js", ".py", ".java", ".c", ".cpp", ".h", ".hpp", ".cs", ".rb", ".php", ".go", ".rs", ".ts", ".tsx", ".jsx", ".html", ".yaml", ".yml", ".toml", ".ini", ".cfg", ".conf"]: + if type and f".{type}" not in [".mp4", ".mp3", ".png", ".jpg", ".jpeg", ".gif", ".webp", ".txt", ".json", ".csv", ".xml", ".html", ".css", ".js", ".py", ".java", ".c", ".cpp", ".h", ".hpp", ".cs", ".rb", ".php", ".go", ".rs", ".ts", ".tsx", ".jsx", ".html", ".yaml", ".yml", ".toml", ".ini", ".cfg", ".conf"]: raise errors.SurrealDBHandler.CreateAttachmentError(f"Invalid type: {type}") try: async with Surreal(config.SDB_URL) as db: