55 lines
1.8 KiB
Python
55 lines
1.8 KiB
Python
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)
|