blob: 5589f25f27b94d800c5dae9cac38a01836282ba2 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
import asyncio, json, os, discord
import mautrix.client
intents = discord.Intents.default()
intents.message_content = True
dclient = discord.Client(intents=intents)
mclient = mautrix.client.ClientAPI(
os.getenv("MATRIX_USERID"),
base_url=os.getenv("MATRIX_HOMESERVER"),
token=os.getenv("MATRIX_TOKEN")
)
rooms = {1196201613132574733: "!eChKdzVweNTyHctaKQ:wired.rehab"}
async def matrix_init():
joined = await mclient.get_joined_rooms()
print(joined)
for room in rooms.values():
try:
await mclient.join_room_by_id(room)
except Exception as e:
print("Failed to join room", room)
print(type(e).__name__, e)
exit(1)
joined = await mclient.get_joined_rooms()
print(joined)
for room in joined:
await mclient.send_message(room, '{"body":"joined","msgtype":"m.text"}')
task = asyncio.get_event_loop().create_task(matrix_init())
asyncio.get_event_loop().run_until_complete(task)
@dclient.event
async def on_ready():
print(f"Logged in as {dclient.user}")
@dclient.event
async def on_message(message):
if message.channel.id in rooms.keys():
print(f"<{message.author.global_name}> {message.content}")
content = {}
content['body'] = f"<message.author.global_name> {message.content}"
content['msgtype'] = "m.text"
print(json.dumps(content))
await mclient.send_message(rooms[message.channel.id], json.dumps(content))
dclient.run(os.getenv("DISCORD_TOKEN"))
|