Replies in translation

This commit is contained in:
deadcade 2022-08-27 14:42:02 +02:00
parent 6086400eaa
commit 63c11f24cc

15
main.py
View file

@ -184,6 +184,15 @@ async def send_text(room_id, text):
formatted = formatted.replace("\n", "<br>") formatted = formatted.replace("\n", "<br>")
return await client.room_send(room_id=room_id, message_type="m.room.message", content={"msgtype": "m.text", "body": unformatted + " (SelfBot)", "format": "org.matrix.custom.html", "formatted_body": formatted + (f" (<a href=\"{settings['source_url']}\">SelfBot</a>)" if settings["source_url"] else " (SelfBot)")}, ignore_unverified_devices=True) return await client.room_send(room_id=room_id, message_type="m.room.message", content={"msgtype": "m.text", "body": unformatted + " (SelfBot)", "format": "org.matrix.custom.html", "formatted_body": formatted + (f" (<a href=\"{settings['source_url']}\">SelfBot</a>)" if settings["source_url"] else " (SelfBot)")}, ignore_unverified_devices=True)
# Send a reply (with HTML formatting)
async def send_reply(room_id, original_event, text):
unformatted, formatted = text, text
unformatted = "".join([part.split(">")[-1] for part in unformatted.split("<")])
unformatted = html.unescape(unformatted)
# \n doesn't work in HTML, replace it with <br>
formatted = formatted.replace("\n", "<br>")
return await client.room_send(room_id=room_id, message_type="m.room.message", content={"msgtype": "m.text", "body": unformatted + " (SelfBot)", "format": "org.matrix.custom.html", "formatted_body": formatted + (f" (<a href=\"{settings['source_url']}\">SelfBot</a>)" if settings["source_url"] else " (SelfBot)"), "m.relates_to": {"m.in_reply_to": {"event_id": original_event.event_id}}}, ignore_unverified_devices=True)
# Commands definition # Commands definition
# Appends shrug to the end of the message # Appends shrug to the end of the message
async def shrug(args, room, event): async def shrug(args, room, event):
@ -326,6 +335,10 @@ async def reaction_callback(room: nio.MatrixRoom, event: nio.UnknownEvent, react
if settings["debug"]: if settings["debug"]:
print(f"Got reaction in {room.room_id} from {event.sender}.") print(f"Got reaction in {room.room_id} from {event.sender}.")
# No selfbot in mautrix-discord rooms.
if " (Discord)" in room.display_name:
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)
if isinstance(event_response, nio.RoomGetEventError): if isinstance(event_response, nio.RoomGetEventError):
@ -353,7 +366,7 @@ async def reaction_callback(room: nio.MatrixRoom, event: nio.UnknownEvent, react
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") translated = lt.translate(reacted_to_event.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_text(room.room_id, "Translated from " + lang + " (" + flag_emoji("US" if lang == "EN" else lang) + "):\n" + translated) return await send_reply(room.room_id, reacted_to_event, "Translated from " + lang + " (" + flag_emoji("US" if lang == "EN" else lang) + "):\n" + translated + "\n")
async def message_callback(room: nio.MatrixRoom, event: nio.RoomMessageText) -> None: async def message_callback(room: nio.MatrixRoom, event: nio.RoomMessageText) -> None:
global client, settings, emojis global client, settings, emojis