2020-01-08 17:09:10 +01:00
|
|
|
import discord
|
|
|
|
from discord.ext import commands
|
2020-01-08 17:44:19 +01:00
|
|
|
from util import checking, embed
|
2020-01-08 17:09:10 +01:00
|
|
|
from api import translation
|
|
|
|
|
|
|
|
class Translation(commands.Cog):
|
|
|
|
|
|
|
|
def __init__(self, client: discord.Client):
|
|
|
|
self.client = client
|
|
|
|
|
2020-01-08 17:44:19 +01:00
|
|
|
@commands.command(name="translate", description="Translates the given text", usage="translate <Language> <Text>", aliases=["tl"])
|
2020-01-08 17:09:10 +01:00
|
|
|
@commands.check(checking.is_author_bot)
|
|
|
|
async def translate(self, ctx: commands.Context, language: str, *message: str):
|
|
|
|
# Get language code
|
|
|
|
code = translation.name_to_ISO(language)
|
|
|
|
text = ' '.join(message)
|
|
|
|
if code == "":
|
2020-01-08 17:44:19 +01:00
|
|
|
await ctx.send(embed=embed.make_error_embed(f"There is no language named **{language}**."))
|
2020-01-08 17:09:10 +01:00
|
|
|
return
|
|
|
|
|
2020-01-08 17:44:19 +01:00
|
|
|
response = translation.translate(text, code)
|
2020-01-08 21:18:17 +01:00
|
|
|
if len(response) == 0:
|
|
|
|
await ctx.send(embed=embed.make_error_embed(f"The translation API doesn't support **{language}**."))
|
|
|
|
return
|
2020-01-08 17:44:19 +01:00
|
|
|
translated = response[0]
|
|
|
|
direction = response[1].split("-")
|
|
|
|
_from = translation.ISO_to_name(direction[0])
|
|
|
|
_to = translation.ISO_to_name(direction[1])
|
|
|
|
await ctx.send(embed=embed.make_embed_field(f"{_from} -> {_to}", None, text, translated))
|
2020-01-08 17:09:10 +01:00
|
|
|
|
|
|
|
def setup(client: discord.Client):
|
|
|
|
client.add_cog(Translation(client))
|