From ccdf2e7831d3dd967973acec80425778541b37f1 Mon Sep 17 00:00:00 2001 From: sidakk95 Date: Tue, 30 Nov 2021 14:16:38 -0500 Subject: [PATCH 1/5] #233: implement translation feature --- .env.example | 2 +- bot/constants.py | 1 + bot/utility/utility.py | 26 ++++++++++++++++++++++++++ requirements.txt | 1 + 4 files changed, 29 insertions(+), 1 deletion(-) diff --git a/.env.example b/.env.example index b9e422c..7223406 100644 --- a/.env.example +++ b/.env.example @@ -1,6 +1,6 @@ # API Keys DISCORD_BOT_TOKEN = - +DEEPL_API_KEY = # File Paths LOG_FILE_PATH = diff --git a/bot/constants.py b/bot/constants.py index 18f2dda..99d4f86 100644 --- a/bot/constants.py +++ b/bot/constants.py @@ -117,6 +117,7 @@ # API Keys DISCORD_BOT_TOKEN = os.getenv("DISCORD_BOT_TOKEN") or "Undefined" +DEEPL_API_KEY = os.getenv("DEEPL_API_KEY") or "Undefined" # File Paths LOG_FILE_PATH = os.getenv("LOG_FILE_PATH") or '/logfile.log' diff --git a/bot/utility/utility.py b/bot/utility/utility.py index 97fd241..386a675 100644 --- a/bot/utility/utility.py +++ b/bot/utility/utility.py @@ -4,10 +4,13 @@ from typing import List, Optional import discord +from deepl import DeepLException from discord.ext import commands from bot import constants +from bot.__main__ import intents from bot.logger import command_log, log +import deepl class UtilityCog(commands.Cog): @@ -212,6 +215,29 @@ async def welcome_message(self, user: discord.Member): "__gemeinsam__ schaffen wir das!** :muscle:") await user.send(content=content, embed=embed) + @commands.command(name='translate') + @command_log + async def translate_msg(self, ctx: commands.Context, *, msg_id: int): + """Command Handler for the `translate` command. + + Translates the message with the provided id to english and sends the translation as a dm to the user + + Args: + ctx (discord.ext.commands.Context): The context in which the command was called. + msg_id (int): The id of the message that should be translated + + """ + message = await ctx.fetch_message(msg_id) + if constants.DEEPL_API_KEY == "undefined": + await message.author.send("Translation is currently unavailable(no api key)") + translator = deepl.Translator(constants.DEEPL_API_KEY) + try: + translated_text = translator.translate_text(message.content, target_lang="EN-US").text + except DeepLException: + await message.author.send("Translation is currently unavailable") + return + await message.author.send("Original:\n{}\n\nTranslated:\n{}".format(message.content, translated_text)) + def build_serverinfo_strings(guild: discord.Guild) -> List[str]: """Function for building the strings needed for the serverinfo Embed. diff --git a/requirements.txt b/requirements.txt index d8ad24f..d01255e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -43,3 +43,4 @@ websockets==9.1 wrapt==1.12.1 yarl==1.6.3 youtube-dl==2021.5.16 +deepl==1.3.1 \ No newline at end of file From 4e7e81a5d0ce6c582b15ae1420d6b0232f97165d Mon Sep 17 00:00:00 2001 From: sidakk95 Date: Tue, 30 Nov 2021 16:03:08 -0500 Subject: [PATCH 2/5] fixed undefined comparison --- bot/utility/utility.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/utility/utility.py b/bot/utility/utility.py index 386a675..b9d310f 100644 --- a/bot/utility/utility.py +++ b/bot/utility/utility.py @@ -228,7 +228,7 @@ async def translate_msg(self, ctx: commands.Context, *, msg_id: int): """ message = await ctx.fetch_message(msg_id) - if constants.DEEPL_API_KEY == "undefined": + if constants.DEEPL_API_KEY == "Undefined": await message.author.send("Translation is currently unavailable(no api key)") translator = deepl.Translator(constants.DEEPL_API_KEY) try: From dc5e09974cbca8152b79c6bf9b15233902420dd2 Mon Sep 17 00:00:00 2001 From: Kevin Sidak <17435476+zoidberg77@users.noreply.github.com> Date: Wed, 1 Dec 2021 09:31:45 -0500 Subject: [PATCH 3/5] Update bot/utility/utility.py Co-authored-by: PKlempe <49726903+PKlempe@users.noreply.github.com> --- bot/utility/utility.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/utility/utility.py b/bot/utility/utility.py index b9d310f..7b55094 100644 --- a/bot/utility/utility.py +++ b/bot/utility/utility.py @@ -217,7 +217,7 @@ async def welcome_message(self, user: discord.Member): @commands.command(name='translate') @command_log - async def translate_msg(self, ctx: commands.Context, *, msg_id: int): + async def translate_message(self, ctx: commands.Context, message: discord.Message): """Command Handler for the `translate` command. Translates the message with the provided id to english and sends the translation as a dm to the user From 629241dbaea665a2fb9e901605cb60fad48430c1 Mon Sep 17 00:00:00 2001 From: Kevin Sidak <17435476+zoidberg77@users.noreply.github.com> Date: Wed, 1 Dec 2021 09:32:02 -0500 Subject: [PATCH 4/5] Update bot/utility/utility.py Co-authored-by: PKlempe <49726903+PKlempe@users.noreply.github.com> --- bot/utility/utility.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bot/utility/utility.py b/bot/utility/utility.py index 7b55094..bc809fe 100644 --- a/bot/utility/utility.py +++ b/bot/utility/utility.py @@ -229,7 +229,8 @@ async def translate_message(self, ctx: commands.Context, message: discord.Messag """ message = await ctx.fetch_message(msg_id) if constants.DEEPL_API_KEY == "Undefined": - await message.author.send("Translation is currently unavailable(no api key)") + await message.author.send("**Error:** No API Key has been specified for using the translation service. Please contact the moderators of the server.") + return translator = deepl.Translator(constants.DEEPL_API_KEY) try: translated_text = translator.translate_text(message.content, target_lang="EN-US").text From 9ebf1e194075e49b95d9ac6a05d9abc14a971c51 Mon Sep 17 00:00:00 2001 From: Kevin Sidak <17435476+zoidberg77@users.noreply.github.com> Date: Wed, 1 Dec 2021 09:32:35 -0500 Subject: [PATCH 5/5] Update bot/utility/utility.py Co-authored-by: PKlempe <49726903+PKlempe@users.noreply.github.com> --- bot/utility/utility.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/utility/utility.py b/bot/utility/utility.py index bc809fe..50ee605 100644 --- a/bot/utility/utility.py +++ b/bot/utility/utility.py @@ -235,7 +235,7 @@ async def translate_message(self, ctx: commands.Context, message: discord.Messag try: translated_text = translator.translate_text(message.content, target_lang="EN-US").text except DeepLException: - await message.author.send("Translation is currently unavailable") + await message.author.send("**Error:** The translation service is currently unavailable. If this problem persists, please contact the moderators of the server.") return await message.author.send("Original:\n{}\n\nTranslated:\n{}".format(message.content, translated_text))