From abc1eb15be6201bb1a13dc92086c10cdb8c5f22f Mon Sep 17 00:00:00 2001 From: all-other-usernames-were-taken <74026992+all-other-usernames-were-taken@users.noreply.github.com> Date: Wed, 6 Oct 2021 16:33:50 -0400 Subject: [PATCH] use preexisting message data & clean up code a bit --- src/eventbus.py | 3 ++- src/irc_link.py | 11 ++++++++--- src/telephone.py | 29 ++++++++++++++++++++++++++--- 3 files changed, 36 insertions(+), 7 deletions(-) diff --git a/src/eventbus.py b/src/eventbus.py index 58972b2..c06ebda 100644 --- a/src/eventbus.py +++ b/src/eventbus.py @@ -26,12 +26,13 @@ class Message: message: list[typing.Union[str, dict]] source: (str, any) id: int + replyee: AuthorInfo attachments: list[discord.Attachment] evbus_messages = prometheus_client.Counter("abr_evbus_messages", "Messages processed by event bus", ["source_type"]) evbus_messages_dropped = prometheus_client.Counter("abr_evbus_messages_dropped", "Messages received by event bus but dropped by rate limits", ["source_type"]) -# maps each bridge destination type (discord/APIONET/etc) to the listeners for it. +# maps each bridge destination type (discord/APIONET/etc) to the listeners for it listeners = collections.defaultdict(set) # maintains a list of all the unidirectional links between channels - key is source, values are targets diff --git a/src/irc_link.py b/src/irc_link.py index b946182..b9cd3b9 100644 --- a/src/irc_link.py +++ b/src/irc_link.py @@ -58,16 +58,21 @@ async def initialize(): async def on_bridge_message(channel_name, msg): if channel_name in util.config["irc"]["channels"]: + if channel_name not in joined: conn.join(channel_name) + # if its a reply - dmsg = discord.fetch_message(msg.id) - if dmsg.reference.message_id: - conn.privmessage(channel_name, f"Replying to @{random_color(dmsg.reference.cached_message.author.id)}{dmsg.reference.cached_message.author.name}{color_code('')}:") + if msg.replyee: + conn.privmessage (channel_name, f"Replying to @{random_color(msg.replyee.id)}{msg.replyee.name}{color_code('')}:") + # ping fix - zero width space embedded in messages line = f"<{random_color(msg.author.id)}{msg.author.name[0]}\u200B{msg.author.name[1:]}{color_code('')}> " + render_formatting(msg.message)[:400] + conn.privmsg(channel_name, line) + for at in msg.attachments: conn.privmsg(channel_name, f"-> {at.filename}: {at.proxy_url}") + else: logging.warning("IRC channel %s not allowed", channel_name) diff --git a/src/telephone.py b/src/telephone.py index f9c2dbc..36f694c 100644 --- a/src/telephone.py +++ b/src/telephone.py @@ -113,8 +113,31 @@ class Telephone(commands.Cog): if msg.content == "" and len(msg.attachments) == 0: return if (msg.author == self.bot.user and msg.content[0] == "<") or msg.author.discriminator == "0000": return channel_id = msg.channel.id - 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() ]) + + 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, + + eventbus.AuthorInfo( + msg.reference.cached_message.author.name, + msg.reference.cached_message.author.id, + str(msg.reference.cached_message.author.avatar_url), + msg.reference.cached_message.author.bot + ), + + [ at for at in msg.attachments if not at.is_spoiler() ] + ) + await eventbus.push(msg) def cog_unload(self): @@ -331,4 +354,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())