diff --git a/cogs/commands.py b/cogs/commands.py index 3fd0e4c..9ae410f 100644 --- a/cogs/commands.py +++ b/cogs/commands.py @@ -526,12 +526,18 @@ class Commands(discord.Cog, name="Commands"): required=True, 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() 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: - await self.translations_cog.translate_command(ctx, text, target_language) + await self.translations_cog.translate_command(ctx, text, target_language, formality) else: await ctx.respond( "Translations are disabled on this server.", ephemeral=True diff --git a/cogs/translation_service_cog.py b/cogs/translation_service_cog.py index fc217a4..05c226b 100644 --- a/cogs/translation_service_cog.py +++ b/cogs/translation_service_cog.py @@ -58,7 +58,7 @@ class TranslationService(discord.Cog, name="TranslationService"): 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""" await ctx.defer() # TODO Add pagination! @@ -74,7 +74,7 @@ class TranslationService(discord.Cog, name="TranslationService"): try: 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: await ctx.respond(f"There was an error with the DeepL API: {e.message}") diff --git a/models/autocomplete_model.py b/models/autocomplete_model.py index 73c1db4..8d99a1f 100644 --- a/models/autocomplete_model.py +++ b/models/autocomplete_model.py @@ -86,6 +86,14 @@ class Translations_autocompleter: 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: """Autocompleter for the opener command""" diff --git a/models/deepl_model.py b/models/deepl_model.py index f851a14..2622a92 100644 --- a/models/deepl_model.py +++ b/models/deepl_model.py @@ -51,7 +51,7 @@ class TranslationModel: max_tries=4, 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 language is: ", translate_language) print("The token is ", self.deepl_token) @@ -59,6 +59,7 @@ class TranslationModel: payload = { "text": text, "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 headers = {