From f9af29a5c779c2a6c2a74d7f0b1ee8abd4962ef9 Mon Sep 17 00:00:00 2001 From: Frisk Date: Sat, 10 Aug 2024 14:59:37 +0200 Subject: [PATCH] Few fixes --- src/discord/message.py | 26 ++++++++++++++++---------- src/discord/queue.py | 6 +++--- src/wiki.py | 13 ++++++------- 3 files changed, 25 insertions(+), 20 deletions(-) diff --git a/src/discord/message.py b/src/discord/message.py index 662b0ea..d226146 100644 --- a/src/discord/message.py +++ b/src/discord/message.py @@ -48,9 +48,11 @@ class DiscordMessageMetadata: return f"" def __getstate__(self): - obj_copy = self.__dict__.copy() - del obj_copy["domain"] - return obj_copy + if "domain" in self.__dict__: + obj_copy = self.__dict__.copy() + del obj_copy['domain'] + return obj_copy + return super().__getstate__() def json(self) -> dict: dict_obj = { @@ -107,7 +109,7 @@ class DiscordMessage: raise TypeError("Tried to assign a value when message type is plain message!") def __getitem__(self, item): - return self.embed[item] + return self.embed.__getitem__(item) def __repr__(self): """Return the Discord webhook object ready to be sent""" @@ -117,9 +119,11 @@ class DiscordMessage: return self.length def __getstate__(self): - obj_copy = self.__dict__.copy() - del obj_copy['wiki'] - return obj_copy + if "wiki" in self.__dict__: + obj_copy = self.__dict__.copy() + del obj_copy['wiki'] + return obj_copy + return super().__getstate__() def json(self): dict_obj = { @@ -232,9 +236,11 @@ class StackedDiscordMessage: return self.message_list.__iter__() def __getstate__(self): - obj_copy = self.__dict__.copy() - del obj_copy['wiki'] - return obj_copy + if "wiki" in self.__dict__: + obj_copy = self.__dict__.copy() + del obj_copy['wiki'] + return obj_copy + return super().__getstate__() def is_empty(self): return len(self.message_list) == 0 diff --git a/src/discord/queue.py b/src/discord/queue.py index 4eae6c2..6fc29ce 100644 --- a/src/discord/queue.py +++ b/src/discord/queue.py @@ -265,7 +265,7 @@ async def send_to_discord_webhook(message: [StackedDiscordMessage, DiscordMessag header['Content-Type'] = 'application/json' header['X-RateLimit-Precision'] = "millisecond" async with aiohttp.ClientSession(headers=header, timeout=aiohttp.ClientTimeout(total=6)) as session: - if isinstance(message, StackedDiscordMessage): + if method == "POST": async with session.post(f"https://discord.com/api/webhooks/{webhook_path}?wait=true", data=repr(message)) as resp: # TODO Detect Invalid Webhook Token try: resp_json = await resp.json() @@ -279,8 +279,8 @@ async def send_to_discord_webhook(message: [StackedDiscordMessage, DiscordMessag logger.exception(f"Could not decode JSON response from Discord. Response: {await resp.text()}]") return await handle_discord_http(resp.status, repr(message), resp) elif method == "DELETE": - async with session.request(method=message.method, url=f"https://discord.com/api/webhooks/{webhook_path}/messages/{message.discord_callback_message_id}") as resp: + async with session.request(method=method, url=f"https://discord.com/api/webhooks/{webhook_path}/messages/{message.discord_callback_message_id}") as resp: return await handle_discord_http(resp.status, repr(message), resp) elif method == "PATCH": - async with session.request(method=message.method, url=f"https://discord.com/api/webhooks/{webhook_path}/messages/{message.discord_callback_message_id}", data=repr(message)) as resp: + async with session.request(method=method, url=f"https://discord.com/api/webhooks/{webhook_path}/messages/{message.discord_callback_message_id}", data=repr(message)) as resp: return await handle_discord_http(resp.status, repr(message), resp) diff --git a/src/wiki.py b/src/wiki.py index f0cc227..a6862a7 100644 --- a/src/wiki.py +++ b/src/wiki.py @@ -79,7 +79,7 @@ class MessageHistory: async def find_all_revids(self, page_id: int) -> list[int]: """Function to find all revisions for a page in message history""" result = [] - async for item in dbmanager.fetch_rows(f"SELECT DISTINCT rev_id FROM rcgcdb_msg_metadata INNER JOIN rcgcdb_msg_metadata ON rcgcdb_msg_metadata.message_id = rcgcdb_msg_history.message_id INNER JOIN rcgcdb ON rcgcdb_msg_history.webhook = rcgcdb.webhook WHERE rcgcdb.wiki = $1 AND page_id = $2", (self.wiki.script_url, page_id)): + async for item in dbmanager.fetch_rows(f"SELECT DISTINCT rev_id FROM rcgcdb_msg_metadata INNER JOIN rcgcdb_msg_history ON rcgcdb_msg_metadata.message_id = rcgcdb_msg_history.message_id INNER JOIN rcgcdb ON rcgcdb_msg_history.webhook = rcgcdb.webhook WHERE rcgcdb.wiki = $1 AND page_id = $2", self.wiki.script_url, page_id): result.append(item["rev_id"]) return result @@ -107,7 +107,7 @@ class MessageHistory: @staticmethod def register_message(stacked_message: StackedDiscordMessage): """Registers a message in the database""" - dbmanager.add(("INSERT INTO rcgcdb_msg_history(message_id, webhook, message_object) VALUES ($1, $2, $3);", + dbmanager.add(("INSERT INTO rcgcdb_msg_history(message_id, webhook, message_object) VALUES ($1, $2, $3) ON CONFLICT (message_id) DO UPDATE SET message_object = $3;", (stacked_message.discord_callback_message_id, stacked_message.webhook, pickle.dumps(stacked_message)))) for stack_id, message in enumerate(stacked_message): dbmanager.add(("INSERT INTO rcgcdb_msg_metadata(message_id, log_id, page_id, rev_id, message_display, stacked_index) VALUES ($1, $2, $3, $4, $5, $6);", @@ -153,7 +153,6 @@ class Wiki: "namespaces": self.namespaces, "tags": self.tags, "recache_requested": self.recache_requested, - "cached_discord_messages": len(self.message_history), "discord_message_history": [message.json() for message in self.message_history[-10:]] } if self.domain.name == "fandom.com": @@ -238,11 +237,11 @@ class Wiki: if "user" in censored_properties and "url" in message["author"]: message["author"]["name"] = context._("hidden") message["author"].pop("url") - if "action" in censored_properties and "url" in message: + if "action" in censored_properties and "url" in message.embed: message["title"] = context._("~~hidden~~") - message["embed"].pop("url") - if "content" in censored_properties and "fields" in message: - message["embed"].pop("fields") + message.embed.pop("url") + if "content" in censored_properties and "fields" in message.embed: + message.embed.pop("fields") if "comment" in censored_properties: message["description"] = context._("~~hidden~~") logger.debug(f"Rev-deleting contents of message {stacked_message.discord_callback_message_id} due to being in list of ids {ids}.")