-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtranslator_cog.py
89 lines (71 loc) · 2.93 KB
/
translator_cog.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
from discord.ext.commands import Cog, hybrid_command, Context, CommandError
from googletrans import Translator, LANGUAGES
from discord import Embed, Interaction
from discord.app_commands import Choice, autocomplete
from bot.helpers.error_helper import get_original_exception
async def language_autocomplete(_: Interaction, current: str) -> list[Choice[str]]:
"""Provide autocomplete suggestions for language names.
:param _: The interaction object.
:type _: Interaction
:param current: The current value of the input field.
:type current: str
:return: A list of `Choice` objects containing language names.
:rtype: list[Choice[str]]
"""
languages = list(LANGUAGES.values())
return [
Choice(name=language.capitalize(), value=language)
for language in languages[:25] if current.lower() in language.lower()
]
class TranslatorCog(
Cog,
name="Translator",
description="Translate a sentence/word from any languages into any languages."
):
@hybrid_command(
name='translator',
help='Translate a sentence/word from any languages into any languages',
usage="sentence={sentence}"
)
@autocomplete(translate_into=language_autocomplete)
async def translator(self, ctx: Context, *, sentence: str, translate_into: str):
"""Translate a sentence or word from any language into any languages.
:param ctx: The context object.
:type ctx: Context
:param sentence: The sentence or word to be translated.
:type sentence: str
:param translate_into: The language code for the target language.
:type translate_into: str
:return: Embed with original input and its translation
"""
await ctx.defer()
text_translator = Translator()
translated_text = text_translator.translate(sentence, dest=translate_into)
embed = Embed(color=self.bot.default_color)
embed.add_field(
name=f"{LANGUAGES[translated_text.src].capitalize()} Original",
value=sentence.capitalize(),
inline=False
)
embed.add_field(
name=f"{translate_into} Translation",
value=translated_text.text,
inline=False
)
await ctx.send(embed=embed)
def __init__(self, bot):
self.bot = bot
@translator.error
async def translator_error(self, ctx: Context, error: CommandError):
"""Error handler for the `translator` command.
:param ctx: The context object.
:type ctx: Context
:param error: The error object.
:type error: Exception
:return: This function sends an embed message to the Discord channel
"""
original_error = get_original_exception(error)
if isinstance(original_error, ValueError):
await ctx.send("Please enter a valid language code.", ephemeral=True)
async def setup(bot):
await bot.add_cog(TranslatorCog(bot))