mirror of
https://github.com/neoarz/Syntrel.git
synced 2025-12-25 03:40:11 +01:00
fix(translate):auto-complete not showing
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
import discord
|
||||
from discord import app_commands
|
||||
from discord.ext import commands
|
||||
from discord.ext.commands import Context
|
||||
|
||||
from .translate import translate_command
|
||||
from .translate import translate_command, language_autocomplete
|
||||
from .codepreview import codepreview_command
|
||||
|
||||
class Utilities(commands.GroupCog, name="utils"):
|
||||
@@ -39,6 +40,13 @@ class Utilities(commands.GroupCog, name="utils"):
|
||||
return content.startswith(f"{prefix}{group} ")
|
||||
|
||||
@utilities_group.command(name="translate")
|
||||
@app_commands.describe(
|
||||
text="The text to translate",
|
||||
to_lang="Target language (e.g., 'en', 'es', 'fr')",
|
||||
from_lang="Source language (leave empty for auto-detect)"
|
||||
)
|
||||
@app_commands.autocomplete(to_lang=language_autocomplete)
|
||||
@app_commands.autocomplete(from_lang=language_autocomplete)
|
||||
async def utilities_group_translate(self, context: Context, text: str = None, to_lang: str = "en", from_lang: str = None):
|
||||
await self._invoke_hybrid(context, "translate", text=text, to_lang=to_lang, from_lang=from_lang)
|
||||
|
||||
@@ -51,6 +59,13 @@ class Utilities(commands.GroupCog, name="utils"):
|
||||
name="translate",
|
||||
description="Translate text to another language"
|
||||
)
|
||||
@app_commands.describe(
|
||||
text="The text to translate",
|
||||
to_lang="Target language (e.g., 'en', 'es', 'fr')",
|
||||
from_lang="Source language (leave empty for auto-detect)"
|
||||
)
|
||||
@app_commands.autocomplete(to_lang=language_autocomplete)
|
||||
@app_commands.autocomplete(from_lang=language_autocomplete)
|
||||
async def translate(self, context, text: str = None, to_lang: str = "en", from_lang: str = None):
|
||||
return await translate_command()(self, context, text=text, to_lang=to_lang, from_lang=from_lang)
|
||||
|
||||
|
||||
@@ -9,8 +9,7 @@ import json
|
||||
import urllib.parse
|
||||
|
||||
|
||||
def translate_command():
|
||||
languages = {
|
||||
languages = {
|
||||
"auto": "Auto-detect",
|
||||
"en": "English",
|
||||
"es": "Spanish",
|
||||
@@ -134,6 +133,30 @@ def translate_command():
|
||||
"lv": "Latvian",
|
||||
}
|
||||
|
||||
async def language_autocomplete(interaction: discord.Interaction, current: str) -> list[app_commands.Choice[str]]:
|
||||
current = current.lower()
|
||||
choices = []
|
||||
|
||||
for code, name in languages.items():
|
||||
if current in code.lower() or current in name.lower():
|
||||
display_name = f"{code} - {name}"
|
||||
if len(display_name) > 100:
|
||||
display_name = f"{code} - {name[:90]}..."
|
||||
choices.append(app_commands.Choice(name=display_name, value=code))
|
||||
|
||||
if len(choices) >= 25:
|
||||
break
|
||||
|
||||
if not choices:
|
||||
popular = ["en", "es", "fr", "de", "it", "pt", "ru", "ja", "ko", "zh-CN"]
|
||||
for code in popular:
|
||||
name = languages.get(code, code)
|
||||
choices.append(app_commands.Choice(name=f"{code} - {name}", value=code))
|
||||
|
||||
return choices
|
||||
|
||||
def translate_command():
|
||||
|
||||
async def send_embed(context, embed: discord.Embed, *, ephemeral: bool = False) -> None:
|
||||
interaction = getattr(context, "interaction", None)
|
||||
if interaction is not None:
|
||||
@@ -194,27 +217,6 @@ def translate_command():
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
async def language_autocomplete(interaction: discord.Interaction, current: str) -> list[app_commands.Choice[str]]:
|
||||
current = current.lower()
|
||||
choices = []
|
||||
|
||||
for code, name in languages.items():
|
||||
if current in code.lower() or current in name.lower():
|
||||
display_name = f"{code} - {name}"
|
||||
if len(display_name) > 100:
|
||||
display_name = f"{code} - {name[:90]}..."
|
||||
choices.append(app_commands.Choice(name=display_name, value=code))
|
||||
|
||||
if len(choices) >= 25:
|
||||
break
|
||||
|
||||
if not choices:
|
||||
popular = ["en", "es", "fr", "de", "it", "pt", "ru", "ja", "ko", "zh-CN"]
|
||||
for code in popular:
|
||||
name = languages.get(code, code)
|
||||
choices.append(app_commands.Choice(name=f"{code} - {name}", value=code))
|
||||
|
||||
return choices
|
||||
|
||||
@commands.hybrid_command(
|
||||
name="translate",
|
||||
|
||||
Reference in New Issue
Block a user