speed up moderations and smoothen translations

Kaveen Kumarasinghe 2 years ago
parent d683eafdfc
commit 4df4096f02

@ -128,8 +128,8 @@ class ModerationsService(discord.Cog, name="ModerationsService"):
Moderation.moderation_tasks[guild_id] = asyncio.ensure_future(
Moderation.process_moderation_queue(
Moderation.moderation_queues[guild_id],
1,
1,
0.25,
0.25,
moderations_channel,
warn_set,
delete_set,

@ -511,9 +511,9 @@ class GPT3ComCon(discord.Cog, name="GPT3ComCon"):
after.guild.id in Moderation.moderation_queues
and Moderation.moderation_queues[after.guild.id] is not None
):
# Create a timestamp that is 0.5 seconds from now
# Create a timestamp that is 0.25 seconds from now
timestamp = (
datetime.datetime.now() + datetime.timedelta(seconds=0.5)
datetime.datetime.now() + datetime.timedelta(seconds=0.25)
).timestamp()
await Moderation.moderation_queues[after.guild.id].put(
Moderation(after, timestamp)

@ -1,3 +1,5 @@
import traceback
import aiohttp
import discord
@ -8,14 +10,14 @@ from services.environment_service import EnvService
ALLOWED_GUILDS = EnvService.get_allowed_guilds()
def build_translation_embed(text, translated_text, translated_language):
def build_translation_embed(text, translated_text, translated_language, detected_language):
"""Build an embed for the translation"""
embed_description = f"**Original Text:** \n\n{text}\n\n **Translated Text:** \n\n{translated_text}"
embed = discord.Embed(
title=f"Translation to {translated_language}",
title=f"Translation from {detected_language} to {translated_language}",
description=embed_description,
color=0x311432,
)
embed.add_field(name="Original text", value=text, inline=False)
embed.add_field(name="Translated Text", value=translated_text, inline=False)
return embed
@ -65,7 +67,7 @@ class TranslationService(discord.Cog, name="TranslationService"):
return
try:
response = await self.translation_model.send_translate_request(
response, detected_language = await self.translation_model.send_translate_request(
text,
TranslationModel.get_country_code_from_name(target_language),
formality,
@ -75,7 +77,7 @@ class TranslationService(discord.Cog, name="TranslationService"):
return
await ctx.respond(
embed=build_translation_embed(text, response, target_language)
embed=build_translation_embed(text, response, target_language, TranslationModel.get_country_name_from_code(detected_language))
)
async def translate_action(self, ctx, message):
@ -96,6 +98,8 @@ class TranslationService(discord.Cog, name="TranslationService"):
class TranslateView(discord.ui.View):
def __init__(self, translation_model, message, selection_message):
super().__init__()
self.language_long = None
self.language = None
self.translation_model = translation_model
self.message = message
self.selection_message = selection_message
@ -115,20 +119,27 @@ class TranslateView(discord.ui.View):
async def select_callback(
self, select, interaction
): # the function called when the user is done selecting options
try:
await interaction.response.defer()
response = await self.translation_model.send_translate_request(
self.message.content,
TranslationModel.get_country_code_from_name(select.values[0]),
self.formality,
)
await self.message.reply(
mention_author=False,
embed=build_translation_embed(
self.message.content, response, select.values[0]
),
try:
self.language = TranslationModel.get_country_code_from_name(select.values[0])
self.language_long = select.values[0]
except:
traceback.print_exc()
@discord.ui.select(
placeholder="Formality (optional)",
min_values=1,
max_values=1,
options=[
discord.SelectOption(label="Prefer more", value="prefer_more"),
discord.SelectOption(label="default", value="default"),
discord.SelectOption(label="Prefer less", value="prefer_less"),
],
)
await self.selection_message.delete()
async def formality_callback(self, select, interaction):
try:
self.formality = select.values[0]
await interaction.response.defer()
except aiohttp.ClientResponseError as e:
await interaction.response.send_message(
f"There was an error with the DeepL API: {e.message}",
@ -142,20 +153,28 @@ class TranslateView(discord.ui.View):
)
return
@discord.ui.select(
placeholder="Formality (optional)",
min_values=1,
max_values=1,
options=[
discord.SelectOption(label="Prefer more", value="prefer_more"),
discord.SelectOption(label="default", value="default"),
discord.SelectOption(label="Prefer less", value="prefer_less"),
],
# A button "Translate"
@discord.ui.button(label="Translate", style=discord.ButtonStyle.green)
async def button_callback(self, button, interaction):
if not self.language or not self.language_long:
await interaction.response.send_message(
"Please select a language first.", ephemeral=True, delete_after=15
)
async def formality_callback(self, select, interaction):
return
try:
self.formality = select.values[0]
await interaction.response.defer()
response, detected_language = await self.translation_model.send_translate_request(
self.message.content,
self.language,
self.formality,
)
await self.message.reply(
mention_author=False,
embed=build_translation_embed(
self.message.content, response, self.language_long, TranslationModel.get_country_name_from_code(detected_language)
),
)
await self.selection_message.delete()
except aiohttp.ClientResponseError as e:
await interaction.response.send_message(
f"There was an error with the DeepL API: {e.message}",

@ -52,9 +52,6 @@ class TranslationModel:
on_backoff=backoff_handler,
)
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)
async with aiohttp.ClientSession(raise_for_status=True) as session:
payload = {
"text": text,
@ -74,7 +71,7 @@ class TranslationModel:
print(response)
try:
return response["translations"][0]["text"]
return response["translations"][0]["text"], response["translations"][0]["detected_source_language"]
except Exception:
print(response)
traceback.print_exc()
@ -97,7 +94,10 @@ class TranslationModel:
@staticmethod
def get_country_name_from_code(code):
"""Get the country name from the code"""
try:
return COUNTRY_CODES[code]
except KeyError:
return "Unknown Language"
@staticmethod
def get_country_code_from_name(name):

Loading…
Cancel
Save