bridge attachments, other fixes

This commit is contained in:
osmarks 2021-07-28 20:30:37 +01:00
parent 4700a2f389
commit ba2ce97290
5 changed files with 27 additions and 15 deletions

View File

@ -4,6 +4,7 @@ import dataclasses
import typing
import collections
import logging
import discord
import util
@ -25,6 +26,7 @@ class Message:
message: list[typing.Union[str, dict]]
source: (str, any)
id: int
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"])

View File

@ -51,7 +51,7 @@ async def initialize():
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())
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))
async def on_bridge_message(channel_name, msg):
@ -60,6 +60,8 @@ async def initialize():
# 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)

View File

@ -86,7 +86,7 @@ def setup(bot):
created_timestamp = datetime.utcfromtimestamp(created_timestamp).replace(tzinfo=timezone.utc)
extra = json.loads(extra)
uid = extra["author_id"]
tz = await util.get_user_timezone(util.AltCtx(util.IDWrapper(uid), util.IDWrapper(None), bot))
tz = await util.get_user_timezone(util.AltCtx(util.IDWrapper(uid), util.IDWrapper(extra.get("guild_id")), bot))
print(created_timestamp, tz, created_timestamp.astimezone(tz))
created_time = util.format_time(created_timestamp.astimezone(tz))
text = f"<@{uid}> Reminder queued at {created_time}: {reminder_text}"

View File

@ -87,29 +87,34 @@ class Telephone(commands.Cog):
self.webhooks[row["channel_id"]] = row["webhook"]
logging.info("Loaded %d webhooks", len(rows))
async def on_bridge_message(self, channel_id, msg):
async def on_bridge_message(self, channel_id, msg: eventbus.Message):
channel = self.bot.get_channel(channel_id)
if channel:
webhook = self.webhooks.get(channel_id)
if webhook:
try:
self.webhook_queue.put_nowait((webhook, render_formatting(channel, msg.message)[:2000], msg.author.name, msg.author.avatar_url))
except asyncio.QueueFull:
text = f"<{msg.author.name}> {render_formatting(channel, msg.message)}"
attachments_text = "\n".join(f"{at.filename}: {at.proxy_url}" for at in msg.attachments)
async def send_raw(text):
if webhook:
try:
self.webhook_queue.put_nowait((webhook, text, msg.author.name, msg.author.avatar_url))
except asyncio.QueueFull:
text = f"<{msg.author.name}> {text}"
await channel.send(text[:2000], allowed_mentions=discord.AllowedMentions(everyone=False, roles=False, users=False))
else:
text = f"<{msg.author.name}> {text}"
await channel.send(text[:2000], allowed_mentions=discord.AllowedMentions(everyone=False, roles=False, users=False))
else:
text = f"<{msg.author.name}> {render_formatting(channel, msg.message)}"
await channel.send(text[:2000], allowed_mentions=discord.AllowedMentions(everyone=False, roles=False, users=False))
await send_raw(render_formatting(channel, msg.message)[:2000])
if attachments_text: await send_raw(attachments_text)
else:
logging.warning("Channel %d not found", channel_id)
@commands.Cog.listener("on_message")
async def send_to_bridge(self, msg):
# discard webhooks and bridge messages (hackily, admittedly, not sure how else to do this)
if msg.content == "": 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
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)
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() ])
await eventbus.push(msg)
def cog_unload(self):
@ -136,7 +141,7 @@ When you want to end a call, use hangup.
@telephone.command(brief="Link to other channels", help="""Connect to another channel on Discord or any supported bridges.
Virtual channels also exist.
""")
@commands.check(util.admin_check)
@commands.check(util.extpriv_check)
async def link(self, ctx, target_type, target_id, bidirectional: bool = True):
target_id = util.extract_codeblock(target_id)
try:
@ -147,7 +152,7 @@ When you want to end a call, use hangup.
pass
@telephone.command(brief="Undo link commands.")
@commands.check(util.admin_check)
@commands.check(util.extpriv_check)
async def unlink(self, ctx, target_type, target_id, bidirectional: bool = True):
target_id = util.extract_codeblock(target_id)
try:

View File

@ -255,6 +255,9 @@ async def server_mod_check(ctx):
async def admin_check(ctx):
return await ctx.bot.is_owner(ctx.author)
async def extpriv_check(ctx):
return await ctx.bot.is_owner(ctx.author) or ctx.author.id in config["extpriv_users"]
async def get_asset(bot: commands.Bot, identifier):
safe_ident = re.sub("[^A-Za-z0-9_.-]", "_", identifier)
x = await bot.database.execute_fetchone("SELECT * FROM assets WHERE identifier = ?", (safe_ident,))