-
-
Notifications
You must be signed in to change notification settings - Fork 5
#233: implement translation feature #234
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
ccdf2e7
4e7e81a
dc5e099
629241d
9ebf1e1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| # API Keys | ||
| DISCORD_BOT_TOKEN = | ||
|
|
||
| DEEPL_API_KEY = | ||
|
|
||
| # File Paths | ||
| LOG_FILE_PATH = | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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,30 @@ 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_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 | ||
|
|
||
| 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("**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 | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This task will probably take some time. Wrap it in a And what about embedded messsages? Most of the info messages on the server are so called Embeds and a lot of times |
||
| except DeepLException: | ||
| 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)) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Discord messages have a char limit of 2000 for non-Nitro and 4000 for Nitro users. By showing both the original and the translated version, this will often exceed these limits. A better way to do this would be to create an Embed instead of a normal message. In an embed you can use markdown to create hyperlinks. This way you could simply link the message url from the original and paste the translated version into a field from the embed. A good tool for designing embeds is this for example: https://leovoel.github.io/embed-visualizer/ |
||
|
|
||
|
|
||
| def build_serverinfo_strings(guild: discord.Guild) -> List[str]: | ||
| """Function for building the strings needed for the serverinfo Embed. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -43,3 +43,4 @@ websockets==9.1 | |
| wrapt==1.12.1 | ||
| yarl==1.6.3 | ||
| youtube-dl==2021.5.16 | ||
| deepl==1.3.1 | ||
Uh oh!
There was an error while loading. Please reload this page.