Better reply handling

This commit is contained in:
deadcade 2022-08-27 14:58:10 +02:00
parent 63c11f24cc
commit 8136a433f3

16
main.py
View file

@ -338,6 +338,10 @@ async def reaction_callback(room: nio.MatrixRoom, event: nio.UnknownEvent, react
# No selfbot in mautrix-discord rooms. # No selfbot in mautrix-discord rooms.
if " (Discord)" in room.display_name: if " (Discord)" in room.display_name:
return return
# Only acknowledge reactions that we sent
if event.sender != client.user_id:
return
# Get the original event that was reacted to # Get the original event that was reacted to
event_response = await client.room_get_event(room.room_id, reacted_to_id) event_response = await client.room_get_event(room.room_id, reacted_to_id)
@ -345,10 +349,6 @@ async def reaction_callback(room: nio.MatrixRoom, event: nio.UnknownEvent, react
return return
reacted_to_event = event_response.event reacted_to_event = event_response.event
# Only acknowledge reactions that we sent
if event.sender != client.user_id:
return
# Get the reaction # Get the reaction
reaction_content = (event.source.get("content", {}).get("m.relates_to", {}).get("key")) reaction_content = (event.source.get("content", {}).get("m.relates_to", {}).get("key"))
@ -361,10 +361,16 @@ async def reaction_callback(room: nio.MatrixRoom, event: nio.UnknownEvent, react
else: else:
# Translate from lang to English # Translate from lang to English
from_lang = country from_lang = country
lt = LibreTranslateAPI(settings["libretranslate_api"], settings["libretranslate_api_key"] if "libretranslate_api_key" in settings.keys() and settings["libretranslate_api_key"] else None) lt = LibreTranslateAPI(settings["libretranslate_api"], settings["libretranslate_api_key"] if "libretranslate_api_key" in settings.keys() and settings["libretranslate_api_key"] else None)
# If the language is not supported
if from_lang not in [x["code"] for x in lt.languages()] and from_lang != "auto": if from_lang not in [x["code"] for x in lt.languages()] and from_lang != "auto":
return await send_text(room.room_id, "Language " + from_lang + " is not supported!") return await send_text(room.room_id, "Language " + from_lang + " is not supported!")
translated = lt.translate(reacted_to_event.body, from_lang, "en") original_body = reacted_to_event.body
# If the message was a reply
if reacted_to_event.source.get("content", {}).get("m.relates_to", {}).get("m.in_reply_to", {}).get("event_id"):
original_body = "\n\n".join(reacted_to_event.split("\n\n")[1:]) # Remove the in reply to part
translated = lt.translate(original_body, from_lang, "en")
lang = lt.detect(reacted_to_event.body)[0]["language"].upper() if from_lang == "auto" else from_lang.upper() lang = lt.detect(reacted_to_event.body)[0]["language"].upper() if from_lang == "auto" else from_lang.upper()
return await send_reply(room.room_id, reacted_to_event, "Translated from " + lang + " (" + flag_emoji("US" if lang == "EN" else lang) + "):\n" + translated + "\n") return await send_reply(room.room_id, reacted_to_event, "Translated from " + lang + " (" + flag_emoji("US" if lang == "EN" else lang) + "):\n" + translated + "\n")