From f03b5167b4ef291cfdc931b936ebebf12b04bda1 Mon Sep 17 00:00:00 2001 From: git-bruh Date: Sat, 14 Nov 2020 21:33:06 +0530 Subject: [PATCH] Allow mentioning multiple discord users in a matrix message --- main.py | 42 ++++++++++++++++++------------------------ 1 file changed, 18 insertions(+), 24 deletions(-) diff --git a/main.py b/main.py index 1914610..d28b943 100644 --- a/main.py +++ b/main.py @@ -51,7 +51,7 @@ async def on_message(message): # Replace mention/emote IDs with names content = await process(message.content, "emote_") - content = await process(content, "mention") + content = await process(content, "mention_") content = f"<{message.author.name}> {content}" @@ -95,8 +95,17 @@ async def process(message, category): start_ = 2 end = ">" - # Replace mentioned user IDs with names (Discord -> Matrix) + # Replace mentioned user names with IDs (Matrix -> Discord) elif category == "mention": + channel = await get_channel() + guild = channel.guild + + start = "@" + start_ = 0 + end = "" + + # Replace mentioned user IDs with names (Discord -> Matrix) + elif category == "mention_": start = "<@!" start_ = 3 end = ">" @@ -115,6 +124,12 @@ async def process(message, category): message = message.replace(item, f":{emote_name}:") elif category == "mention": + user = item[1:] + + for member in await guild.query_members(query=user): + message = message.replace(item, member.mention) + + elif category == "mention_": user = discord_client.get_user(int(item_)) message = message.replace(item, f"@{user.name}") @@ -137,22 +152,6 @@ async def webhook_send(author, avatar, message): await hook.send(username=author, avatar_url=avatar, content=message) -async def partial_mention(user): - channel = await get_channel() - - # Get guild to parse member list - guild = channel.guild - - # Remove "@" - user = user[1:] - - for member in await guild.query_members(query=user): - user_mention = f"<@!{member.id}>" - return user_mention - - return None - - async def create_matrix_client(): homeserver = config["homeserver"] username = config["username"] @@ -216,12 +215,7 @@ async def message_callback(room, event): message = message.replace("@here", "@\u200Bhere") # Replace partial mention of Discord user with ID - if message.startswith("@"): - user = message.split()[0] - user_mention = await partial_mention(user) - - if user_mention is not None: - message = message.replace(user, user_mention) + message = await process(message, "mention") # Get attachments try: