Skip to content

Commit

Permalink
PKlempe#233: implement translation feature
Browse files Browse the repository at this point in the history
  • Loading branch information
zoidberg77 committed Nov 30, 2021
1 parent 5bd5e0d commit ccdf2e7
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .env.example
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 =
Expand Down
1 change: 1 addition & 0 deletions bot/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
26 changes: 26 additions & 0 deletions bot/utility/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,4 @@ websockets==9.1
wrapt==1.12.1
yarl==1.6.3
youtube-dl==2021.5.16
deepl==1.3.1

0 comments on commit ccdf2e7

Please sign in to comment.