Add formality param to translations

Kaveen Kumarasinghe 2 years ago
parent c1526b40fc
commit 02762fd61c

@ -526,12 +526,18 @@ class Commands(discord.Cog, name="Commands"):
required=True, required=True,
autocomplete=Translations_autocompleter.get_languages, autocomplete=Translations_autocompleter.get_languages,
) )
@discord.option(
name="formality",
description="Formal/Informal tone of translation",
required=False,
autocomplete=Translations_autocompleter.get_formality_values,
)
@discord.guild_only() @discord.guild_only()
async def translate( async def translate(
self, ctx: discord.ApplicationContext, text: str, target_language: str self, ctx: discord.ApplicationContext, text: str, target_language: str, formality: str,
): ):
if self.translations_cog: if self.translations_cog:
await self.translations_cog.translate_command(ctx, text, target_language) await self.translations_cog.translate_command(ctx, text, target_language, formality)
else: else:
await ctx.respond( await ctx.respond(
"Translations are disabled on this server.", ephemeral=True "Translations are disabled on this server.", ephemeral=True

@ -58,7 +58,7 @@ class TranslationService(discord.Cog, name="TranslationService"):
return embed return embed
async def translate_command(self, ctx, text, target_language): async def translate_command(self, ctx, text, target_language, formality):
"""Delete all local images""" """Delete all local images"""
await ctx.defer() await ctx.defer()
# TODO Add pagination! # TODO Add pagination!
@ -74,7 +74,7 @@ class TranslationService(discord.Cog, name="TranslationService"):
try: try:
response = await self.translation_model.send_translate_request( response = await self.translation_model.send_translate_request(
text, TranslationModel.get_country_code_from_name(target_language) text, TranslationModel.get_country_code_from_name(target_language), formality
) )
except aiohttp.ClientResponseError as e: except aiohttp.ClientResponseError as e:
await ctx.respond(f"There was an error with the DeepL API: {e.message}") await ctx.respond(f"There was an error with the DeepL API: {e.message}")

@ -86,6 +86,14 @@ class Translations_autocompleter:
if language.lower().startswith(ctx.value.lower()) if language.lower().startswith(ctx.value.lower())
] ]
async def get_formality_values(self, ctx: discord.AutocompleteContext):
"""gets valid values for the formality option"""
return [
value
for value in ["prefer_more", "prefer_less"]
if value.lower().startswith(ctx.value.lower())
]
class File_autocompleter: class File_autocompleter:
"""Autocompleter for the opener command""" """Autocompleter for the opener command"""

@ -51,7 +51,7 @@ class TranslationModel:
max_tries=4, max_tries=4,
on_backoff=backoff_handler, on_backoff=backoff_handler,
) )
async def send_translate_request(self, text, translate_language): async def send_translate_request(self, text, translate_language, formality):
print("The text is: ", text) print("The text is: ", text)
print("The language is: ", translate_language) print("The language is: ", translate_language)
print("The token is ", self.deepl_token) print("The token is ", self.deepl_token)
@ -59,6 +59,7 @@ class TranslationModel:
payload = { payload = {
"text": text, "text": text,
"target_lang": translate_language, "target_lang": translate_language,
"formality": "default" if formality is None else formality,
} }
# Instead of sending as json, we want to send as regular post params # Instead of sending as json, we want to send as regular post params
headers = { headers = {

Loading…
Cancel
Save