Edit discord messages instead of sending new ones

This commit is contained in:
git-bruh 2020-11-30 12:12:05 +05:30
parent debcf5f13d
commit 053cc9d7ee
No known key found for this signature in database
GPG key ID: E1475C50075ADCE6

32
main.py
View file

@ -66,10 +66,7 @@ async def on_message_edit(before, after):
content = await process_discord(after) content = await process_discord(after)
await message_redact(message_cache[before.id], "Message edited") await message_send(content[0], edit_id=message_cache[before.id])
matrix_message = await message_send(f"{content[0]} (edited)")
message_cache[after.id] = matrix_message
@discord_client.event @discord_client.event
@ -191,7 +188,7 @@ async def create_matrix_client():
await matrix_client.close() await matrix_client.close()
async def message_send(message, reply_id=None): async def message_send(message, reply_id=None, edit_id=None):
content = { content = {
"msgtype": "m.text", "msgtype": "m.text",
"body": message, "body": message,
@ -202,6 +199,19 @@ async def message_send(message, reply_id=None):
"m.in_reply_to": {"event_id": reply_id}, "m.in_reply_to": {"event_id": reply_id},
} }
if edit_id:
content["body"] = f" * {message}"
content["m.new_content"] = {
"body": message,
"msgtype": "m.text"
}
content["m.relates_to"] = {
"event_id": edit_id,
"rel_type": "m.replace",
}
message = await matrix_client.room_send( message = await matrix_client.room_send(
room_id=config["room_id"], room_id=config["room_id"],
message_type="m.room.message", message_type="m.room.message",
@ -224,6 +234,18 @@ async def message_callback(room, event):
if room.room_id != config["room_id"]: if room.room_id != config["room_id"]:
return return
# https://github.com/Rapptz/discord.py/issues/6058
# content_dict = event.source.get("content")
# try:
# if content_dict["m.relates_to"]["rel_type"] == "m.replace":
# edited_event = content_dict["m.relates_to"]["event_id"]
# edited_content = content_dict["m.new_content"]["body"]
# webhook_message = message_cache[edited_event]
# await something_to_edit_webhook(webhook_message, edited_content)
# return
# except KeyError:
# pass
message = event.body message = event.body
if not message: if not message: