From ca639d9e522dcabc0537df6c27bf68bd9ac209b9a9914056f2acdad24d1dd50f Mon Sep 17 00:00:00 2001 From: Philipp Date: Sun, 15 Dec 2024 13:59:43 +0100 Subject: [PATCH] Initial commit --- .env_example | 3 +++ .gitignore | 2 ++ main.py | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+) create mode 100644 .env_example create mode 100644 .gitignore create mode 100644 main.py diff --git a/.env_example b/.env_example new file mode 100644 index 0000000..5bcdefa --- /dev/null +++ b/.env_example @@ -0,0 +1,3 @@ +SYNAPSE_TOKEN=abcz +DISCORD_TOKEN=zcba +MATRIX_URL=matrix.domain.example diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cde347e --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.env +venv/* diff --git a/main.py b/main.py new file mode 100644 index 0000000..b2909b9 --- /dev/null +++ b/main.py @@ -0,0 +1,55 @@ +import os +from dotenv import load_dotenv +import sys +import discord +import requests + +load_dotenv() + +synapse_token = os.environ['SYNAPSE_TOKEN'] +discord_token = os.environ['DISCORD_TOKEN'] +matrix_url = os.environ['MATRIX_URL'] +user_id = sys.argv[1] + +class Client(discord.Client): + new_members = [{"id": user_id}] + + synapse_token = synapse_token + + async def on_ready(self): + print(f'Logged in as {self.user}!') + for member in self.new_members: + user = await self.fetch_user(member["id"]) + print(f'Updating: {user.name}: {user.display_avatar.url}') + matrix_user_id = '@user_' + str(user.id) + ':boehm.sh' + mxc_uri = await self.post_matrix_media(matrix_user_id, user.display_avatar) + await self.put_matrix_user(matrix_user_id, mxc_uri) + + async def get_matrix_data(self, user_id): + url = 'https://' + matrix_url + '/_synapse/admin/v2/users/' + user_id + headers = {"Authorization": "Bearer " + self.synapse_token} + + r = requests.get(url, headers=headers) + print(r.json()) + + async def post_matrix_media(self, user_id, image): + url = 'https://' + matrix_url + '/_matrix/media/v3/upload?filename=' + user_id + headers = {"Authorization": "Bearer " + self.synapse_token} + + r = requests.post(url, data=await image.read(), headers=headers) + + return r.json() + + async def put_matrix_user(self, matrix_user, mxc_uri): + url = 'https://' + matrix_url + '/_synapse/admin/v2/users/' + matrix_user + headers = {"Authorization": "Bearer " + self.synapse_token} + data = {"avatar_url": mxc_uri["content_uri"]} + + r = requests.put(url, json=data, headers=headers) + print("Update for user completed...") + + +intents = discord.Intents.default() + +client = Client(intents=intents) +client.run(discord_token)