forked from ULTRA-OP/ULTRA-X
-
Notifications
You must be signed in to change notification settings - Fork 0
/
translate.py
49 lines (45 loc) · 1.41 KB
/
translate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
""" Google Translate
Available Commands:
.tr LanguageCode as reply to a message
.tr LangaugeCode | text to translate"""
import emoji
from googletrans import Translator
from ULTRA.utils import admin_cmd
from telethon import events
@borg.on(admin_cmd(pattern="tr ?(.*)"))
@borg.on(events.NewMessage(pattern=r"\.tr ?(.*)",incoming=True))
async def _(event):
if event.fwd_from:
return
if "trim" in event.raw_text:
# https://t.me/c/1220993104/192075
return
input_str = event.pattern_match.group(1)
if event.reply_to_msg_id:
previous_message = await event.get_reply_message()
text = previous_message.message
lan = input_str or "gu"
elif "|" in input_str:
lan, text = input_str.split("|")
else:
await event.edit("`.tr LanguageCode` as reply to a message")
return
text = emoji.demojize(text.strip())
lan = lan.strip()
translator = Translator()
try:
translated = translator.translate(text, dest=lan)
after_tr_text = translated.text
# TODO: emojify the :
# either here, or before translation
output_str = """**Translated By ULTRA X**
Source **( {} )**
Translation **( {} )**
{}""".format(
translated.src,
lan,
after_tr_text
)
await event.edit(output_str)
except Exception as exc:
await event.edit(str(exc))