2024-01-14 23:16:42 -05:00
|
|
|
import asyncio, json, os
|
|
|
|
import hikari
|
2024-01-14 23:16:42 -05:00
|
|
|
import mautrix.client
|
2024-01-14 23:16:41 -05:00
|
|
|
|
2024-01-14 23:16:42 -05:00
|
|
|
dclient = hikari.GatewayBot(
|
|
|
|
token=os.getenv("DISCORD_TOKEN"),
|
|
|
|
intents=(hikari.Intents.MESSAGE_CONTENT|hikari.Intents.GUILD_MESSAGES)
|
|
|
|
)
|
2024-01-14 23:16:42 -05:00
|
|
|
mclient = mautrix.client.ClientAPI(
|
|
|
|
os.getenv("MATRIX_USERID"),
|
|
|
|
base_url=os.getenv("MATRIX_HOMESERVER"),
|
|
|
|
token=os.getenv("MATRIX_TOKEN")
|
|
|
|
)
|
2024-01-14 23:16:41 -05:00
|
|
|
|
2024-01-14 23:16:42 -05:00
|
|
|
rooms = {1196201613132574733: "!eChKdzVweNTyHctaKQ:wired.rehab"}
|
2024-01-14 23:16:41 -05:00
|
|
|
|
2024-01-14 23:16:42 -05:00
|
|
|
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)
|
|
|
|
|
2024-01-14 23:16:42 -05:00
|
|
|
@dclient.listen()
|
|
|
|
async def on_message(event: hikari.GuildMessageCreateEvent) -> None:
|
|
|
|
if not event.content:
|
|
|
|
return
|
2024-01-14 23:16:41 -05:00
|
|
|
|
2024-01-14 23:16:42 -05:00
|
|
|
chanid = event.channel_id
|
|
|
|
if chanid in rooms.keys():
|
|
|
|
print(f"<{event.message.author.global_name}> {event.content}")
|
2024-01-14 23:16:41 -05:00
|
|
|
|
2024-01-14 23:16:42 -05:00
|
|
|
content = {}
|
2024-01-14 23:16:42 -05:00
|
|
|
content['body'] = f"<{event.message.author.global_name}> {event.content}"
|
2024-01-14 23:16:42 -05:00
|
|
|
content['msgtype'] = "m.text"
|
2024-01-14 23:16:42 -05:00
|
|
|
await mclient.send_message(rooms[chanid], json.dumps(content))
|
2024-01-14 23:16:42 -05:00
|
|
|
|
2024-01-14 23:16:42 -05:00
|
|
|
dclient.run()
|