mirror of
				https://github.com/osmarks/autobotrobot
				synced 2025-10-31 16:02:59 +00:00 
			
		
		
		
	Improve bridging capabilities
This commit is contained in:
		| @@ -27,6 +27,7 @@ class Message: | |||||||
|     source: (str, any) |     source: (str, any) | ||||||
|     id: int |     id: int | ||||||
|     attachments: list[discord.Attachment] |     attachments: list[discord.Attachment] | ||||||
|  |     reply: (AuthorInfo, str) = None | ||||||
|  |  | ||||||
| evbus_messages = prometheus_client.Counter("abr_evbus_messages", "Messages processed by event bus", ["source_type"]) | 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"]) | evbus_messages_dropped = prometheus_client.Counter("abr_evbus_messages_dropped", "Messages received by event bus but dropped by rate limits", ["source_type"]) | ||||||
|   | |||||||
| @@ -54,14 +54,39 @@ async def initialize(): | |||||||
|         msg = eventbus.Message(eventbus.AuthorInfo(event.source.nick, str(event.source), None), [" ".join(event.arguments)], (util.config["irc"]["name"], event.target), util.random_id(), []) |         msg = eventbus.Message(eventbus.AuthorInfo(event.source.nick, str(event.source), None), [" ".join(event.arguments)], (util.config["irc"]["name"], event.target), util.random_id(), []) | ||||||
|         asyncio.create_task(eventbus.push(msg)) |         asyncio.create_task(eventbus.push(msg)) | ||||||
|  |  | ||||||
|  |     def bytewise_truncate(x, max): | ||||||
|  |         x = x[:max] | ||||||
|  |         while True: | ||||||
|  |             try: | ||||||
|  |                 return x, x.decode("utf-8") | ||||||
|  |             except UnicodeDecodeError: | ||||||
|  |                 x = x[:-1] | ||||||
|  |  | ||||||
|  |     def render_line(author, content): | ||||||
|  |         # colorize for aesthetics | ||||||
|  |         # add ZWS to prevent pinging | ||||||
|  |         return f"<{random_color(author.id)}{author.name[0]}\u200B{author.name[1:]}{color_code('')}> {content}" | ||||||
|  |  | ||||||
|     async def on_bridge_message(channel_name, msg): |     async def on_bridge_message(channel_name, msg): | ||||||
|         if channel_name in util.config["irc"]["channels"]: |         if channel_name in util.config["irc"]["channels"]: | ||||||
|             if channel_name not in joined: conn.join(channel_name) |             if channel_name not in joined: conn.join(channel_name) | ||||||
|             # ping fix - zero width space embedded in messages |             if msg.reply: | ||||||
|             line = f"<{random_color(msg.author.id)}{msg.author.name[0]}\u200B{msg.author.name[1:]}{color_code('')}> " + render_formatting(msg.message)[:400] |                 reply_line = render_line(msg.reply[0], render_formatting(msg.reply[1])).encode("utf-8") | ||||||
|             conn.privmsg(channel_name, line) |                 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}]") | ||||||
|  |             lines = [] | ||||||
|  |             content = render_formatting(msg.message).encode("utf-8") | ||||||
|  |             # somewhat accursedly break string into valid UTF-8 substrings with <=400 bytes | ||||||
|  |             while content: | ||||||
|  |                 next_line, next_line_u = bytewise_truncate(content, 400) | ||||||
|  |                 lines.append(next_line_u) | ||||||
|  |                 content = content[len(next_line):] | ||||||
|  |             for line in lines: | ||||||
|  |                 conn.privmsg(channel_name, render_line(msg.author, line)) | ||||||
|             for at in msg.attachments: |             for at in msg.attachments: | ||||||
|                 conn.privmsg(channel_name, f"-> {at.filename}: {at.proxy_url}") |                 conn.privmsg(channel_name, render_line(msg.author, f"-> {at.filename}: {at.proxy_url}")) | ||||||
|         else: |         else: | ||||||
|             logging.warning("IRC channel %s not allowed", channel_name) |             logging.warning("IRC channel %s not allowed", channel_name) | ||||||
|  |  | ||||||
|   | |||||||
| @@ -113,8 +113,12 @@ class Telephone(commands.Cog): | |||||||
|         if msg.content == "" and len(msg.attachments) == 0: return |         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 |         if (msg.author == self.bot.user and msg.content[0] == "<") or msg.author.discriminator == "0000": return | ||||||
|         channel_id = msg.channel.id |         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)) | ||||||
|         msg = eventbus.Message(eventbus.AuthorInfo(msg.author.name, msg.author.id, str(msg.author.avatar_url), msg.author.bot),  |         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() ]) |             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) |         await eventbus.push(msg) | ||||||
|  |  | ||||||
|     def cog_unload(self): |     def cog_unload(self): | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user