2020-11-11 16:46:15 +01:00
|
|
|
import logging
|
|
|
|
import json
|
|
|
|
import os
|
2020-11-12 14:22:48 +01:00
|
|
|
import nio
|
2020-11-11 16:46:15 +01:00
|
|
|
import discord
|
|
|
|
|
|
|
|
|
|
|
|
def config_gen(config_file):
|
|
|
|
config_dict = {
|
|
|
|
"homeserver": "https://matrix.org",
|
|
|
|
"room_id": "room:matrix.org",
|
|
|
|
"username": "@name:matrix.org",
|
|
|
|
"password": "my-secret-password",
|
|
|
|
"channel_id": "channel",
|
|
|
|
"token": "my-secret-token"
|
|
|
|
}
|
|
|
|
|
|
|
|
if not os.path.exists(config_file):
|
|
|
|
with open(config_file, "w") as f:
|
|
|
|
json.dump(config_dict, f, indent=4)
|
|
|
|
print(f"Example configuration dumped to {config_file}")
|
|
|
|
exit()
|
|
|
|
|
|
|
|
with open(config_file, "r") as f:
|
|
|
|
config = json.loads(f.read())
|
|
|
|
|
|
|
|
return config
|
|
|
|
|
|
|
|
|
|
|
|
config = config_gen("config.json")
|
|
|
|
|
|
|
|
discord_client = discord.Client()
|
2020-11-12 14:22:48 +01:00
|
|
|
logger = logging.getLogger(__name__)
|
2020-11-11 16:46:15 +01:00
|
|
|
|
|
|
|
|
|
|
|
@discord_client.event
|
|
|
|
async def on_ready():
|
|
|
|
print(f"Logged in as {discord_client.user}")
|
2020-11-12 14:22:48 +01:00
|
|
|
|
|
|
|
# Start Matrix bot
|
|
|
|
await create_matrix_client()
|
2020-11-11 16:46:15 +01:00
|
|
|
|
|
|
|
|
|
|
|
@discord_client.event
|
|
|
|
async def on_message(message):
|
2020-11-12 14:22:48 +01:00
|
|
|
# Don't respond to bots/webhooks
|
2020-11-11 16:46:15 +01:00
|
|
|
if message.author.bot:
|
|
|
|
return
|
|
|
|
|
2020-11-12 14:22:48 +01:00
|
|
|
message_ = f"<{message.author}> {message.content}"
|
2020-11-11 16:46:15 +01:00
|
|
|
|
2020-11-12 14:22:48 +01:00
|
|
|
if str(message.channel.id) == config["channel_id"]:
|
|
|
|
await message_send(message_)
|
|
|
|
|
|
|
|
|
|
|
|
async def emote(message):
|
|
|
|
# Extract emotes from message
|
|
|
|
emote_list = []
|
|
|
|
for item in message.split():
|
|
|
|
if item[0] == item[-1] == ":":
|
|
|
|
emote_list.append(item[1:-1])
|
2020-11-11 16:46:15 +01:00
|
|
|
|
2020-11-12 14:22:48 +01:00
|
|
|
# Replace emotes with IDs
|
|
|
|
for emote in emote_list:
|
|
|
|
emote = discord.utils.get(discord_client.emojis, name=emote)
|
|
|
|
if emote is not None:
|
|
|
|
emote = str(emote)
|
|
|
|
replace_emote = emote.split(":")[1]
|
|
|
|
message = message.replace(f":{replace_emote}:", emote)
|
|
|
|
|
|
|
|
return message
|
|
|
|
|
|
|
|
|
|
|
|
async def webhook_send(author, message):
|
|
|
|
# Get Discord channel from channel ID
|
2020-11-11 16:46:15 +01:00
|
|
|
channel = int(config["channel_id"])
|
|
|
|
channel = discord_client.get_channel(channel)
|
|
|
|
|
2020-11-12 14:22:48 +01:00
|
|
|
# Create webhook if it doesn't exist
|
|
|
|
hook_name = "matrix_bridge"
|
2020-11-11 16:46:15 +01:00
|
|
|
hooks = await channel.webhooks()
|
|
|
|
hook = discord.utils.get(hooks, name=hook_name)
|
|
|
|
if hook is None:
|
|
|
|
hook = await channel.create_webhook(name=hook_name)
|
|
|
|
|
2020-11-12 14:22:48 +01:00
|
|
|
# Replace emote names
|
|
|
|
message = await emote(message)
|
|
|
|
|
2020-11-11 16:46:15 +01:00
|
|
|
await hook.send(content=message, username=author)
|
|
|
|
|
|
|
|
|
|
|
|
async def create_matrix_client():
|
|
|
|
homeserver = config["homeserver"]
|
|
|
|
username = config["username"]
|
|
|
|
password = config["password"]
|
|
|
|
|
2020-11-12 14:22:48 +01:00
|
|
|
global matrix_client
|
|
|
|
|
|
|
|
matrix_client = nio.AsyncClient(homeserver, username)
|
|
|
|
matrix_client.add_event_callback(message_callback, nio.RoomMessageText)
|
|
|
|
|
|
|
|
print(await matrix_client.login(password))
|
|
|
|
|
|
|
|
await matrix_client.sync_forever(timeout=30000)
|
|
|
|
await matrix_client.close()
|
2020-11-11 16:46:15 +01:00
|
|
|
|
|
|
|
|
2020-11-12 14:22:48 +01:00
|
|
|
async def message_send(message):
|
|
|
|
await matrix_client.room_send(
|
|
|
|
room_id=config["room_id"],
|
|
|
|
message_type="m.room.message",
|
|
|
|
content={
|
|
|
|
"msgtype": "m.text",
|
|
|
|
"body": message
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
async def message_callback(room, event):
|
|
|
|
message = event.formatted_body or event.body
|
|
|
|
if not message:
|
|
|
|
return
|
|
|
|
|
|
|
|
print(
|
|
|
|
f"Bot message received for room {room.display_name} | "
|
|
|
|
f"{room.user_name(event.sender)}: {message}"
|
|
|
|
)
|
2020-11-11 16:46:15 +01:00
|
|
|
|
2020-11-12 14:22:48 +01:00
|
|
|
# await webhook_send(event.sender, message)
|
2020-11-11 16:46:15 +01:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2020-11-12 14:22:48 +01:00
|
|
|
# Start Discord bot
|
2020-11-11 16:46:15 +01:00
|
|
|
discord_client.run(config["token"])
|
|
|
|
|
|
|
|
|
2020-11-12 14:22:48 +01:00
|
|
|
main()
|