aioappsrv/main.py

387 lines
12 KiB
Python
Raw Normal View History

import discord
2020-11-11 16:46:15 +01:00
import json
import logging
2020-12-01 07:38:59 +01:00
import nio
import os
2020-12-01 07:38:59 +01:00
import re
2020-11-11 16:46:15 +01:00
def config_gen(config_file):
config_dict = {
"homeserver": "https://matrix.org",
"username": "@name:matrix.org",
"password": "my-secret-password",
"token": "my-secret-token",
"bridge": {"channel_id": "room_id", }
2020-11-11 16:46:15 +01:00
}
if not os.path.exists(config_file):
with open(config_file, "w") as f:
json.dump(config_dict, f, indent=4)
print(f"Example configuration dumped to {config_file}")
exit()
with open(config_file, "r") as f:
config = json.loads(f.read())
return config
config = config_gen("config.json")
2020-12-30 15:31:18 +01:00
message_store, channel_store = {}, {}
2020-11-14 16:14:16 +01:00
2020-12-01 09:27:36 +01:00
class MatrixClient(nio.AsyncClient):
2020-12-07 14:58:05 +01:00
async def create(self, discord_client):
2020-12-30 15:31:18 +01:00
self.logger = logging.getLogger("matrix_logger")
2020-11-30 13:35:49 +01:00
password = config["password"]
2020-11-30 16:44:34 +01:00
timeout = 30000
2020-11-14 16:14:16 +01:00
2020-12-30 15:31:18 +01:00
self.logger.info(await self.login(password))
2020-12-30 15:31:18 +01:00
self.logger.info("Doing initial sync.")
2020-12-01 09:27:36 +01:00
await self.sync(timeout)
2020-11-30 13:35:49 +01:00
# Set up event callbacks
2020-12-30 15:31:18 +01:00
callbacks = Callbacks(self)
2020-12-01 09:27:36 +01:00
self.add_event_callback(
2020-11-30 13:35:49 +01:00
callbacks.message_callback,
(nio.RoomMessageText, nio.RoomMessageMedia,
2020-12-13 08:02:20 +01:00
nio.RoomMessageEmote)
)
2020-12-01 09:27:36 +01:00
self.add_event_callback(
2020-12-13 08:02:20 +01:00
callbacks.redaction_callback, nio.RedactionEvent
)
2020-11-11 16:46:15 +01:00
2020-12-01 09:27:36 +01:00
self.add_ephemeral_callback(
2020-12-13 08:02:20 +01:00
callbacks.typing_callback, nio.EphemeralEvent
)
2020-12-07 14:58:05 +01:00
await discord_client.wait_until_ready()
2020-12-30 15:31:18 +01:00
self.logger.info("Syncing forever.")
2020-12-01 09:27:36 +01:00
await self.sync_forever(timeout=timeout)
2020-11-30 16:44:34 +01:00
2020-12-01 09:27:36 +01:00
await self.close()
2020-11-30 16:44:34 +01:00
async def message_send(self, message, channel_id,
reply_id=None, edit_id=None):
room_id = config["bridge"][str(channel_id)]
2020-11-30 13:35:49 +01:00
content = {
"msgtype": "m.text",
"body": message,
}
2020-11-30 13:35:49 +01:00
if reply_id:
2020-12-01 09:27:36 +01:00
reply_event = await self.room_get_event(
2020-12-24 11:11:35 +01:00
room_id, reply_id
2020-11-30 16:14:17 +01:00
)
2020-12-24 11:11:35 +01:00
reply_event = reply_event.event
2020-11-30 16:14:17 +01:00
2020-11-30 13:35:49 +01:00
content["m.relates_to"] = {
"m.in_reply_to": {"event_id": reply_id},
}
2020-11-30 16:14:17 +01:00
content["format"] = "org.matrix.custom.html"
2020-12-02 09:21:58 +01:00
content["formatted_body"] = f"""<mx-reply><blockquote>
<a href="https://matrix.to/#/{room_id}/{reply_id}">In reply to</a>
2020-12-24 11:11:35 +01:00
<a href="https://matrix.to/#/{reply_event.sender}">{reply_event.sender}</a><br>
{reply_event.body}</blockquote></mx-reply>{message}"""
2020-11-30 16:14:17 +01:00
2020-11-30 13:35:49 +01:00
if edit_id:
content["body"] = f" * {message}"
2020-11-30 13:35:49 +01:00
content["m.new_content"] = {
"body": message,
"msgtype": "m.text"
}
2020-11-30 13:35:49 +01:00
content["m.relates_to"] = {
"event_id": edit_id,
"rel_type": "m.replace",
}
2020-11-12 14:22:48 +01:00
2020-12-01 09:27:36 +01:00
message = await self.room_send(
room_id=room_id,
2020-11-30 13:35:49 +01:00
message_type="m.room.message",
content=content
)
2020-11-12 14:22:48 +01:00
2020-11-30 13:35:49 +01:00
return message.event_id
2020-11-12 14:22:48 +01:00
async def message_redact(self, message, channel_id):
2020-12-01 09:27:36 +01:00
await self.room_redact(
room_id=config["bridge"][str(channel_id)],
2020-11-30 13:35:49 +01:00
event_id=message
)
2020-11-14 16:14:16 +01:00
async def webhook_send(self, author, avatar, message,
event_id, channel_id):
channel = channel_store[channel_id]
2020-12-01 05:58:06 +01:00
# Create webhook if it doesn't exist
hook_name = "matrix_bridge"
hooks = await channel.webhooks()
hook = discord.utils.get(hooks, name=hook_name)
if not hook:
hook = await channel.create_webhook(name=hook_name)
# Username must be between 1 and 80 characters in length
2020-12-01 05:58:06 +01:00
# 'wait=True' allows us to store the sent message
try:
hook = await hook.send(username=author[:80], avatar_url=avatar,
2020-12-01 05:58:06 +01:00
content=message, wait=True)
message_store[event_id] = hook
2020-12-08 11:34:04 +01:00
message_store[hook.id] = event_id
2020-12-01 05:58:06 +01:00
except discord.errors.HTTPException as e:
2020-12-30 15:31:18 +01:00
self.logger.warning(f"Failed to send message {event_id}: {e}")
2020-12-01 09:27:36 +01:00
2020-11-11 16:46:15 +01:00
2020-11-30 13:35:49 +01:00
class DiscordClient(discord.Client):
2020-11-30 16:27:21 +01:00
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
2020-12-01 09:27:36 +01:00
self.matrix_client = MatrixClient(
2020-12-30 15:31:18 +01:00
config["homeserver"], config["username"]
)
2020-12-01 09:27:36 +01:00
2020-12-07 14:58:05 +01:00
self.bg_task = self.loop.create_task(self.matrix_client.create(self))
2020-11-30 16:27:21 +01:00
2020-11-30 13:35:49 +01:00
async def on_ready(self):
print(f"Logged in as {self.user}")
2020-11-11 16:46:15 +01:00
for channel in config["bridge"].keys():
channel_store[channel] = self.get_channel(int(channel))
2020-11-11 16:46:15 +01:00
2020-11-30 13:35:49 +01:00
async def on_message(self, message):
if message.author.bot or str(message.channel.id) not in \
config["bridge"].keys():
2020-11-30 13:35:49 +01:00
return
2020-11-11 16:46:15 +01:00
2020-12-01 09:27:36 +01:00
content = await self.process_message(message)
2020-11-12 15:42:23 +01:00
2020-12-01 09:27:36 +01:00
matrix_message = await self.matrix_client.message_send(
2020-12-13 08:02:20 +01:00
content[0], message.channel.id, reply_id=content[1]
)
2020-11-12 14:22:48 +01:00
2020-11-30 13:35:49 +01:00
message_store[message.id] = matrix_message
2020-11-12 15:42:23 +01:00
2020-11-30 13:35:49 +01:00
async def on_message_edit(self, before, after):
if after.author.bot or str(after.channel.id) not in \
config["bridge"].keys():
2020-11-30 13:35:49 +01:00
return
2020-11-12 15:42:23 +01:00
2020-12-01 09:27:36 +01:00
content = await self.process_message(after)
2020-11-12 14:22:48 +01:00
2020-12-24 11:11:35 +01:00
if before.id in message_store:
await self.matrix_client.message_send(
content[0], after.channel.id, edit_id=message_store[before.id]
)
2020-11-30 13:35:49 +01:00
async def on_message_delete(self, message):
if message.id in message_store:
await self.matrix_client.message_redact(
2020-12-13 08:02:20 +01:00
message_store[message.id], message.channel.id
)
2020-11-14 14:43:25 +01:00
2020-11-30 13:35:49 +01:00
async def on_typing(self, channel, user, when):
if user.bot or str(channel.id) not in \
config["bridge"].keys():
2020-11-30 13:35:49 +01:00
return
2020-11-12 14:22:48 +01:00
2020-11-30 13:35:49 +01:00
# Send typing event
await self.matrix_client.room_typing(
2020-12-13 08:02:20 +01:00
config["bridge"][str(channel.id)], timeout=0
)
2020-12-01 09:27:36 +01:00
async def process_message(self, message):
content = message.clean_content
replied_event = None
if message.reference:
replied_message = await message.channel.fetch_message(
2020-12-13 08:02:20 +01:00
message.reference.message_id
)
2020-12-01 09:27:36 +01:00
try:
replied_event = message_store[replied_message.id]
except KeyError:
pass
# Replace emote IDs with names
content = re.sub(r"<a?(:\w+:)\d*>", r"\g<1>", content)
# Append attachments to message
for attachment in message.attachments:
content += f"\n{attachment.url}"
2020-12-02 09:21:58 +01:00
content = f"[{message.author.name}] {content}"
2020-12-01 09:27:36 +01:00
return content, replied_event
2020-11-11 16:46:15 +01:00
2020-11-30 13:35:49 +01:00
class Callbacks(object):
2020-12-30 15:31:18 +01:00
def __init__(self, matrix_client):
self.matrix_client = matrix_client
2020-12-01 09:27:36 +01:00
def get_channel(self, room):
channel_id = next(
(channel_id for channel_id, room_id in config["bridge"].items()
2020-12-13 08:02:20 +01:00
if room_id == room.room_id), None
)
return channel_id
2020-11-30 13:35:49 +01:00
async def message_callback(self, room, event):
2020-12-01 09:27:36 +01:00
# Ignore messages from ourselves or other rooms
if room.room_id not in config["bridge"].values() or \
2020-12-30 15:31:18 +01:00
event.sender == self.matrix_client.user:
2020-11-30 13:35:49 +01:00
return
2020-11-30 13:35:49 +01:00
message = event.body
if not message:
return
2020-12-07 14:18:45 +01:00
content_dict = event.source.get("content")
channel_id = self.get_channel(room)
author = event.sender.split(":")[0][1:]
2020-12-08 11:42:33 +01:00
avatar = None
homeserver = event.sender.split(":")[-1]
2020-12-08 11:42:33 +01:00
url = "https://matrix.org/_matrix/media/r0/download"
try:
if content_dict["m.relates_to"]["rel_type"] == "m.replace":
edited_event = content_dict["m.relates_to"]["event_id"]
2020-12-10 13:35:16 +01:00
edited_content = await self.process_message(
2020-12-13 08:02:20 +01:00
content_dict["m.new_content"]["body"], channel_id
)
webhook_message = message_store[edited_event]
try:
await webhook_message.edit(content=edited_content)
except discord.errors.NotFound as e:
2020-12-30 15:31:18 +01:00
self.matrix_client.logger.warning(
f"Failed to edit message {edited_event}: {e}"
)
return
except KeyError:
pass
2020-12-07 14:18:45 +01:00
try:
if content_dict["m.relates_to"]["m.in_reply_to"]["event_id"] in \
message_store.values():
message = message.replace(f"<{config['username']}>", "", 1)
except KeyError:
pass
2020-12-08 11:42:33 +01:00
if content_dict["msgtype"] == "m.emote":
message = f"_{author} {message}_"
message = await self.process_message(message, channel_id)
2020-11-30 13:35:49 +01:00
# Get attachments
try:
attachment = event.url.split("/")[-1]
2020-11-30 13:35:49 +01:00
# Highlight attachment name
message = f"`{message}`"
2020-11-12 14:22:48 +01:00
2020-11-30 13:35:49 +01:00
message += f"\n{url}/{homeserver}/{attachment}"
except AttributeError:
pass
2020-11-14 14:43:25 +01:00
2020-11-30 13:35:49 +01:00
# Get avatar
for user in room.users.values():
if user.user_id == event.sender:
if user.avatar_url:
avatar = user.avatar_url.split("/")[-1]
avatar = f"{url}/{homeserver}/{avatar}"
break
2020-12-30 15:31:18 +01:00
await self.matrix_client.webhook_send(
2020-12-13 08:02:20 +01:00
author, avatar, message, event.event_id, channel_id
)
2020-11-30 13:35:49 +01:00
async def redaction_callback(self, room, event):
2020-12-01 09:27:36 +01:00
# Ignore messages from ourselves or other rooms
if room.room_id not in config["bridge"].values() or \
2020-12-30 15:31:18 +01:00
event.sender == self.matrix_client.user:
2020-11-30 13:35:49 +01:00
return
2020-11-11 16:46:15 +01:00
2020-11-30 13:35:49 +01:00
# Redact webhook message
try:
message = message_store[event.redacts]
await message.delete()
except discord.errors.NotFound as e:
2020-12-30 15:31:18 +01:00
self.matrix_client.logger.warning(
2020-12-13 08:02:20 +01:00
f"Failed to delete message {event.event_id}: {e}"
)
2020-11-30 13:35:49 +01:00
except KeyError:
pass
2020-11-13 11:08:28 +01:00
2020-11-30 13:35:49 +01:00
async def typing_callback(self, room, event):
2020-12-01 09:27:36 +01:00
# Ignore events from other rooms
if room.room_id not in config["bridge"].values():
2020-11-30 13:35:49 +01:00
return
2020-11-13 17:31:04 +01:00
2020-11-30 13:35:49 +01:00
if room.typing_users:
2020-12-01 09:27:36 +01:00
# Ignore events from ourselves
2020-11-30 13:35:49 +01:00
if len(room.typing_users) == 1 \
2020-12-30 15:31:18 +01:00
and room.typing_users[0] == self.matrix_client.user:
2020-11-30 13:35:49 +01:00
return
channel_id = self.get_channel(room)
2020-11-30 13:35:49 +01:00
# Send typing event
async with channel_store[channel_id].typing():
2020-11-30 13:35:49 +01:00
pass
2020-12-30 15:31:18 +01:00
async def process_message(self, message, channel_id):
mentions = re.findall(r"(^|\s)(@(\w*))", message)
emotes = re.findall(r":(\w*):", message)
guild = channel_store[channel_id].guild
added_emotes = []
for emote in emotes:
if emote not in added_emotes:
added_emotes.append(emote)
emote_ = discord.utils.get(guild.emojis, name=emote)
if emote_:
message = message.replace(f":{emote}:", str(emote_))
for mention in mentions:
if mention[2] != "":
member = await guild.query_members(query=mention[2])
if member:
message = message.replace(mention[1], member[0].mention)
return message
2020-11-14 16:25:37 +01:00
2020-11-30 16:44:34 +01:00
def main():
2020-12-30 15:31:18 +01:00
logging.basicConfig(level=logging.INFO)
2020-11-30 13:35:49 +01:00
intents = discord.Intents.default()
intents.members = True
2020-11-11 16:46:15 +01:00
2020-12-07 16:56:42 +01:00
allowed_mentions = discord.AllowedMentions(everyone=False, roles=False)
2020-12-30 15:31:18 +01:00
DiscordClient(
intents=intents, allowed_mentions=allowed_mentions
).run(config["token"])
2020-11-30 16:44:34 +01:00
2020-11-11 16:46:15 +01:00
2020-11-12 15:42:23 +01:00
if __name__ == "__main__":
2020-11-30 16:44:34 +01:00
main()