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:43 -05:00
|
|
|
config = json.load(open("config.json", "r"))
|
|
|
|
|
2024-01-14 23:16:42 -05:00
|
|
|
dclient = hikari.GatewayBot(
|
2024-01-14 23:16:43 -05:00
|
|
|
token=config["discord_token"],
|
2024-01-14 23:16:42 -05:00
|
|
|
intents=(hikari.Intents.MESSAGE_CONTENT|hikari.Intents.GUILD_MESSAGES)
|
|
|
|
)
|
2024-01-14 23:16:42 -05:00
|
|
|
mclient = mautrix.client.ClientAPI(
|
2024-01-14 23:16:43 -05:00
|
|
|
config["matrix_userid"],
|
|
|
|
base_url=config["matrix_homeserver"],
|
|
|
|
token=config["matrix_token"]
|
2024-01-14 23:16:42 -05:00
|
|
|
)
|
2024-01-14 23:16:41 -05:00
|
|
|
|
2024-01-14 23:16:43 -05:00
|
|
|
rooms = config["rooms"]
|
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:43 -05:00
|
|
|
chanid = str(event.channel_id)
|
2024-01-14 23:16:42 -05:00
|
|
|
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()
|