|
19 | 19 |
|
20 | 20 | from bs4 import BeautifulSoup |
21 | 21 | from emoji import get_emoji_regexp |
22 | | -from googletrans import LANGUAGES, Translator |
| 22 | +from googletrans import LANGUAGES, google_translator |
23 | 23 | from gtts import gTTS |
24 | 24 | from gtts.lang import tts_langs |
25 | 25 | from requests import get |
@@ -442,36 +442,38 @@ async def imdb(e): |
442 | 442 | await e.edit("Plox enter **Valid movie name** kthx") |
443 | 443 |
|
444 | 444 |
|
445 | | -@register(outgoing=True, pattern=r"^.trt(?: |$)([\s\S]*)") |
| 445 | +@register(outgoing=True, pattern=r"^\.trt(?: |$)([\s\S]*)") |
446 | 446 | async def translateme(trans): |
447 | 447 | """ For .trt command, translate the given text using Google Translate. """ |
448 | | - translator = Translator() |
449 | | - textx = await trans.get_reply_message() |
450 | | - message = trans.pattern_match.group(1) |
451 | | - if message: |
452 | | - pass |
453 | | - elif textx: |
454 | | - message = textx.text |
| 448 | + |
| 449 | + if trans.is_reply and not trans.pattern_match.group(1): |
| 450 | + message = await trans.get_reply_message() |
| 451 | + message = str(message.message) |
455 | 452 | else: |
456 | | - await trans.edit("`Give a text or reply to a message to translate!`") |
457 | | - return |
| 453 | + message = str(trans.pattern_match.group(1)) |
| 454 | + |
| 455 | + if not message: |
| 456 | + return await trans.edit( |
| 457 | + "**Give some text or reply to a message to translate!**") |
458 | 458 |
|
| 459 | + await trans.edit("**Processing...**") |
| 460 | + translator = google_translator() |
459 | 461 | try: |
460 | | - reply_text = translator.translate(deEmojify(message), dest=TRT_LANG) |
| 462 | + reply_text = translator.translate(deEmojify(message), |
| 463 | + lang_tgt=TRT_LANG) |
461 | 464 | except ValueError: |
462 | | - await trans.edit("Invalid destination language.") |
463 | | - return |
| 465 | + return await trans.edit( |
| 466 | + "**Invalid language selected, use **`.lang tts <language code>`**.**" |
| 467 | + ) |
| 468 | + |
| 469 | + try: |
| 470 | + source_lan = translator.detect(deEmojify(message))[1].title() |
| 471 | + except: |
| 472 | + source_lan = "(Google didn't provide this info)" |
464 | 473 |
|
465 | | - source_lan = LANGUAGES[f"{reply_text.src.lower()}"] |
466 | | - transl_lan = LANGUAGES[f"{reply_text.dest.lower()}"] |
467 | | - reply_text = f"From **{source_lan.title()}**\nTo **{transl_lan.title()}:**\n\n{reply_text.text}" |
| 474 | + reply_text = f"From: **{source_lan}**\nTo: **{LANGUAGES.get(TRT_LANG).title()}**\n\n{reply_text}" |
468 | 475 |
|
469 | 476 | await trans.edit(reply_text) |
470 | | - if BOTLOG: |
471 | | - await trans.client.send_message( |
472 | | - BOTLOG_CHATID, |
473 | | - f"Translated some {source_lan.title()} stuff to {transl_lan.title()} just now.", |
474 | | - ) |
475 | 477 |
|
476 | 478 |
|
477 | 479 | @register(pattern=".lang (trt|tts) (.*)", outgoing=True) |
|
0 commit comments