dmab/main.py

50 lines
1.4 KiB
Python
Raw Normal View History

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"))