-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranslate.py
50 lines (44 loc) · 2.12 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
50
import requests
import discord
import json
import codecs
from bs4 import BeautifulSoup
from discord.ext import commands
class Translate:
def __init__(self, bot):
self.bot = bot
@commands.command()
async def translate(self, ctx, lang_to_trans, *, msg):
"""Translates words from one language to another. Do `tux help`translate for more information.
Usage:
`tux translate <language you wanna translate>` <words>` - Translate words from one language to another. Full language names must be used.
The original language will be assumed automatically.
"""
some_var = requests.get(
"https://cdn.discordapp.com/attachments/407783970558836740/410076802762014720/langs.json", verify=False).text
language_codes = json.loads(some_var)
real_language = False
lang_to_trans = lang_to_trans.lower()
for entry in language_codes:
if lang_to_trans in language_codes[entry]["name"].replace(";", "").replace(",", "").lower().split():
language = language_codes[entry]["name"].replace(
";", "").replace(",", "").split()[0]
lang_to_trans = entry
real_language = True
if real_language:
translate = requests.get(
"https://translate.google.com/m?hl={}&sl=auto&q={}".format(lang_to_trans, msg), verify=False).text
result = str(translate).split('class="t0">')[1].split("</div>")[0]
result = BeautifulSoup(result, "lxml").text
embed = discord.Embed(color=discord.Color.blue())
embed.add_field(name="Original", value=msg, inline=False)
embed.add_field(name=language, value=result.replace(
"&", "&"), inline=False)
if result == msg:
embed.add_field(
name="**Warning**", value="Google Translate doesn't support this language.")
await ctx.send("", embed=embed)
else:
await ctx.send("Enter a real language please.")
def setup(bot):
bot.add_cog(Translate(bot))