From 8bc3277c0ed853b14aee52d1fe63f138775fe3b5 Mon Sep 17 00:00:00 2001 From: LyricLy Date: Tue, 9 Nov 2021 21:11:15 +1300 Subject: [PATCH] Improve support for bridging replies --- src/irc_link.py | 13 ++++++++----- src/telephone.py | 21 +++++++++++++++++---- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/irc_link.py b/src/irc_link.py index b4536ea..c054dbe 100644 --- a/src/irc_link.py +++ b/src/irc_link.py @@ -71,11 +71,14 @@ async def initialize(): if channel_name in util.config["irc"]["channels"]: if channel_name not in joined: conn.join(channel_name) if msg.reply: - reply_line = render_line(msg.reply[0], render_formatting(msg.reply[1])).encode("utf-8") - reply_line_new, reply_line_u = bytewise_truncate(reply_line, 300) - if reply_line_new != reply_line: - reply_line_u += " ..." - conn.privmsg(channel_name, f"[Replying to {reply_line_u}]") + if msg.reply[0] and msg.reply[1]: + reply_line = render_line(msg.reply[0], render_formatting(msg.reply[1])).encode("utf-8") + reply_line_new, reply_line_u = bytewise_truncate(reply_line, 300) + if reply_line_new != reply_line: + reply_line_u += " ..." + conn.privmsg(channel_name, f"[Replying to {reply_line_u}]") + else: + conn.privmsg(channel_name, "[Replying to an unknown message]") lines = [] content = render_formatting(msg.message).encode("utf-8") # somewhat accursedly break string into valid UTF-8 substrings with <=400 bytes diff --git a/src/telephone.py b/src/telephone.py index 64d3de7..e27f0c4 100644 --- a/src/telephone.py +++ b/src/telephone.py @@ -114,9 +114,22 @@ class Telephone(commands.Cog): if (msg.author == self.bot.user and msg.content[0] == "<") or msg.author.discriminator == "0000": return channel_id = msg.channel.id reply = None - if msg.reference and msg.reference.cached_message: - replying_to = msg.reference.cached_message - reply = (eventbus.AuthorInfo(replying_to.author.name, replying_to.author.id, str(replying_to.author.avatar_url), replying_to.author.bot), parse_formatting(self.bot, replying_to.content)) + if msg.reference: + if isinstance(msg.reference.resolved, discord.DeletedReferencedMessage): + replying_to = None + elif msg.reference.resolved: + replying_to = msg.reference.resolved + elif msg.reference.cached_message: + replying_to = msg.reference.cached_message + else: + try: + replying_to = await self.bot.get_guild(msg.reference.guild_id).get_channel(msg.reference.channel_id).fetch_message(msg.reference.message_id) + except discord.HTTPException: + replying_to = None + if replying_to: + reply = (eventbus.AuthorInfo(replying_to.author.name, replying_to.author.id, str(replying_to.author.avatar_url), replying_to.author.bot), parse_formatting(self.bot, replying_to.content)) + else: + reply = (None, None) msg = eventbus.Message(eventbus.AuthorInfo(msg.author.name, msg.author.id, str(msg.author.avatar_url), msg.author.bot), parse_formatting(self.bot, msg.content), ("discord", channel_id), msg.id, [ at for at in msg.attachments if not at.is_spoiler() ], reply=reply) await eventbus.push(msg) @@ -335,4 +348,4 @@ When you want to end a call, use hangup. def setup(bot): cog = Telephone(bot) bot.add_cog(cog) - asyncio.create_task(cog.initial_load_webhooks()) \ No newline at end of file + asyncio.create_task(cog.initial_load_webhooks())