From 1cc61d85c3d9ef0eb39f4f05913e6bc60e909f62 Mon Sep 17 00:00:00 2001 From: vin Date: Sun, 14 Jan 2024 23:16:42 -0500 Subject: [PATCH] use hikari instead of discord.py Running async/awaitable functions inside discord.py's event loop was very difficult. Hikari didn't have this problem. --- main.py | 37 ++++++++++++++++--------------------- requirements.txt | 2 +- 2 files changed, 17 insertions(+), 22 deletions(-) diff --git a/main.py b/main.py index 5589f25..bb521b1 100644 --- a/main.py +++ b/main.py @@ -1,10 +1,11 @@ -import asyncio, json, os, discord +import asyncio, json, os +import hikari import mautrix.client -intents = discord.Intents.default() -intents.message_content = True - -dclient = discord.Client(intents=intents) +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"), @@ -14,8 +15,6 @@ mclient = mautrix.client.ClientAPI( 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) @@ -25,25 +24,21 @@ async def matrix_init(): 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.listen() +async def on_message(event: hikari.GuildMessageCreateEvent) -> None: + if not event.content: + return -@dclient.event -async def on_message(message): - if message.channel.id in rooms.keys(): - print(f"<{message.author.global_name}> {message.content}") + chanid = event.channel_id + if chanid in rooms.keys(): + print(f"<{event.message.author.global_name}> {event.content}") content = {} - content['body'] = f" {message.content}" + content['body'] = f"<{event.message.author.global_name}> {event.content}" content['msgtype'] = "m.text" + await mclient.send_message(rooms[chanid], json.dumps(content)) - print(json.dumps(content)) - await mclient.send_message(rooms[message.channel.id], json.dumps(content)) - -dclient.run(os.getenv("DISCORD_TOKEN")) +dclient.run() diff --git a/requirements.txt b/requirements.txt index bdb716d..059f925 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,2 @@ -discord.py +hikari mautrix