dmab/main.py

47 lines
1.2 KiB
Python
Raw Normal View History

import asyncio, json, os
import hikari
import mautrix.client
config = json.load(open("config.json", "r"))
dclient = hikari.GatewayBot(
token=config["discord_token"],
intents=(hikari.Intents.MESSAGE_CONTENT|hikari.Intents.GUILD_MESSAGES)
)
mclient = mautrix.client.ClientAPI(
config["matrix_userid"],
base_url=config["matrix_homeserver"],
token=config["matrix_token"]
)
rooms = config["rooms"]
async def matrix_init():
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)
task = asyncio.get_event_loop().create_task(matrix_init())
asyncio.get_event_loop().run_until_complete(task)
@dclient.listen()
async def on_message(event: hikari.GuildMessageCreateEvent) -> None:
if not event.content:
return
chanid = str(event.channel_id)
if chanid in rooms.keys():
print(f"<{event.message.author.global_name}> {event.content}")
content = {}
content['body'] = f"<{event.message.author.global_name}> {event.content}"
content['msgtype'] = "m.text"
await mclient.send_message(rooms[chanid], json.dumps(content))
dclient.run()