Initial commit
This commit is contained in:
commit
ca639d9e52
3 changed files with 60 additions and 0 deletions
3
.env_example
Normal file
3
.env_example
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
SYNAPSE_TOKEN=abcz
|
||||||
|
DISCORD_TOKEN=zcba
|
||||||
|
MATRIX_URL=matrix.domain.example
|
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
.env
|
||||||
|
venv/*
|
55
main.py
Normal file
55
main.py
Normal file
|
@ -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)
|
Loading…
Reference in a new issue