1
0
mirror of https://github.com/osmarks/autobotrobot synced 2025-02-07 04:30:02 +00:00

Add support for IRC actions

This commit is contained in:
LyricLy 2021-12-21 05:32:08 +13:00
parent 9c713a0980
commit 9c114a52f1
Signed by untrusted user: LyricLy
GPG Key ID: 561E314A55C0B2B8
2 changed files with 21 additions and 7 deletions

View File

@ -27,6 +27,7 @@ class Message:
source: (str, any) source: (str, any)
id: int id: int
attachments: list[discord.Attachment] attachments: list[discord.Attachment]
action: bool = False
reply: (AuthorInfo, str) = None 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"])

View File

@ -50,10 +50,16 @@ async def initialize():
def inuse(conn, event): def inuse(conn, event):
conn.nick(scramble(conn.get_nickname())) conn.nick(scramble(conn.get_nickname()))
def pubmsg(conn, event): def on_msg(conn, event, action):
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(), [], action=action)
asyncio.create_task(eventbus.push(msg)) asyncio.create_task(eventbus.push(msg))
def pubmsg(conn, event):
on_msg(conn, event, False)
def action(conn, event):
on_msg(conn, event, True)
def bytewise_truncate(x, max): def bytewise_truncate(x, max):
x = x[:max] x = x[:max]
while True: while True:
@ -62,16 +68,22 @@ async def initialize():
except UnicodeDecodeError: except UnicodeDecodeError:
x = x[:-1] x = x[:-1]
def render_line(author, content): def render_name(author):
return f"{random_color(author.id)}{author.name[0]}\u200B{author.name[1:]}{color_code('')}"
def render_line(author, content, action):
# colorize for aesthetics # colorize for aesthetics
# add ZWS to prevent pinging # add ZWS to prevent pinging
return f"<{random_color(author.id)}{author.name[0]}\u200B{author.name[1:]}{color_code('')}> {content}" if not action:
return f"<{render_name(author)}> {content}"
else:
return f"* {render_name(author)} {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)
if msg.reply: if msg.reply:
reply_line = render_line(msg.reply[0], render_formatting(msg.reply[1])).encode("utf-8") reply_line = render_line(msg.reply[0], render_formatting(msg.reply[1]), False).encode("utf-8")
reply_line_new, reply_line_u = bytewise_truncate(reply_line, 300) reply_line_new, reply_line_u = bytewise_truncate(reply_line, 300)
if reply_line_new != reply_line: if reply_line_new != reply_line:
reply_line_u += " ..." reply_line_u += " ..."
@ -84,9 +96,9 @@ async def initialize():
lines.append(next_line_u) lines.append(next_line_u)
content = content[len(next_line):] content = content[len(next_line):]
for line in lines: for line in lines:
conn.privmsg(channel_name, render_line(msg.author, line)) conn.privmsg(channel_name, render_line(msg.author, line, msg.action))
for at in msg.attachments: for at in msg.attachments:
conn.privmsg(channel_name, render_line(msg.author, f"-> {at.filename}: {at.proxy_url}")) conn.privmsg(channel_name, render_line(msg.author, f"-> {at.filename}: {at.proxy_url}", False))
else: else:
logging.warning("IRC channel %s not allowed", channel_name) logging.warning("IRC channel %s not allowed", channel_name)
@ -106,6 +118,7 @@ async def initialize():
conn.add_global_handler("disconnect", disconnect) conn.add_global_handler("disconnect", disconnect)
conn.add_global_handler("nicknameinuse", inuse) conn.add_global_handler("nicknameinuse", inuse)
conn.add_global_handler("pubmsg", pubmsg) conn.add_global_handler("pubmsg", pubmsg)
conn.add_global_handler("action", action)
global unlisten global unlisten
unlisten = eventbus.add_listener(util.config["irc"]["name"], on_bridge_message) unlisten = eventbus.add_listener(util.config["irc"]["name"], on_bridge_message)