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.
This commit is contained in:
Vineet K 2024-01-14 23:16:42 -05:00
parent c2d1d70cd1
commit 1cc61d85c3
2 changed files with 17 additions and 22 deletions

37
main.py
View File

@ -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.author.global_name> {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()

View File

@ -1,2 +1,2 @@
discord.py
hikari
mautrix