Big ass update

This commit is contained in:
mid
2025-12-28 09:51:55 +02:00
parent 070aee431e
commit f0a7869888
4 changed files with 320 additions and 19 deletions

View File

@@ -1,5 +1,5 @@
import os, re, hashlib, sys, traceback
import asyncio
import asyncio, aiohttp
import nextcord
class GatewayDiscord(nextcord.Client):
@@ -28,9 +28,14 @@ class GatewayDiscord(nextcord.Client):
self.active_users = {}
self.new_user_queue = []
self.avatar_cache_url = {}
self.avatar_cache = {}
self.webhook_avatars = {}
def new_user(identification, nick_name):
if identification[0] == "discord" and identification[1] == self.gateway_name:
# Ignore ours
return
self.new_user_queue.append(identification)
@@ -39,7 +44,7 @@ class GatewayDiscord(nextcord.Client):
self.handle_new_user_queue()
PLEXUS.sub("new_user", new_user)
def message(msg_unique_id, room_name, nick_name, gateway_type, gateway_name, unique_id, body, attachments, was_edit):
def message(msg_unique_id, room_name, nick_name, avatar, gateway_type, gateway_name, unique_id, body, attachments, was_edit):
if gateway_type == "discord" and gateway_name == self.gateway_name:
# Ignore ours
return
@@ -57,12 +62,22 @@ class GatewayDiscord(nextcord.Client):
asyncio.ensure_future(wh.edit_message(self.xdm_id_to_discord_id[msg_unique_id], content = str(body)))
else:
def on_done(fut):
wh_msg = fut.result()
async def aw():
if avatar is None:
self.webhook_avatars[wh.id] = None
await wh.edit(avatar = None)
else:
current_avatar_hash = hashlib.sha256(avatar).hexdigest()
if self.webhook_avatars[wh.id] != current_avatar_hash:
self.webhook_avatars[wh.id] = current_avatar_hash
await wh.edit(avatar = avatar)
wh_msg = await wh.send(content = str(body), username = nick_name, wait = True, allowed_mentions = nextcord.AllowedMentions(everyone = False))
self.xdm_id_to_discord_id[msg_unique_id] = wh_msg.id
self.discord_id_to_xdm_id[wh_msg.id] = msg_unique_id
asyncio.ensure_future(wh.send(content = str(body), username = nick_name, wait = True, allowed_mentions = nextcord.AllowedMentions(everyone = False))).add_done_callback(on_done)
asyncio.ensure_future(aw())
PLEXUS.sub("message", message)
PLEXUS.sub("ready", lambda: asyncio.ensure_future(self.start(gateway_data["token"])))
@@ -77,6 +92,16 @@ class GatewayDiscord(nextcord.Client):
self.active_users[member.id] = {}
PLEXUS.pub("new_user", ("discord", self.gateway_name, member.id), member.display_name)
async def cache_avatar(self, memb):
avatar_url = memb.display_avatar.url
avatar_url = avatar_url.split("?")[0] + "?size=128"
if (memb.id not in self.avatar_cache_url) or (self.avatar_cache_url[memb.id] != avatar_url):
self.avatar_cache_url[memb.id] = avatar_url
async with aiohttp.ClientSession() as session:
async with session.get(avatar_url) as resp:
self.avatar_cache[memb.id] = await resp.read()
async def on_message(self, message):
if message.author.bot:
# Ignore ours
@@ -99,7 +124,9 @@ class GatewayDiscord(nextcord.Client):
self.xdm_id_to_discord_id[msg_unique_id] = message.id
self.discord_id_to_xdm_id[message.id] = msg_unique_id
PLEXUS.pub("message", msg_unique_id, room_name, message.author.display_name, "discord", self.gateway_name, message.author.id, message.clean_content, [a.url for a in message.attachments], False)
await self.cache_avatar(message.author)
PLEXUS.pub("message", msg_unique_id, room_name, message.author.display_name, self.avatar_cache[message.author.id], "discord", self.gateway_name, message.author.id, message.clean_content, [a.url for a in message.attachments], False)
async def on_message_edit(self, before, after):
if before.author.bot:
@@ -123,9 +150,11 @@ class GatewayDiscord(nextcord.Client):
if before.id not in self.discord_id_to_xdm_id:
return
await self.cache_avatar(after.author)
msg_unique_id = self.discord_id_to_xdm_id[after.id]
room_name = self.pair_to_room_name[(guild_id, channel_id)]
PLEXUS.pub("message", msg_unique_id, room_name, after.author.display_name, "discord", self.gateway_name, after.author.id, after.clean_content, [a.url for a in after.attachments], True)
PLEXUS.pub("message", msg_unique_id, room_name, after.author.display_name, self.avatar_cache[after.author.id], "discord", self.gateway_name, after.author.id, after.clean_content, [a.url for a in after.attachments], True)
async def on_message_delete(self, message):
if message.author.bot:
@@ -169,6 +198,7 @@ class GatewayDiscord(nextcord.Client):
wh = await discord_channel.create_webhook(name = "xdm", reason = "XDM bridge")
self.webhooks[gateway["channel"]] = wh
self.webhook_avatars[wh.id] = None
self.room_name_to_pair[room_name] = (gateway["guild"], gateway["channel"])
self.pair_to_room_name[(gateway["guild"], gateway["channel"])] = room_name