Skip to content

Commit

Permalink
[Discord] Handle OSError in Bot.on_message_edit event handler method
Browse files Browse the repository at this point in the history
  • Loading branch information
Harmon758 committed Oct 21, 2023
1 parent bd7ffe4 commit adf2576
Showing 1 changed file with 29 additions and 26 deletions.
55 changes: 29 additions & 26 deletions Discord/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -1083,32 +1083,35 @@ async def on_error(self, event_method, *args, **kwargs):
logging.getLogger("errors").error("Uncaught exception\n", exc_info = (error_type, value, error_traceback))

async def on_message_edit(self, before, after):
if after.edited_at != before.edited_at:
if before.content != after.content:
await self.db.execute(
"""
INSERT INTO chat.edits (edited_at, message_id, before_content, after_content)
SELECT $1, $2, $3, $4
WHERE EXISTS (SELECT * FROM chat.messages WHERE chat.messages.message_id = $2)
ON CONFLICT (edited_at, message_id) DO
UPDATE SET before_content = $3, after_content = $4
""",
after.edited_at.replace(tzinfo = datetime.timezone.utc), after.id,
before.content.replace('\N{NULL}', ""), after.content.replace('\N{NULL}', "")
)
before_embeds = [embed.to_dict() for embed in before.embeds]
after_embeds = [embed.to_dict() for embed in after.embeds]
if before_embeds != after_embeds:
await self.db.execute(
"""
INSERT INTO chat.edits (edited_at, message_id, before_embeds, after_embeds)
SELECT $1, $2, $3, $4
WHERE EXISTS (SELECT * FROM chat.messages WHERE chat.messages.message_id = $2)
ON CONFLICT (edited_at, message_id) DO
UPDATE SET before_embeds = CAST($3 AS jsonb[]), after_embeds = CAST($4 AS jsonb[])
""",
after.edited_at.replace(tzinfo = datetime.timezone.utc), after.id, before_embeds, after_embeds
)
try:
if after.edited_at != before.edited_at:
if before.content != after.content:
await self.db.execute(
"""
INSERT INTO chat.edits (edited_at, message_id, before_content, after_content)
SELECT $1, $2, $3, $4
WHERE EXISTS (SELECT * FROM chat.messages WHERE chat.messages.message_id = $2)
ON CONFLICT (edited_at, message_id) DO
UPDATE SET before_content = $3, after_content = $4
""",
after.edited_at.replace(tzinfo = datetime.timezone.utc), after.id,
before.content.replace('\N{NULL}', ""), after.content.replace('\N{NULL}', "")
)
before_embeds = [embed.to_dict() for embed in before.embeds]
after_embeds = [embed.to_dict() for embed in after.embeds]
if before_embeds != after_embeds:
await self.db.execute(
"""
INSERT INTO chat.edits (edited_at, message_id, before_embeds, after_embeds)
SELECT $1, $2, $3, $4
WHERE EXISTS (SELECT * FROM chat.messages WHERE chat.messages.message_id = $2)
ON CONFLICT (edited_at, message_id) DO
UPDATE SET before_embeds = CAST($3 AS jsonb[]), after_embeds = CAST($4 AS jsonb[])
""",
after.edited_at.replace(tzinfo = datetime.timezone.utc), after.id, before_embeds, after_embeds
)
except OSError as e:
self.print(f"Error processing message edit: {e}")

async def on_socket_event_type(self, event_type):
self.socket_events[event_type] = self.socket_events.get(event_type, 0) + 1
Expand Down

0 comments on commit adf2576

Please sign in to comment.