vin
1cc61d85c3
Running async/awaitable functions inside discord.py's event loop was very difficult. Hikari didn't have this problem.
45 lines
1.2 KiB
Python
45 lines
1.2 KiB
Python
import asyncio, json, os
|
|
import hikari
|
|
import mautrix.client
|
|
|
|
dclient = hikari.GatewayBot(
|
|
token=os.getenv("DISCORD_TOKEN"),
|
|
intents=(hikari.Intents.MESSAGE_CONTENT|hikari.Intents.GUILD_MESSAGES)
|
|
)
|
|
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():
|
|
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 = 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()
|