mirror of
https://github.com/osmarks/autobotrobot
synced 2025-02-06 20:20:02 +00:00
Add support for IRC actions
This commit is contained in:
parent
9c713a0980
commit
9c114a52f1
@ -27,6 +27,7 @@ class Message:
|
||||
source: (str, any)
|
||||
id: int
|
||||
attachments: list[discord.Attachment]
|
||||
action: bool = False
|
||||
reply: (AuthorInfo, str) = None
|
||||
|
||||
evbus_messages = prometheus_client.Counter("abr_evbus_messages", "Messages processed by event bus", ["source_type"])
|
||||
|
@ -50,10 +50,16 @@ async def initialize():
|
||||
def inuse(conn, event):
|
||||
conn.nick(scramble(conn.get_nickname()))
|
||||
|
||||
def pubmsg(conn, event):
|
||||
msg = eventbus.Message(eventbus.AuthorInfo(event.source.nick, str(event.source), None), [" ".join(event.arguments)], (util.config["irc"]["name"], event.target), util.random_id(), [])
|
||||
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(), [], action=action)
|
||||
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):
|
||||
x = x[:max]
|
||||
while True:
|
||||
@ -62,16 +68,22 @@ async def initialize():
|
||||
except UnicodeDecodeError:
|
||||
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
|
||||
# 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):
|
||||
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 = 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)
|
||||
if reply_line_new != reply_line:
|
||||
reply_line_u += " ..."
|
||||
@ -84,9 +96,9 @@ async def initialize():
|
||||
lines.append(next_line_u)
|
||||
content = content[len(next_line):]
|
||||
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:
|
||||
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:
|
||||
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("nicknameinuse", inuse)
|
||||
conn.add_global_handler("pubmsg", pubmsg)
|
||||
conn.add_global_handler("action", action)
|
||||
|
||||
global unlisten
|
||||
unlisten = eventbus.add_listener(util.config["irc"]["name"], on_bridge_message)
|
||||
|
Loading…
x
Reference in New Issue
Block a user