Ruby-Cogs/translator/translator.py

277 lines
No EOL
12 KiB
Python

from redbot.core import commands, Config
from googletrans import Translator, LANGUAGES
import discord
from discord import app_commands
import asyncio
from datetime import datetime
from redbot.core.bot import Red
from . import ButtonMenu
class TranslatorCog(commands.Cog):
"""Translate messages using Google Translate"""
def __init__(self, bot: Red):
self.bot = bot
self.translator = Translator()
self.languages = LANGUAGES
self.config = Config.get_conf(self, identifier=95932766180, force_registration=True)
self.config.register_guild(use_embeds=True)
async def cog_unload(self):
"""Cleanup when cog is unloaded."""
if hasattr(self, 'translator'):
del self.translator
@commands.group(name="transset")
@commands.admin_or_permissions(manage_guild=True)
async def _transset(self, ctx):
"""Configure translator settings"""
if ctx.invoked_subcommand is None:
current_setting = await self.config.guild(ctx.guild).use_embeds()
await ctx.send(f"Current embed setting: {current_setting}")
@_transset.command(name="embed")
async def _transset_embed(self, ctx, toggle: bool):
"""Toggle whether to use embeds for translations or plain text"""
await self.config.guild(ctx.guild).use_embeds.set(toggle)
await ctx.send(f"Embed display has been {'enabled' if toggle else 'disabled'}.")
async def translate_and_respond(self, ctx, text_to_translate, dest_lang="en", source_lang="auto", original_author=None, ephemeral=False, reference_message=None):
try:
# Validate destination language
if dest_lang.lower() not in self.languages and dest_lang.lower() not in {v.lower(): k for k, v in self.languages.items()}:
return await ctx.send(
f"Invalid language code. Use `{ctx.prefix}langlist` to see available languages.",
ephemeral=True,
reference=reference_message
)
# Convert language name to code if full name was provided
if dest_lang.lower() in {v.lower(): k for k, v in self.languages.items()}:
dest_lang = next(k for k, v in self.languages.items() if v.lower() == dest_lang.lower())
# Translate the text
translation = self.translator.translate(text_to_translate, dest=dest_lang, src=source_lang)
use_embeds = await self.config.guild(ctx.guild).use_embeds()
if use_embeds:
embed = discord.Embed(
color=discord.Color.from_rgb(25, 118, 210), # Material Design Blue
timestamp=datetime.utcnow()
)
# Set author with more context
if original_author:
embed.set_author(
name=f"Translation requested by {ctx.author.display_name}",
icon_url=ctx.author.display_avatar.url
)
if original_author != ctx.author:
embed.set_footer(
text=f"Original message by {original_author.display_name} • ID: {ctx.author.id}",
icon_url=original_author.display_avatar.url
)
else:
embed.set_footer(text=f"Requested by {ctx.author} • ID: {ctx.author.id}")
else:
embed.set_author(
name=f"Translation by {ctx.author.display_name}",
icon_url=ctx.author.display_avatar.url
)
embed.set_footer(text=f"Requested by {ctx.author} • ID: {ctx.author.id}")
# Add source language field with emoji
source_lang_name = self.languages.get(translation.src, translation.src).title()
embed.add_field(
name=f"📝 Original ({source_lang_name})",
value=f"```\n{text_to_translate[:1000]}```",
inline=False
)
# Add translation field with emoji
dest_lang_name = self.languages.get(translation.dest, translation.dest).title()
embed.add_field(
name=f"🔄 Translation ({dest_lang_name})",
value=f"```\n{translation.text[:1000]}```",
inline=False
)
# Add confidence if available
if hasattr(translation, 'confidence') and translation.confidence:
confidence = int(translation.confidence * 100)
embed.add_field(
name="🎯 Confidence",
value=f"{confidence}%",
inline=True
)
await ctx.send(
embed=embed,
ephemeral=ephemeral,
reference=reference_message
)
else:
author_text = f"\nOriginal message by: {original_author.display_name}" if original_author else ""
response = (
f"{ctx.author.mention}\n"
f"**Original** ({translation.src}):{author_text}\n{text_to_translate}\n\n"
f"**Translation** ({translation.dest}):\n{translation.text}\n\n"
f"Requested by {ctx.author} (ID: {ctx.author.id})"
)
await ctx.send(
response,
ephemeral=ephemeral,
reference=reference_message
)
except Exception as e:
error_embed = discord.Embed(
title="❌ Translation Error",
description=f"An error occurred while translating: {str(e)}",
color=discord.Color.red()
)
error_embed.set_footer(text=f"Requested by {ctx.author} • ID: {ctx.author.id}")
await ctx.send(
embed=error_embed if use_embeds else f"An error occurred while translating: {str(e)}",
ephemeral=True,
reference=reference_message
)
@commands.command(name="translate")
async def translate_text(self, ctx, lang_code: str = "en", *, message=None):
"""Translate a message
Reply to a message with just the language code to translate to that language
Or provide text after the language code to translate that text
Examples:
- Reply to a message: [p]translate jp
- Translate text: [p]translate fr Hello, how are you?
- Auto-detect and translate to English: [p]translate This will detect the language
"""
# Initialize variables
text_to_translate = None
original_author = None
reference_message = None
# If replying to a message
if ctx.message.reference:
try:
referenced_msg = await ctx.fetch_message(ctx.message.reference.message_id)
text_to_translate = referenced_msg.content
original_author = referenced_msg.author
reference_message = referenced_msg
except discord.NotFound:
await ctx.send("Could not find the message you're replying to.")
return
except discord.Forbidden:
await ctx.send("I don't have permission to read the message you're replying to.")
return
# If no language code is provided in a reply, default to English
if not lang_code or lang_code.lower() not in self.languages:
if message: # If there's additional text, it means the first argument was text, not a lang code
text_to_translate = f"{lang_code} {message}"
lang_code = "en"
else:
# Valid language code provided
if message: # If there's a message after the lang code, use that instead of the replied message
text_to_translate = message
original_author = ctx.author
reference_message = ctx.message
else:
# Not replying to a message
original_author = ctx.author
reference_message = ctx.message
if not message: # Only language code provided without text
await ctx.send("Please provide text to translate or reply to a message.", reference=ctx.message)
return
# Check if the "lang_code" is actually the start of the message
if lang_code.lower() not in self.languages:
text_to_translate = f"{lang_code} {message}" if message else lang_code
lang_code = "en" # Default to English for direct translation
else:
text_to_translate = message
# If we still don't have text to translate
if not text_to_translate:
await ctx.send("Please provide text to translate or reply to a message.", reference=ctx.message)
return
await self.translate_and_respond(
ctx,
text_to_translate,
dest_lang=lang_code,
source_lang="auto",
original_author=original_author,
reference_message=reference_message
)
@commands.command(name="langlist")
async def language_list(self, ctx):
"""List all available language codes"""
# Create a sorted list of languages with their codes
sorted_languages = sorted(self.languages.items(), key=lambda x: x[1])
# Create the main embed
embed = discord.Embed(
title="🌐 Available Languages",
description="Use these codes with the translate command\nFormat: `code`: Language Name",
color=discord.Color.from_rgb(25, 118, 210), # Material Design Blue
timestamp=datetime.utcnow()
)
# Split languages into regions/categories for better organization
common_languages = ["en", "es", "fr", "de", "it", "pt", "ru", "ja", "ko", "zh-cn"]
# Add common languages field
common_lang_list = []
for code in common_languages:
if code in self.languages:
common_lang_list.append(f"`{code}`: {self.languages[code]}")
embed.add_field(
name="📌 Common Languages",
value="\n".join(common_lang_list),
inline=False
)
# Create chunks of remaining languages
remaining_languages = [(code, lang) for code, lang in sorted_languages if code not in common_languages]
chunks = [remaining_languages[i:i + 15] for i in range(0, len(remaining_languages), 15)]
# Add footer with requester info
embed.set_footer(text=f"Requested by {ctx.author} • Page 1/{len(chunks) + 1}")
# Create list of embeds
embeds = [embed]
# Create additional embeds for remaining languages
for i, chunk in enumerate(chunks):
chunk_embed = discord.Embed(
color=discord.Color.from_rgb(25, 118, 210),
timestamp=datetime.utcnow()
)
chunk_embed.description = "\n".join(f"`{code}`: {lang}" for code, lang in chunk)
chunk_embed.set_footer(text=f"Requested by {ctx.author} • Page {i+2}/{len(chunks) + 1}")
embeds.append(chunk_embed)
# Send the embeds with button navigation
view = ButtonMenu(embeds)
await ctx.send(embed=embeds[0], view=view)
@app_commands.command(name="translate")
@app_commands.describe(
text="The text to translate",
to_language="The language to translate to (e.g., 'en' for English)",
from_language="The language to translate from (optional)"
)
async def slash_translate(self, interaction: discord.Interaction, text: str, to_language: str, from_language: str = "auto"):
"""Translate text from one language to another"""
await interaction.response.defer(ephemeral=True)
await self.translate_and_respond(interaction, text, to_language, from_language, interaction.user, ephemeral=True)
async def setup(bot):
await bot.add_cog(TranslatorCog(bot))