mirror of https://github.com/stijndcl/didier
Translate slash command
parent
358f8693dd
commit
ef547a7090
|
@ -163,8 +163,8 @@ class Birthdays(commands.Cog):
|
|||
|
||||
# Create a datetime object for this birthday
|
||||
timeString = "{}/{}/{}".format(
|
||||
stringFormatters.leadingZero(str(day)),
|
||||
stringFormatters.leadingZero(str(month)),
|
||||
stringFormatters.leading_zero(str(day)),
|
||||
stringFormatters.leading_zero(str(month)),
|
||||
year
|
||||
)
|
||||
|
||||
|
|
|
@ -37,7 +37,7 @@ class Faq(commands.Cog):
|
|||
return await self.faqCategory(ctx, (constants.faq_channels[ctx.channel.id],))
|
||||
|
||||
# List of all categories with the first letter capitalized
|
||||
resp = [stringFormatters.titleCase(cat[0]) for cat in faq.getCategories()]
|
||||
resp = [stringFormatters.title_case(cat[0]) for cat in faq.getCategories()]
|
||||
|
||||
# Sort alphabetically
|
||||
resp.sort()
|
||||
|
@ -146,7 +146,7 @@ class Faq(commands.Cog):
|
|||
resp.sort(key=lambda x: int(x[0]))
|
||||
|
||||
embed = discord.Embed(colour=discord.Colour.blue())
|
||||
embed.set_author(name="FAQ {}".format(stringFormatters.titleCase(category)))
|
||||
embed.set_author(name="FAQ {}".format(stringFormatters.title_case(category)))
|
||||
|
||||
# Add everything into the embed
|
||||
for i, pair in enumerate(resp):
|
||||
|
|
|
@ -120,7 +120,7 @@ class Fun(commands.Cog):
|
|||
memeList = memes.getAllMemes()
|
||||
|
||||
# Turn the list into a list of [Name: fields]
|
||||
memeList = [": ".join([stringFormatters.titleCase(meme[1]),
|
||||
memeList = [": ".join([stringFormatters.title_case(meme[1]),
|
||||
str(meme[2])]) for meme in sorted(memeList, key=lambda x: x[1])]
|
||||
|
||||
pages = paginatedLeaderboard.Pages(source=paginatedLeaderboard.Source(memeList, "Memes", discord.Colour.blue()),
|
||||
|
|
|
@ -0,0 +1,26 @@
|
|||
from discord.ext import commands
|
||||
from dislash import SlashInteraction, slash_command, Option, OptionType
|
||||
|
||||
from data.embeds.translate import Translation
|
||||
from startup.didier import Didier
|
||||
|
||||
|
||||
class TranslateSlash(commands.Cog):
|
||||
def __init__(self, client: Didier):
|
||||
self.client: Didier = client
|
||||
|
||||
@slash_command(
|
||||
name="translate",
|
||||
description="Google Translate",
|
||||
options=[
|
||||
Option("text", "Tekst om te vertalen", OptionType.STRING, required=True),
|
||||
Option("to", "Taal om naar te vertalen (default NL)", OptionType.STRING)
|
||||
]
|
||||
)
|
||||
async def _translate_slash(self, interaction: SlashInteraction, text: str, to: str = "nl"):
|
||||
translation = Translation(text=text, to=to.lower())
|
||||
await interaction.reply(embed=translation.to_embed())
|
||||
|
||||
|
||||
def setup(client: Didier):
|
||||
client.add_cog(TranslateSlash(client))
|
|
@ -2,7 +2,7 @@ from decorators import help
|
|||
import discord
|
||||
from discord.ext import commands
|
||||
from enums.help_categories import Category
|
||||
from functions.stringFormatters import titleCase as tc
|
||||
from functions.stringFormatters import title_case as tc
|
||||
from googletrans import Translator, LANGUAGES
|
||||
import re
|
||||
|
||||
|
|
|
@ -0,0 +1,54 @@
|
|||
import discord
|
||||
from googletrans import Translator, LANGUAGES
|
||||
from functions.stringFormatters import title_case
|
||||
from typing import Optional
|
||||
|
||||
|
||||
class Translation:
|
||||
def __init__(self, text: str, to: str):
|
||||
self.text = text
|
||||
self.to = to
|
||||
self.embed: Optional[discord.Embed] = None
|
||||
self.translation = None
|
||||
|
||||
self.translate(text, to)
|
||||
|
||||
def translate(self, query: str, to: str):
|
||||
"""
|
||||
Translate [query] into [to]
|
||||
"""
|
||||
try:
|
||||
translator = Translator()
|
||||
self.translation = translator.translate(query, to, "auto")
|
||||
except ValueError as e:
|
||||
message = str(e)
|
||||
|
||||
if "destination" in message:
|
||||
self._create_error_embed(f"{title_case(to)} is geen geldige taal.")
|
||||
|
||||
raise e
|
||||
|
||||
def _create_error_embed(self, message):
|
||||
embed = discord.Embed(colour=discord.Colour.red())
|
||||
embed.set_author(name="Didier Translate")
|
||||
embed.description = message
|
||||
self.embed = embed
|
||||
|
||||
def to_embed(self) -> discord.Embed:
|
||||
# There's an error embed to show
|
||||
if self.embed is not None:
|
||||
return self.embed
|
||||
|
||||
embed = discord.Embed(colour=discord.Colour.blue())
|
||||
embed.set_author(name="Didier Translate")
|
||||
|
||||
language = self.translation.src
|
||||
embed.add_field(name="Gedetecteerde taal", value=title_case(LANGUAGES[language]))
|
||||
|
||||
if self.translation.extra_data["confidence"] is not None:
|
||||
embed.add_field(name="Zekerheid", value="{}%".format(self.translation.extra_data["confidence"] * 100))
|
||||
|
||||
embed.add_field(name="Origineel ({})".format(self.translation.src.upper()), value=self.text, inline=False)
|
||||
embed.add_field(name="Vertaling ({})".format(self.to.upper()), value=self.translation.text)
|
||||
|
||||
return embed
|
|
@ -1,6 +1,6 @@
|
|||
from datetime import datetime
|
||||
from discord import Embed, Colour
|
||||
from functions.stringFormatters import leadingZero as lz
|
||||
from functions.stringFormatters import leading_zero as lz
|
||||
from functions.timeFormatters import intToWeekday
|
||||
from markdownify import markdownify as md
|
||||
import pytz
|
||||
|
|
|
@ -5,7 +5,7 @@ from datetime import datetime
|
|||
from enum import Enum
|
||||
from functions.timeFormatters import fromString
|
||||
from functions.scrapers.sporza import getJPLMatches, getJPLTable
|
||||
from functions.stringFormatters import leadingZero
|
||||
from functions.stringFormatters import leading_zero
|
||||
import re
|
||||
from requests import get
|
||||
import tabulate
|
||||
|
@ -103,7 +103,7 @@ class Match:
|
|||
|
||||
# No score to show yet, show time when the match starts
|
||||
if not self._hasStarted():
|
||||
return "{}:{}".format(leadingZero(str(self.start.hour)), leadingZero(str(self.start.minute)))
|
||||
return "{}:{}".format(leading_zero(str(self.start.hour)), leading_zero(str(self.start.minute)))
|
||||
|
||||
return "{} - {}".format(self.homeScore, self.awayScore)
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
def titleCase(string):
|
||||
def title_case(string):
|
||||
return " ".join(capitalize(word) for word in string.split(" "))
|
||||
|
||||
|
||||
|
@ -8,7 +8,7 @@ def capitalize(string):
|
|||
return string[0].upper()
|
||||
|
||||
|
||||
def leadingZero(string, size=2):
|
||||
def leading_zero(string, size=2):
|
||||
string = str(string)
|
||||
while len(string) < size:
|
||||
string = "0" + string
|
||||
|
|
|
@ -161,14 +161,14 @@ def fromString(timeString: str, formatString="%d/%m/%Y", tzinfo=pytz.timezone("E
|
|||
|
||||
|
||||
def fromArray(data: List[int]) -> datetime:
|
||||
day = stringFormatters.leadingZero(str(data[0]))
|
||||
month = stringFormatters.leadingZero(str(data[1]))
|
||||
day = stringFormatters.leading_zero(str(data[0]))
|
||||
month = stringFormatters.leading_zero(str(data[1]))
|
||||
year = str(data[2])
|
||||
|
||||
if len(data) == 6:
|
||||
hour = stringFormatters.leadingZero(str(data[3]))
|
||||
minute = stringFormatters.leadingZero(str(data[4]))
|
||||
second = stringFormatters.leadingZero(str(data[5]))
|
||||
hour = stringFormatters.leading_zero(str(data[3]))
|
||||
minute = stringFormatters.leading_zero(str(data[4]))
|
||||
second = stringFormatters.leading_zero(str(data[5]))
|
||||
|
||||
return fromString(f"{day}/{month}/{year} {hour}:{minute}:{second}", formatString="%d/%m/%Y %H:%M:%S")
|
||||
|
||||
|
|
Loading…
Reference in New Issue