mirror of
https://github.com/neoarz/Syntrel.git
synced 2025-12-25 03:40:11 +01:00
chore: ruff formatting
This commit is contained in:
@@ -20,10 +20,14 @@ class Utilities(commands.GroupCog, name="utils"):
|
||||
embed = discord.Embed(
|
||||
title="Utilities Commands",
|
||||
description="Use `.utils <subcommand>` or `/utils <subcommand>`.",
|
||||
color=0x7289DA
|
||||
color=0x7289DA,
|
||||
)
|
||||
embed.set_author(
|
||||
name="Utilities", icon_url="https://yes.nighty.works/raw/8VLDcg.webp"
|
||||
)
|
||||
embed.add_field(
|
||||
name="Available", value="translate, codepreview, dictionary", inline=False
|
||||
)
|
||||
embed.set_author(name="Utilities", icon_url="https://yes.nighty.works/raw/8VLDcg.webp")
|
||||
embed.add_field(name="Available", value="translate, codepreview, dictionary", inline=False)
|
||||
await context.send(embed=embed)
|
||||
|
||||
async def _invoke_hybrid(self, context: Context, name: str, **kwargs):
|
||||
@@ -47,62 +51,68 @@ class Utilities(commands.GroupCog, name="utils"):
|
||||
@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)"
|
||||
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)
|
||||
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
|
||||
)
|
||||
|
||||
@utilities_group.command(name="codepreview")
|
||||
async def utilities_group_codepreview(self, context: Context, url: str = None):
|
||||
await self._invoke_hybrid(context, "codepreview", url=url)
|
||||
|
||||
@utilities_group.command(name="dictionary")
|
||||
@app_commands.describe(
|
||||
word="The word to look up"
|
||||
)
|
||||
@app_commands.describe(word="The word to look up")
|
||||
async def utilities_group_dictionary(self, context: Context, word: str = None):
|
||||
await self._invoke_hybrid(context, "dictionary", word=word)
|
||||
|
||||
@commands.check(_require_group_prefix)
|
||||
@commands.hybrid_command(
|
||||
name="translate",
|
||||
description="Translate text to another language"
|
||||
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)"
|
||||
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)
|
||||
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
|
||||
)
|
||||
|
||||
@commands.check(_require_group_prefix)
|
||||
@commands.hybrid_command(
|
||||
name="codepreview",
|
||||
description="Preview code from GitHub URLs"
|
||||
name="codepreview", description="Preview code from GitHub URLs"
|
||||
)
|
||||
async def codepreview(self, context, url: str = None):
|
||||
return await codepreview_command()(self, context, url=url)
|
||||
|
||||
@commands.check(_require_group_prefix)
|
||||
@commands.hybrid_command(
|
||||
name="dictionary",
|
||||
description="Get the definition of a word"
|
||||
)
|
||||
@app_commands.describe(
|
||||
word="The word to look up"
|
||||
name="dictionary", description="Get the definition of a word"
|
||||
)
|
||||
@app_commands.describe(word="The word to look up")
|
||||
async def dictionary(self, context, word: str = None):
|
||||
return await dictionary_command()(self, context, word=word)
|
||||
|
||||
|
||||
async def setup(bot) -> None:
|
||||
cog = Utilities(bot)
|
||||
await bot.add_cog(cog)
|
||||
|
||||
|
||||
bot.logger.info("Loaded extension 'utilities.translate'")
|
||||
bot.logger.info("Loaded extension 'utilities.codepreview'")
|
||||
bot.logger.info("Loaded extension 'utilities.dictionary'")
|
||||
|
||||
@@ -9,40 +9,44 @@ import json
|
||||
|
||||
def codepreview_command():
|
||||
LANGUAGE_MAP = {
|
||||
'.py': 'python',
|
||||
'.js': 'js',
|
||||
'.ts': 'ts',
|
||||
'.java': 'java',
|
||||
'.cpp': 'cpp',
|
||||
'.c': 'c',
|
||||
'.cs': 'cs',
|
||||
'.go': 'go',
|
||||
'.rs': 'rust',
|
||||
'.rb': 'ruby',
|
||||
'.php': 'php',
|
||||
'.html': 'html',
|
||||
'.css': 'css',
|
||||
'.json': 'json',
|
||||
'.xml': 'xml',
|
||||
'.yaml': 'yaml',
|
||||
'.yml': 'yaml',
|
||||
'.ini': 'ini',
|
||||
'.toml': 'toml',
|
||||
'.lua': 'lua',
|
||||
'.sh': 'bash',
|
||||
'.md': 'markdown',
|
||||
'.sql': 'sql',
|
||||
'.diff': 'diff',
|
||||
'.txt': '',
|
||||
".py": "python",
|
||||
".js": "js",
|
||||
".ts": "ts",
|
||||
".java": "java",
|
||||
".cpp": "cpp",
|
||||
".c": "c",
|
||||
".cs": "cs",
|
||||
".go": "go",
|
||||
".rs": "rust",
|
||||
".rb": "ruby",
|
||||
".php": "php",
|
||||
".html": "html",
|
||||
".css": "css",
|
||||
".json": "json",
|
||||
".xml": "xml",
|
||||
".yaml": "yaml",
|
||||
".yml": "yaml",
|
||||
".ini": "ini",
|
||||
".toml": "toml",
|
||||
".lua": "lua",
|
||||
".sh": "bash",
|
||||
".md": "markdown",
|
||||
".sql": "sql",
|
||||
".diff": "diff",
|
||||
".txt": "",
|
||||
}
|
||||
|
||||
async def send_embed(context, embed: discord.Embed, *, ephemeral: bool = False) -> None:
|
||||
async def send_embed(
|
||||
context, embed: discord.Embed, *, ephemeral: bool = False
|
||||
) -> None:
|
||||
interaction = getattr(context, "interaction", None)
|
||||
if interaction is not None:
|
||||
if interaction.response.is_done():
|
||||
await interaction.followup.send(embed=embed, ephemeral=ephemeral)
|
||||
else:
|
||||
await interaction.response.send_message(embed=embed, ephemeral=ephemeral)
|
||||
await interaction.response.send_message(
|
||||
embed=embed, ephemeral=ephemeral
|
||||
)
|
||||
else:
|
||||
await context.send(embed=embed)
|
||||
|
||||
@@ -50,7 +54,7 @@ def codepreview_command():
|
||||
for ext, lang in LANGUAGE_MAP.items():
|
||||
if filename.endswith(ext):
|
||||
return lang
|
||||
return ''
|
||||
return ""
|
||||
|
||||
async def fetch_github_content(url):
|
||||
try:
|
||||
@@ -64,12 +68,12 @@ def codepreview_command():
|
||||
|
||||
async def fetch_pr_diff(owner, repo, pr_number):
|
||||
try:
|
||||
api_url = f'https://api.github.com/repos/{owner}/{repo}/pulls/{pr_number}'
|
||||
api_url = f"https://api.github.com/repos/{owner}/{repo}/pulls/{pr_number}"
|
||||
headers = {
|
||||
'Accept': 'application/vnd.github.v3.diff',
|
||||
'User-Agent': 'Discord-Bot'
|
||||
"Accept": "application/vnd.github.v3.diff",
|
||||
"User-Agent": "Discord-Bot",
|
||||
}
|
||||
|
||||
|
||||
async with aiohttp.ClientSession() as session:
|
||||
async with session.get(api_url, headers=headers) as response:
|
||||
if response.status == 200:
|
||||
@@ -81,98 +85,95 @@ def codepreview_command():
|
||||
|
||||
async def fetch_pr_info(owner, repo, pr_number):
|
||||
try:
|
||||
api_url = f'https://api.github.com/repos/{owner}/{repo}/pulls/{pr_number}'
|
||||
api_url = f"https://api.github.com/repos/{owner}/{repo}/pulls/{pr_number}"
|
||||
headers = {
|
||||
'Accept': 'application/vnd.github.v3+json',
|
||||
'User-Agent': 'Discord-Bot'
|
||||
"Accept": "application/vnd.github.v3+json",
|
||||
"User-Agent": "Discord-Bot",
|
||||
}
|
||||
|
||||
|
||||
async with aiohttp.ClientSession() as session:
|
||||
async with session.get(api_url, headers=headers) as response:
|
||||
if response.status == 200:
|
||||
pr_data = await response.json()
|
||||
return {
|
||||
'title': pr_data.get('title', ''),
|
||||
'number': pr_data.get('number', pr_number),
|
||||
'state': pr_data.get('state', ''),
|
||||
'merged': pr_data.get('merged', False),
|
||||
'additions': pr_data.get('additions', 0),
|
||||
'deletions': pr_data.get('deletions', 0),
|
||||
'changed_files': pr_data.get('changed_files', 0),
|
||||
'user': pr_data.get('user', {}).get('login', ''),
|
||||
'base_branch': pr_data.get('base', {}).get('ref', ''),
|
||||
'head_branch': pr_data.get('head', {}).get('ref', '')
|
||||
"title": pr_data.get("title", ""),
|
||||
"number": pr_data.get("number", pr_number),
|
||||
"state": pr_data.get("state", ""),
|
||||
"merged": pr_data.get("merged", False),
|
||||
"additions": pr_data.get("additions", 0),
|
||||
"deletions": pr_data.get("deletions", 0),
|
||||
"changed_files": pr_data.get("changed_files", 0),
|
||||
"user": pr_data.get("user", {}).get("login", ""),
|
||||
"base_branch": pr_data.get("base", {}).get("ref", ""),
|
||||
"head_branch": pr_data.get("head", {}).get("ref", ""),
|
||||
}
|
||||
except Exception:
|
||||
pass
|
||||
return None
|
||||
|
||||
def parse_github_url(url):
|
||||
pr_pattern = r'https://github\.com/([^/]+)/([^/]+)/pull/(\d+)(?:/files)?'
|
||||
pr_pattern = r"https://github\.com/([^/]+)/([^/]+)/pull/(\d+)(?:/files)?"
|
||||
pr_match = re.match(pr_pattern, url)
|
||||
|
||||
|
||||
if pr_match:
|
||||
owner, repo, pr_number = pr_match.groups()
|
||||
return {
|
||||
'type': 'pr',
|
||||
'owner': owner,
|
||||
'repo': repo,
|
||||
'pr_number': pr_number
|
||||
}
|
||||
|
||||
raw_pattern = r'https://raw\.githubusercontent\.com/([^/]+)/([^/]+)/([^/]+)/(.+?)$'
|
||||
return {"type": "pr", "owner": owner, "repo": repo, "pr_number": pr_number}
|
||||
|
||||
raw_pattern = (
|
||||
r"https://raw\.githubusercontent\.com/([^/]+)/([^/]+)/([^/]+)/(.+?)$"
|
||||
)
|
||||
raw_match = re.match(raw_pattern, url)
|
||||
|
||||
|
||||
if raw_match:
|
||||
owner, repo, branch, filepath = raw_match.groups()
|
||||
|
||||
|
||||
return {
|
||||
'type': 'file',
|
||||
'owner': owner,
|
||||
'repo': repo,
|
||||
'branch': branch,
|
||||
'filepath': filepath,
|
||||
'raw_url': url,
|
||||
'start_line': None,
|
||||
'end_line': None
|
||||
"type": "file",
|
||||
"owner": owner,
|
||||
"repo": repo,
|
||||
"branch": branch,
|
||||
"filepath": filepath,
|
||||
"raw_url": url,
|
||||
"start_line": None,
|
||||
"end_line": None,
|
||||
}
|
||||
|
||||
pattern = r'https://github\.com/([^/]+)/([^/]+)/blob/([^/]+)/(.+?)(?:#L(\d+)(?:-L(\d+))?)?$'
|
||||
|
||||
pattern = r"https://github\.com/([^/]+)/([^/]+)/blob/([^/]+)/(.+?)(?:#L(\d+)(?:-L(\d+))?)?$"
|
||||
match = re.match(pattern, url)
|
||||
|
||||
|
||||
if match:
|
||||
owner, repo, branch, filepath, start_line, end_line = match.groups()
|
||||
|
||||
raw_url = f'https://raw.githubusercontent.com/{owner}/{repo}/{branch}/{filepath}'
|
||||
|
||||
|
||||
raw_url = (
|
||||
f"https://raw.githubusercontent.com/{owner}/{repo}/{branch}/{filepath}"
|
||||
)
|
||||
|
||||
return {
|
||||
'type': 'file',
|
||||
'owner': owner,
|
||||
'repo': repo,
|
||||
'branch': branch,
|
||||
'filepath': filepath,
|
||||
'raw_url': raw_url,
|
||||
'start_line': int(start_line) if start_line else None,
|
||||
'end_line': int(end_line) if end_line else None
|
||||
"type": "file",
|
||||
"owner": owner,
|
||||
"repo": repo,
|
||||
"branch": branch,
|
||||
"filepath": filepath,
|
||||
"raw_url": raw_url,
|
||||
"start_line": int(start_line) if start_line else None,
|
||||
"end_line": int(end_line) if end_line else None,
|
||||
}
|
||||
return None
|
||||
|
||||
def extract_lines(content, start_line, end_line):
|
||||
lines = content.split('\n')
|
||||
|
||||
lines = content.split("\n")
|
||||
|
||||
if start_line and end_line:
|
||||
return '\n'.join(lines[start_line-1:end_line])
|
||||
return "\n".join(lines[start_line - 1 : end_line])
|
||||
elif start_line:
|
||||
return lines[start_line-1] if start_line <= len(lines) else content
|
||||
return lines[start_line - 1] if start_line <= len(lines) else content
|
||||
return content
|
||||
|
||||
@commands.hybrid_command(
|
||||
name="codepreview",
|
||||
description="Preview code from GitHub URLs",
|
||||
)
|
||||
@app_commands.describe(
|
||||
url="GitHub URL to preview code from"
|
||||
)
|
||||
@app_commands.describe(url="GitHub URL to preview code from")
|
||||
async def codepreview(self, context, url: str = None):
|
||||
if isinstance(context.channel, discord.DMChannel):
|
||||
embed = discord.Embed(
|
||||
@@ -180,8 +181,10 @@ def codepreview_command():
|
||||
description="This command can only be used in servers.",
|
||||
color=0xE02B2B,
|
||||
)
|
||||
embed.set_author(name="Utility", icon_url="https://yes.nighty.works/raw/8VLDcg.webp")
|
||||
|
||||
embed.set_author(
|
||||
name="Utility", icon_url="https://yes.nighty.works/raw/8VLDcg.webp"
|
||||
)
|
||||
|
||||
interaction = getattr(context, "interaction", None)
|
||||
if interaction is not None:
|
||||
if not interaction.response.is_done():
|
||||
@@ -198,8 +201,10 @@ def codepreview_command():
|
||||
description="The bot needs the `send messages` permission in this channel.",
|
||||
color=0xE02B2B,
|
||||
)
|
||||
embed.set_author(name="Utility", icon_url="https://yes.nighty.works/raw/8VLDcg.webp")
|
||||
|
||||
embed.set_author(
|
||||
name="Utility", icon_url="https://yes.nighty.works/raw/8VLDcg.webp"
|
||||
)
|
||||
|
||||
interaction = getattr(context, "interaction", None)
|
||||
if interaction is not None:
|
||||
if not interaction.response.is_done():
|
||||
@@ -209,12 +214,18 @@ def codepreview_command():
|
||||
else:
|
||||
await context.send(embed=embed, ephemeral=True)
|
||||
return
|
||||
|
||||
|
||||
if not url or not url.strip():
|
||||
if context.message and context.message.reference and context.message.reference.resolved:
|
||||
if (
|
||||
context.message
|
||||
and context.message.reference
|
||||
and context.message.reference.resolved
|
||||
):
|
||||
replied_message = context.message.reference.resolved
|
||||
if hasattr(replied_message, 'content') and replied_message.content:
|
||||
github_pattern = r'https://(?:github\.com|raw\.githubusercontent\.com)/[^\s]+'
|
||||
if hasattr(replied_message, "content") and replied_message.content:
|
||||
github_pattern = (
|
||||
r"https://(?:github\.com|raw\.githubusercontent\.com)/[^\s]+"
|
||||
)
|
||||
urls = re.findall(github_pattern, replied_message.content)
|
||||
if urls:
|
||||
url = urls[0]
|
||||
@@ -223,7 +234,10 @@ def codepreview_command():
|
||||
title="Error",
|
||||
description="No GitHub URL found in the replied message.",
|
||||
color=0xE02B2B,
|
||||
).set_author(name="Utility", icon_url="https://yes.nighty.works/raw/8VLDcg.webp")
|
||||
).set_author(
|
||||
name="Utility",
|
||||
icon_url="https://yes.nighty.works/raw/8VLDcg.webp",
|
||||
)
|
||||
await send_embed(context, embed, ephemeral=True)
|
||||
return
|
||||
else:
|
||||
@@ -231,7 +245,10 @@ def codepreview_command():
|
||||
title="Error",
|
||||
description="The replied message has no content to extract GitHub URL from.",
|
||||
color=0xE02B2B,
|
||||
).set_author(name="Utility", icon_url="https://yes.nighty.works/raw/8VLDcg.webp")
|
||||
).set_author(
|
||||
name="Utility",
|
||||
icon_url="https://yes.nighty.works/raw/8VLDcg.webp",
|
||||
)
|
||||
await send_embed(context, embed, ephemeral=True)
|
||||
return
|
||||
else:
|
||||
@@ -239,23 +256,31 @@ def codepreview_command():
|
||||
title="Error",
|
||||
description="Please provide a GitHub URL or reply to a message containing a GitHub URL.",
|
||||
color=0xE02B2B,
|
||||
).set_author(name="Utility", icon_url="https://yes.nighty.works/raw/8VLDcg.webp")
|
||||
).set_author(
|
||||
name="Utility", icon_url="https://yes.nighty.works/raw/8VLDcg.webp"
|
||||
)
|
||||
await send_embed(context, embed, ephemeral=True)
|
||||
return
|
||||
|
||||
if not url.startswith('https://github.com/') and not url.startswith('https://raw.githubusercontent.com/'):
|
||||
|
||||
if not url.startswith("https://github.com/") and not url.startswith(
|
||||
"https://raw.githubusercontent.com/"
|
||||
):
|
||||
embed = discord.Embed(
|
||||
title="Error",
|
||||
description="Please provide a valid GitHub URL.",
|
||||
color=0xE02B2B,
|
||||
).set_author(name="Utility", icon_url="https://yes.nighty.works/raw/8VLDcg.webp")
|
||||
).set_author(
|
||||
name="Utility", icon_url="https://yes.nighty.works/raw/8VLDcg.webp"
|
||||
)
|
||||
await send_embed(context, embed, ephemeral=True)
|
||||
return
|
||||
|
||||
# Check if bot has send messages permission before starting processing
|
||||
try:
|
||||
test_embed = discord.Embed(title="Testing permissions...", color=0x7289DA)
|
||||
test_embed.set_author(name="Utility", icon_url="https://yes.nighty.works/raw/8VLDcg.webp")
|
||||
test_embed.set_author(
|
||||
name="Utility", icon_url="https://yes.nighty.works/raw/8VLDcg.webp"
|
||||
)
|
||||
await context.channel.send(embed=test_embed, delete_after=0.1)
|
||||
except discord.Forbidden:
|
||||
embed = discord.Embed(
|
||||
@@ -263,8 +288,10 @@ def codepreview_command():
|
||||
description="The bot needs the `send messages` permission to execute this command.",
|
||||
color=0xE02B2B,
|
||||
)
|
||||
embed.set_author(name="Utility", icon_url="https://yes.nighty.works/raw/8VLDcg.webp")
|
||||
|
||||
embed.set_author(
|
||||
name="Utility", icon_url="https://yes.nighty.works/raw/8VLDcg.webp"
|
||||
)
|
||||
|
||||
interaction = getattr(context, "interaction", None)
|
||||
if interaction is not None:
|
||||
if not interaction.response.is_done():
|
||||
@@ -274,135 +301,177 @@ def codepreview_command():
|
||||
else:
|
||||
await context.send(embed=embed, ephemeral=True)
|
||||
return
|
||||
|
||||
|
||||
parsed = parse_github_url(url)
|
||||
|
||||
|
||||
if not parsed:
|
||||
embed = discord.Embed(
|
||||
title="Error",
|
||||
description="Invalid GitHub URL format. Please provide a valid GitHub blob URL or PR URL.",
|
||||
color=0xE02B2B,
|
||||
).set_author(name="Utility", icon_url="https://yes.nighty.works/raw/8VLDcg.webp")
|
||||
).set_author(
|
||||
name="Utility", icon_url="https://yes.nighty.works/raw/8VLDcg.webp"
|
||||
)
|
||||
await send_embed(context, embed, ephemeral=True)
|
||||
return
|
||||
|
||||
if parsed.get('type') == 'pr':
|
||||
pr_info = await fetch_pr_info(parsed['owner'], parsed['repo'], parsed['pr_number'])
|
||||
diff_content = await fetch_pr_diff(parsed['owner'], parsed['repo'], parsed['pr_number'])
|
||||
|
||||
|
||||
if parsed.get("type") == "pr":
|
||||
pr_info = await fetch_pr_info(
|
||||
parsed["owner"], parsed["repo"], parsed["pr_number"]
|
||||
)
|
||||
diff_content = await fetch_pr_diff(
|
||||
parsed["owner"], parsed["repo"], parsed["pr_number"]
|
||||
)
|
||||
|
||||
if not pr_info or not diff_content:
|
||||
embed = discord.Embed(
|
||||
title="Error",
|
||||
description="Failed to fetch pull request information. The PR might not exist or be accessible.",
|
||||
color=0xE02B2B,
|
||||
).set_author(name="Utility", icon_url="https://yes.nighty.works/raw/8VLDcg.webp")
|
||||
).set_author(
|
||||
name="Utility", icon_url="https://yes.nighty.works/raw/8VLDcg.webp"
|
||||
)
|
||||
await send_embed(context, embed, ephemeral=True)
|
||||
return
|
||||
|
||||
|
||||
pr_url = f"https://github.com/{parsed['owner']}/{parsed['repo']}/pull/{parsed['pr_number']}"
|
||||
|
||||
if pr_info['merged']:
|
||||
pr_color = 0x6f42c1
|
||||
|
||||
if pr_info["merged"]:
|
||||
pr_color = 0x6F42C1
|
||||
pr_status = "Merged"
|
||||
elif pr_info['state'] == 'open':
|
||||
elif pr_info["state"] == "open":
|
||||
pr_color = 0x57F287
|
||||
pr_status = "Open"
|
||||
else:
|
||||
pr_color = 0xE02B2B
|
||||
pr_status = "Closed"
|
||||
|
||||
|
||||
embed = discord.Embed(
|
||||
title=f"Pull Request #{pr_info['number']}: {pr_info['title'][:100]}",
|
||||
description=f"**Repository:** [{parsed['owner']}/{parsed['repo']}]({pr_url})\n**Author:** {pr_info['user']}\n**Status:** {pr_status}",
|
||||
color=pr_color,
|
||||
)
|
||||
embed.set_author(name="Utility", icon_url="https://yes.nighty.works/raw/8VLDcg.webp")
|
||||
embed.add_field(name="Changes", value=f"**+{pr_info['additions']}** / **-{pr_info['deletions']}**", inline=True)
|
||||
embed.add_field(name="Files Changed", value=f"{pr_info['changed_files']}", inline=True)
|
||||
embed.add_field(name="Branches", value=f"`{pr_info['base_branch']}` ← `{pr_info['head_branch']}`", inline=False)
|
||||
embed.set_footer(text=f"Requested by {context.author.name}", icon_url=context.author.display_avatar.url)
|
||||
|
||||
embed.set_author(
|
||||
name="Utility", icon_url="https://yes.nighty.works/raw/8VLDcg.webp"
|
||||
)
|
||||
embed.add_field(
|
||||
name="Changes",
|
||||
value=f"**+{pr_info['additions']}** / **-{pr_info['deletions']}**",
|
||||
inline=True,
|
||||
)
|
||||
embed.add_field(
|
||||
name="Files Changed", value=f"{pr_info['changed_files']}", inline=True
|
||||
)
|
||||
embed.add_field(
|
||||
name="Branches",
|
||||
value=f"`{pr_info['base_branch']}` ← `{pr_info['head_branch']}`",
|
||||
inline=False,
|
||||
)
|
||||
embed.set_footer(
|
||||
text=f"Requested by {context.author.name}",
|
||||
icon_url=context.author.display_avatar.url,
|
||||
)
|
||||
|
||||
interaction = getattr(context, "interaction", None)
|
||||
if interaction is not None and not interaction.response.is_done():
|
||||
await interaction.response.defer(ephemeral=True)
|
||||
|
||||
|
||||
await context.channel.send(embed=embed)
|
||||
|
||||
|
||||
max_diff_length = 1900
|
||||
max_lines = 100
|
||||
|
||||
diff_lines = diff_content.split('\n')
|
||||
|
||||
|
||||
diff_lines = diff_content.split("\n")
|
||||
|
||||
if len(diff_lines) > max_lines:
|
||||
diff_lines = diff_lines[:max_lines]
|
||||
diff_lines.append(f"\n... ({len(diff_content.split(chr(10))) - max_lines} more lines omitted)")
|
||||
|
||||
diff_lines.append(
|
||||
f"\n... ({len(diff_content.split(chr(10))) - max_lines} more lines omitted)"
|
||||
)
|
||||
|
||||
current_chunk = ""
|
||||
|
||||
|
||||
for line in diff_lines:
|
||||
test_chunk = current_chunk + line + '\n'
|
||||
|
||||
test_chunk = current_chunk + line + "\n"
|
||||
|
||||
if len(test_chunk) + 10 > max_diff_length:
|
||||
if current_chunk.strip():
|
||||
remaining_lines = len(diff_lines) - len(current_chunk.split('\n'))
|
||||
remaining_lines = len(diff_lines) - len(
|
||||
current_chunk.split("\n")
|
||||
)
|
||||
if remaining_lines > 0:
|
||||
current_chunk += f"\n... ({remaining_lines} more lines omitted)"
|
||||
await context.channel.send(f"```diff\n{current_chunk.rstrip()}\n```")
|
||||
current_chunk += (
|
||||
f"\n... ({remaining_lines} more lines omitted)"
|
||||
)
|
||||
await context.channel.send(
|
||||
f"```diff\n{current_chunk.rstrip()}\n```"
|
||||
)
|
||||
break
|
||||
else:
|
||||
current_chunk = test_chunk
|
||||
else:
|
||||
if current_chunk.strip():
|
||||
await context.channel.send(f"```diff\n{current_chunk.rstrip()}\n```")
|
||||
|
||||
await context.channel.send(
|
||||
f"```diff\n{current_chunk.rstrip()}\n```"
|
||||
)
|
||||
|
||||
if interaction is not None:
|
||||
try:
|
||||
await interaction.delete_original_response()
|
||||
except:
|
||||
pass
|
||||
return
|
||||
|
||||
content = await fetch_github_content(parsed['raw_url'])
|
||||
|
||||
|
||||
content = await fetch_github_content(parsed["raw_url"])
|
||||
|
||||
if not content:
|
||||
embed = discord.Embed(
|
||||
title="Error",
|
||||
description="Failed to fetch content from GitHub. The file might not exist or be accessible.",
|
||||
color=0xE02B2B,
|
||||
).set_author(name="Utility", icon_url="https://yes.nighty.works/raw/8VLDcg.webp")
|
||||
).set_author(
|
||||
name="Utility", icon_url="https://yes.nighty.works/raw/8VLDcg.webp"
|
||||
)
|
||||
await send_embed(context, embed, ephemeral=True)
|
||||
return
|
||||
|
||||
if parsed['start_line']:
|
||||
code = extract_lines(content, parsed['start_line'], parsed['end_line'])
|
||||
|
||||
if parsed["start_line"]:
|
||||
code = extract_lines(content, parsed["start_line"], parsed["end_line"])
|
||||
line_info = f" (Lines {parsed['start_line']}"
|
||||
if parsed['end_line']:
|
||||
if parsed["end_line"]:
|
||||
line_info += f"-{parsed['end_line']}"
|
||||
line_info += ")"
|
||||
else:
|
||||
code = content
|
||||
line_info = ""
|
||||
|
||||
code_lines = code.split('\n')
|
||||
|
||||
code_lines = code.split("\n")
|
||||
if len(code_lines) > 100:
|
||||
code = '\n'.join(code_lines[:100])
|
||||
code = "\n".join(code_lines[:100])
|
||||
code += f"\n\n... ({len(code_lines) - 100} more lines omitted)"
|
||||
|
||||
filename = parsed['filepath'].split('/')[-1]
|
||||
|
||||
filename = parsed["filepath"].split("/")[-1]
|
||||
language = get_language_from_filename(filename)
|
||||
|
||||
|
||||
embed = discord.Embed(
|
||||
title="Code Preview",
|
||||
description=f"**Repository URL:** [{parsed['owner']}/{parsed['repo']}]({url})",
|
||||
color=0x7289DA,
|
||||
)
|
||||
embed.set_author(name="Utility", icon_url="https://yes.nighty.works/raw/8VLDcg.webp")
|
||||
embed.add_field(name="File", value=f"`{parsed['filepath']}`{line_info}", inline=True)
|
||||
embed.set_author(
|
||||
name="Utility", icon_url="https://yes.nighty.works/raw/8VLDcg.webp"
|
||||
)
|
||||
embed.add_field(
|
||||
name="File", value=f"`{parsed['filepath']}`{line_info}", inline=True
|
||||
)
|
||||
embed.add_field(name="Branch", value=f"`{parsed['branch']}`", inline=True)
|
||||
embed.set_footer(text=f"Requested by {context.author.name}", icon_url=context.author.display_avatar.url)
|
||||
|
||||
embed.set_footer(
|
||||
text=f"Requested by {context.author.name}",
|
||||
icon_url=context.author.display_avatar.url,
|
||||
)
|
||||
|
||||
code_block = f"```{language}\n{code}\n```"
|
||||
|
||||
|
||||
if len(code_block) > 1990:
|
||||
interaction = getattr(context, "interaction", None)
|
||||
if interaction is not None:
|
||||
@@ -411,19 +480,21 @@ def codepreview_command():
|
||||
await context.channel.send(embed=embed)
|
||||
else:
|
||||
await context.channel.send(embed=embed)
|
||||
|
||||
|
||||
max_code_length = 1980 - len(language) - 8
|
||||
code_lines = code.split('\n')
|
||||
code_lines = code.split("\n")
|
||||
current_chunk = []
|
||||
current_length = 0
|
||||
|
||||
|
||||
for line in code_lines:
|
||||
line_length = len(line) + 1
|
||||
if current_length + line_length > max_code_length:
|
||||
remaining_lines = len(code_lines) - len(current_chunk)
|
||||
if remaining_lines > 0:
|
||||
current_chunk.append(f"\n... ({remaining_lines} more lines omitted)")
|
||||
chunk_text = '\n'.join(current_chunk)
|
||||
current_chunk.append(
|
||||
f"\n... ({remaining_lines} more lines omitted)"
|
||||
)
|
||||
chunk_text = "\n".join(current_chunk)
|
||||
await context.channel.send(f"```{language}\n{chunk_text}\n```")
|
||||
break
|
||||
else:
|
||||
@@ -431,9 +502,9 @@ def codepreview_command():
|
||||
current_length += line_length
|
||||
else:
|
||||
if current_chunk:
|
||||
chunk_text = '\n'.join(current_chunk)
|
||||
chunk_text = "\n".join(current_chunk)
|
||||
await context.channel.send(f"```{language}\n{chunk_text}\n```")
|
||||
|
||||
|
||||
if interaction is not None:
|
||||
try:
|
||||
await interaction.delete_original_response()
|
||||
@@ -454,4 +525,4 @@ def codepreview_command():
|
||||
await context.channel.send(embed=embed)
|
||||
await context.channel.send(code_block)
|
||||
|
||||
return codepreview
|
||||
return codepreview
|
||||
|
||||
@@ -6,21 +6,24 @@ import aiohttp
|
||||
|
||||
|
||||
def dictionary_command():
|
||||
|
||||
async def send_embed(context, embed: discord.Embed, *, ephemeral: bool = False) -> None:
|
||||
async def send_embed(
|
||||
context, embed: discord.Embed, *, ephemeral: bool = False
|
||||
) -> None:
|
||||
interaction = getattr(context, "interaction", None)
|
||||
if interaction is not None:
|
||||
if interaction.response.is_done():
|
||||
await interaction.followup.send(embed=embed, ephemeral=ephemeral)
|
||||
else:
|
||||
await interaction.response.send_message(embed=embed, ephemeral=ephemeral)
|
||||
await interaction.response.send_message(
|
||||
embed=embed, ephemeral=ephemeral
|
||||
)
|
||||
else:
|
||||
await context.send(embed=embed)
|
||||
|
||||
async def fetch_definition(word: str) -> dict:
|
||||
try:
|
||||
url = f"https://api.dictionaryapi.dev/api/v2/entries/en/{word.lower()}"
|
||||
|
||||
|
||||
async with aiohttp.ClientSession() as session:
|
||||
async with session.get(url) as response:
|
||||
if response.status == 200:
|
||||
@@ -29,7 +32,10 @@ def dictionary_command():
|
||||
elif response.status == 404:
|
||||
return {"success": False, "error": "Word not found"}
|
||||
else:
|
||||
return {"success": False, "error": f"API returned status code {response.status}"}
|
||||
return {
|
||||
"success": False,
|
||||
"error": f"API returned status code {response.status}",
|
||||
}
|
||||
except aiohttp.ClientError:
|
||||
return {"success": False, "error": "Network error occurred"}
|
||||
except Exception as e:
|
||||
@@ -39,21 +45,26 @@ def dictionary_command():
|
||||
name="dictionary",
|
||||
description="Get the definition of a word",
|
||||
)
|
||||
@app_commands.describe(
|
||||
word="The word to look up"
|
||||
)
|
||||
@app_commands.describe(word="The word to look up")
|
||||
async def dictionary(self, context, word: str = None):
|
||||
if not word or not word.strip():
|
||||
if context.message and context.message.reference and context.message.reference.resolved:
|
||||
if (
|
||||
context.message
|
||||
and context.message.reference
|
||||
and context.message.reference.resolved
|
||||
):
|
||||
replied_message = context.message.reference.resolved
|
||||
if hasattr(replied_message, 'content') and replied_message.content:
|
||||
if hasattr(replied_message, "content") and replied_message.content:
|
||||
word = replied_message.content.strip().split()[0]
|
||||
else:
|
||||
embed = discord.Embed(
|
||||
title="Error",
|
||||
description="The replied message has no text content to look up.",
|
||||
color=0xE02B2B,
|
||||
).set_author(name="Utility", icon_url="https://yes.nighty.works/raw/8VLDcg.webp")
|
||||
).set_author(
|
||||
name="Utility",
|
||||
icon_url="https://yes.nighty.works/raw/8VLDcg.webp",
|
||||
)
|
||||
await send_embed(context, embed, ephemeral=True)
|
||||
return
|
||||
else:
|
||||
@@ -61,110 +72,125 @@ def dictionary_command():
|
||||
title="Error",
|
||||
description="Please provide a word to look up or reply to a message with a word.",
|
||||
color=0xE02B2B,
|
||||
).set_author(name="Utility", icon_url="https://yes.nighty.works/raw/8VLDcg.webp")
|
||||
).set_author(
|
||||
name="Utility", icon_url="https://yes.nighty.works/raw/8VLDcg.webp"
|
||||
)
|
||||
await send_embed(context, embed, ephemeral=True)
|
||||
return
|
||||
|
||||
|
||||
word = word.strip().split()[0]
|
||||
|
||||
|
||||
interaction = getattr(context, "interaction", None)
|
||||
if interaction is not None and not interaction.response.is_done():
|
||||
await interaction.response.defer()
|
||||
|
||||
|
||||
result = await fetch_definition(word)
|
||||
|
||||
|
||||
if not result["success"]:
|
||||
error_message = result.get("error", "Unknown error")
|
||||
|
||||
|
||||
if error_message == "Word not found":
|
||||
description = f"Could not find a definition for **{word}**.\nPlease check the spelling and try again."
|
||||
else:
|
||||
description = f"Failed to fetch definition: {error_message}"
|
||||
|
||||
|
||||
embed = discord.Embed(
|
||||
title="Error",
|
||||
description=description,
|
||||
color=0xE02B2B,
|
||||
).set_author(name="Utility", icon_url="https://yes.nighty.works/raw/8VLDcg.webp")
|
||||
).set_author(
|
||||
name="Utility", icon_url="https://yes.nighty.works/raw/8VLDcg.webp"
|
||||
)
|
||||
await send_embed(context, embed, ephemeral=True)
|
||||
return
|
||||
|
||||
|
||||
data = result["data"]
|
||||
|
||||
|
||||
if not data or len(data) == 0:
|
||||
embed = discord.Embed(
|
||||
title="Error",
|
||||
description=f"No definition found for **{word}**.",
|
||||
color=0xE02B2B,
|
||||
).set_author(name="Utility", icon_url="https://yes.nighty.works/raw/8VLDcg.webp")
|
||||
).set_author(
|
||||
name="Utility", icon_url="https://yes.nighty.works/raw/8VLDcg.webp"
|
||||
)
|
||||
await send_embed(context, embed, ephemeral=True)
|
||||
return
|
||||
|
||||
|
||||
entry = data[0]
|
||||
word_title = entry.get("word", word)
|
||||
phonetic = entry.get("phonetic", "")
|
||||
origin = entry.get("origin", "")
|
||||
meanings = entry.get("meanings", [])
|
||||
|
||||
|
||||
embed = discord.Embed(
|
||||
title=f"Dictionary",
|
||||
description=f"**```{word_title}```**",
|
||||
color=0x7289DA,
|
||||
)
|
||||
embed.set_author(name="Utilities", icon_url="https://yes.nighty.works/raw/8VLDcg.webp")
|
||||
|
||||
embed.set_author(
|
||||
name="Utilities", icon_url="https://yes.nighty.works/raw/8VLDcg.webp"
|
||||
)
|
||||
|
||||
if phonetic:
|
||||
embed.add_field(name="Pronunciation", value=f"`{phonetic}`", inline=False)
|
||||
|
||||
|
||||
max_meanings = 3
|
||||
for idx, meaning in enumerate(meanings[:max_meanings]):
|
||||
part_of_speech = meaning.get("partOfSpeech", "").capitalize()
|
||||
definitions = meaning.get("definitions", [])
|
||||
|
||||
|
||||
if definitions:
|
||||
def_text = ""
|
||||
examples = []
|
||||
|
||||
|
||||
for def_idx, definition in enumerate(definitions[:2], 1):
|
||||
def_line = definition.get("definition", "")
|
||||
example = definition.get("example", "")
|
||||
|
||||
|
||||
if def_line:
|
||||
def_text += f"{def_idx}. {def_line}\n"
|
||||
if example:
|
||||
examples.append(f"{def_idx}. {example}")
|
||||
|
||||
|
||||
if def_text:
|
||||
field_name = f"{part_of_speech}" if part_of_speech else f"Definition {idx + 1}"
|
||||
embed.add_field(name=field_name, value=def_text.strip(), inline=False)
|
||||
|
||||
field_name = (
|
||||
f"{part_of_speech}"
|
||||
if part_of_speech
|
||||
else f"Definition {idx + 1}"
|
||||
)
|
||||
embed.add_field(
|
||||
name=field_name, value=def_text.strip(), inline=False
|
||||
)
|
||||
|
||||
if examples:
|
||||
example_text = "\n".join(examples)
|
||||
embed.add_field(name="Examples", value=example_text, inline=False)
|
||||
|
||||
embed.add_field(
|
||||
name="Examples", value=example_text, inline=False
|
||||
)
|
||||
|
||||
if idx < len(meanings[:max_meanings]) - 1:
|
||||
embed.add_field(name="────────", value="", inline=False)
|
||||
|
||||
|
||||
if origin and len(origin) < 1000:
|
||||
embed.add_field(name="Origin", value=origin, inline=False)
|
||||
|
||||
|
||||
synonyms = []
|
||||
antonyms = []
|
||||
|
||||
|
||||
for meaning in meanings:
|
||||
for definition in meaning.get("definitions", []):
|
||||
synonyms.extend(definition.get("synonyms", []))
|
||||
antonyms.extend(definition.get("antonyms", []))
|
||||
|
||||
|
||||
if synonyms:
|
||||
synonym_text = ", ".join(synonyms[:10])
|
||||
embed.add_field(name="Synonyms", value=synonym_text, inline=True)
|
||||
|
||||
|
||||
if antonyms:
|
||||
antonym_text = ", ".join(antonyms[:10])
|
||||
embed.add_field(name="Antonyms", value=antonym_text, inline=True)
|
||||
|
||||
|
||||
await send_embed(context, embed)
|
||||
|
||||
return dictionary
|
||||
|
||||
@@ -10,164 +10,173 @@ import urllib.parse
|
||||
|
||||
|
||||
languages = {
|
||||
"auto": "Auto-detect",
|
||||
"en": "English",
|
||||
"es": "Spanish",
|
||||
"fr": "French",
|
||||
"de": "German",
|
||||
"it": "Italian",
|
||||
"pt": "Portuguese",
|
||||
"ru": "Russian",
|
||||
"ja": "Japanese",
|
||||
"ko": "Korean",
|
||||
"zh-CN": "Chinese (Simplified)",
|
||||
"zh-TW": "Chinese (Traditional)",
|
||||
"ar": "Arabic",
|
||||
"hi": "Hindi",
|
||||
"th": "Thai",
|
||||
"vi": "Vietnamese",
|
||||
"nl": "Dutch",
|
||||
"pl": "Polish",
|
||||
"tr": "Turkish",
|
||||
"sv": "Swedish",
|
||||
"da": "Danish",
|
||||
"no": "Norwegian",
|
||||
"fi": "Finnish",
|
||||
"cs": "Czech",
|
||||
"sk": "Slovak",
|
||||
"hu": "Hungarian",
|
||||
"ro": "Romanian",
|
||||
"bg": "Bulgarian",
|
||||
"hr": "Croatian",
|
||||
"sr": "Serbian",
|
||||
"sl": "Slovenian",
|
||||
"et": "Estonian",
|
||||
"lv": "Latvian",
|
||||
"lt": "Lithuanian",
|
||||
"uk": "Ukrainian",
|
||||
"be": "Belarusian",
|
||||
"mk": "Macedonian",
|
||||
"sq": "Albanian",
|
||||
"mt": "Maltese",
|
||||
"is": "Icelandic",
|
||||
"ga": "Irish",
|
||||
"cy": "Welsh",
|
||||
"gd": "Scots Gaelic",
|
||||
"eu": "Basque",
|
||||
"ca": "Catalan",
|
||||
"gl": "Galician",
|
||||
"eo": "Esperanto",
|
||||
"la": "Latin",
|
||||
"af": "Afrikaans",
|
||||
"sw": "Swahili",
|
||||
"zu": "Zulu",
|
||||
"xh": "Xhosa",
|
||||
"yo": "Yoruba",
|
||||
"ig": "Igbo",
|
||||
"ha": "Hausa",
|
||||
"am": "Amharic",
|
||||
"om": "Oromo",
|
||||
"ti": "Tigrinya",
|
||||
"so": "Somali",
|
||||
"rw": "Kinyarwanda",
|
||||
"lg": "Ganda",
|
||||
"ny": "Chichewa",
|
||||
"sn": "Shona",
|
||||
"st": "Sesotho",
|
||||
"tn": "Tswana",
|
||||
"ts": "Tsonga",
|
||||
"ss": "Swati",
|
||||
"nr": "Ndebele",
|
||||
"nso": "Northern Sotho",
|
||||
"ve": "Venda",
|
||||
"bn": "Bengali",
|
||||
"gu": "Gujarati",
|
||||
"kn": "Kannada",
|
||||
"ml": "Malayalam",
|
||||
"mr": "Marathi",
|
||||
"ne": "Nepali",
|
||||
"or": "Odia",
|
||||
"pa": "Punjabi",
|
||||
"si": "Sinhala",
|
||||
"ta": "Tamil",
|
||||
"te": "Telugu",
|
||||
"ur": "Urdu",
|
||||
"as": "Assamese",
|
||||
"bho": "Bhojpuri",
|
||||
"doi": "Dogri",
|
||||
"gom": "Konkani",
|
||||
"mai": "Maithili",
|
||||
"mni-Mtei": "Meiteilon",
|
||||
"sa": "Sanskrit",
|
||||
"id": "Indonesian",
|
||||
"ms": "Malay",
|
||||
"tl": "Filipino",
|
||||
"jv": "Javanese",
|
||||
"su": "Sundanese",
|
||||
"ceb": "Cebuano",
|
||||
"hil": "Hiligaynon",
|
||||
"ilo": "Iloko",
|
||||
"pam": "Kapampangan",
|
||||
"war": "Waray",
|
||||
"my": "Myanmar",
|
||||
"km": "Khmer",
|
||||
"lo": "Lao",
|
||||
"ka": "Georgian",
|
||||
"hy": "Armenian",
|
||||
"az": "Azerbaijani",
|
||||
"kk": "Kazakh",
|
||||
"ky": "Kyrgyz",
|
||||
"mn": "Mongolian",
|
||||
"tk": "Turkmen",
|
||||
"ug": "Uyghur",
|
||||
"uz": "Uzbek",
|
||||
"tg": "Tajik",
|
||||
"fa": "Persian",
|
||||
"ps": "Pashto",
|
||||
"sd": "Sindhi",
|
||||
"he": "Hebrew",
|
||||
"yi": "Yiddish",
|
||||
"iw": "Hebrew",
|
||||
"el": "Greek",
|
||||
"lt": "Lithuanian",
|
||||
"lv": "Latvian",
|
||||
}
|
||||
"auto": "Auto-detect",
|
||||
"en": "English",
|
||||
"es": "Spanish",
|
||||
"fr": "French",
|
||||
"de": "German",
|
||||
"it": "Italian",
|
||||
"pt": "Portuguese",
|
||||
"ru": "Russian",
|
||||
"ja": "Japanese",
|
||||
"ko": "Korean",
|
||||
"zh-CN": "Chinese (Simplified)",
|
||||
"zh-TW": "Chinese (Traditional)",
|
||||
"ar": "Arabic",
|
||||
"hi": "Hindi",
|
||||
"th": "Thai",
|
||||
"vi": "Vietnamese",
|
||||
"nl": "Dutch",
|
||||
"pl": "Polish",
|
||||
"tr": "Turkish",
|
||||
"sv": "Swedish",
|
||||
"da": "Danish",
|
||||
"no": "Norwegian",
|
||||
"fi": "Finnish",
|
||||
"cs": "Czech",
|
||||
"sk": "Slovak",
|
||||
"hu": "Hungarian",
|
||||
"ro": "Romanian",
|
||||
"bg": "Bulgarian",
|
||||
"hr": "Croatian",
|
||||
"sr": "Serbian",
|
||||
"sl": "Slovenian",
|
||||
"et": "Estonian",
|
||||
"lv": "Latvian",
|
||||
"lt": "Lithuanian",
|
||||
"uk": "Ukrainian",
|
||||
"be": "Belarusian",
|
||||
"mk": "Macedonian",
|
||||
"sq": "Albanian",
|
||||
"mt": "Maltese",
|
||||
"is": "Icelandic",
|
||||
"ga": "Irish",
|
||||
"cy": "Welsh",
|
||||
"gd": "Scots Gaelic",
|
||||
"eu": "Basque",
|
||||
"ca": "Catalan",
|
||||
"gl": "Galician",
|
||||
"eo": "Esperanto",
|
||||
"la": "Latin",
|
||||
"af": "Afrikaans",
|
||||
"sw": "Swahili",
|
||||
"zu": "Zulu",
|
||||
"xh": "Xhosa",
|
||||
"yo": "Yoruba",
|
||||
"ig": "Igbo",
|
||||
"ha": "Hausa",
|
||||
"am": "Amharic",
|
||||
"om": "Oromo",
|
||||
"ti": "Tigrinya",
|
||||
"so": "Somali",
|
||||
"rw": "Kinyarwanda",
|
||||
"lg": "Ganda",
|
||||
"ny": "Chichewa",
|
||||
"sn": "Shona",
|
||||
"st": "Sesotho",
|
||||
"tn": "Tswana",
|
||||
"ts": "Tsonga",
|
||||
"ss": "Swati",
|
||||
"nr": "Ndebele",
|
||||
"nso": "Northern Sotho",
|
||||
"ve": "Venda",
|
||||
"bn": "Bengali",
|
||||
"gu": "Gujarati",
|
||||
"kn": "Kannada",
|
||||
"ml": "Malayalam",
|
||||
"mr": "Marathi",
|
||||
"ne": "Nepali",
|
||||
"or": "Odia",
|
||||
"pa": "Punjabi",
|
||||
"si": "Sinhala",
|
||||
"ta": "Tamil",
|
||||
"te": "Telugu",
|
||||
"ur": "Urdu",
|
||||
"as": "Assamese",
|
||||
"bho": "Bhojpuri",
|
||||
"doi": "Dogri",
|
||||
"gom": "Konkani",
|
||||
"mai": "Maithili",
|
||||
"mni-Mtei": "Meiteilon",
|
||||
"sa": "Sanskrit",
|
||||
"id": "Indonesian",
|
||||
"ms": "Malay",
|
||||
"tl": "Filipino",
|
||||
"jv": "Javanese",
|
||||
"su": "Sundanese",
|
||||
"ceb": "Cebuano",
|
||||
"hil": "Hiligaynon",
|
||||
"ilo": "Iloko",
|
||||
"pam": "Kapampangan",
|
||||
"war": "Waray",
|
||||
"my": "Myanmar",
|
||||
"km": "Khmer",
|
||||
"lo": "Lao",
|
||||
"ka": "Georgian",
|
||||
"hy": "Armenian",
|
||||
"az": "Azerbaijani",
|
||||
"kk": "Kazakh",
|
||||
"ky": "Kyrgyz",
|
||||
"mn": "Mongolian",
|
||||
"tk": "Turkmen",
|
||||
"ug": "Uyghur",
|
||||
"uz": "Uzbek",
|
||||
"tg": "Tajik",
|
||||
"fa": "Persian",
|
||||
"ps": "Pashto",
|
||||
"sd": "Sindhi",
|
||||
"he": "Hebrew",
|
||||
"yi": "Yiddish",
|
||||
"iw": "Hebrew",
|
||||
"el": "Greek",
|
||||
"lt": "Lithuanian",
|
||||
"lv": "Latvian",
|
||||
}
|
||||
|
||||
async def language_autocomplete(interaction: discord.Interaction, current: str) -> list[app_commands.Choice[str]]:
|
||||
|
||||
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:
|
||||
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:
|
||||
if interaction.response.is_done():
|
||||
await interaction.followup.send(embed=embed, ephemeral=ephemeral)
|
||||
else:
|
||||
await interaction.response.send_message(embed=embed, ephemeral=ephemeral)
|
||||
await interaction.response.send_message(
|
||||
embed=embed, ephemeral=ephemeral
|
||||
)
|
||||
else:
|
||||
await context.send(embed=embed)
|
||||
|
||||
async def _translate_with_google_web(text: str, from_lang: str = "auto", to_lang: str = "en") -> dict:
|
||||
async def _translate_with_google_web(
|
||||
text: str, from_lang: str = "auto", to_lang: str = "en"
|
||||
) -> dict:
|
||||
try:
|
||||
base_url = "https://translate.googleapis.com/translate_a/single"
|
||||
|
||||
@@ -208,9 +217,11 @@ def translate_command():
|
||||
"sl": from_lang,
|
||||
"tl": to_lang,
|
||||
"dt": ["t", "bd"],
|
||||
"q": chunk
|
||||
"q": chunk,
|
||||
}
|
||||
async with session.get(base_url, headers=headers, params=params) as response:
|
||||
async with session.get(
|
||||
base_url, headers=headers, params=params
|
||||
) as response:
|
||||
if response.status != 200:
|
||||
return None
|
||||
result_text = (await response.text()).strip()
|
||||
@@ -232,12 +243,11 @@ def translate_command():
|
||||
|
||||
return {
|
||||
"translatedText": "".join(translated_parts).strip(),
|
||||
"detectedSourceLanguage": detected_lang_overall
|
||||
"detectedSourceLanguage": detected_lang_overall,
|
||||
}
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
|
||||
@commands.hybrid_command(
|
||||
name="translate",
|
||||
description="Translate text to another language",
|
||||
@@ -245,22 +255,31 @@ def translate_command():
|
||||
@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)"
|
||||
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):
|
||||
async def translate(
|
||||
self, context, text: str = None, to_lang: str = "en", from_lang: str = None
|
||||
):
|
||||
if not text or not text.strip():
|
||||
if context.message and context.message.reference and context.message.reference.resolved:
|
||||
if (
|
||||
context.message
|
||||
and context.message.reference
|
||||
and context.message.reference.resolved
|
||||
):
|
||||
replied_message = context.message.reference.resolved
|
||||
if hasattr(replied_message, 'content') and replied_message.content:
|
||||
if hasattr(replied_message, "content") and replied_message.content:
|
||||
text = replied_message.content
|
||||
else:
|
||||
embed = discord.Embed(
|
||||
title="Error",
|
||||
description="The replied message has no text content to translate.",
|
||||
color=0xE02B2B,
|
||||
).set_author(name="Utility", icon_url="https://yes.nighty.works/raw/8VLDcg.webp")
|
||||
).set_author(
|
||||
name="Utility",
|
||||
icon_url="https://yes.nighty.works/raw/8VLDcg.webp",
|
||||
)
|
||||
await send_embed(context, embed, ephemeral=True)
|
||||
return
|
||||
else:
|
||||
@@ -268,55 +287,61 @@ def translate_command():
|
||||
title="Error",
|
||||
description="Please provide text to translate or reply to a message with text.",
|
||||
color=0xE02B2B,
|
||||
).set_author(name="Utility", icon_url="https://yes.nighty.works/raw/8VLDcg.webp")
|
||||
).set_author(
|
||||
name="Utility", icon_url="https://yes.nighty.works/raw/8VLDcg.webp"
|
||||
)
|
||||
await send_embed(context, embed, ephemeral=True)
|
||||
return
|
||||
|
||||
|
||||
if to_lang not in languages:
|
||||
embed = discord.Embed(
|
||||
title="Error",
|
||||
description=f"Invalid target language code: `{to_lang}`. Use the autocomplete feature to see available languages.",
|
||||
color=0xE02B2B,
|
||||
).set_author(name="Utility", icon_url="https://yes.nighty.works/raw/8VLDcg.webp")
|
||||
).set_author(
|
||||
name="Utility", icon_url="https://yes.nighty.works/raw/8VLDcg.webp"
|
||||
)
|
||||
await send_embed(context, embed, ephemeral=True)
|
||||
return
|
||||
|
||||
|
||||
if from_lang and from_lang not in languages:
|
||||
embed = discord.Embed(
|
||||
title="Error",
|
||||
description=f"Invalid source language code: `{from_lang}`. Use the autocomplete feature to see available languages.",
|
||||
color=0xE02B2B,
|
||||
).set_author(name="Utility", icon_url="https://yes.nighty.works/raw/8VLDcg.webp")
|
||||
).set_author(
|
||||
name="Utility", icon_url="https://yes.nighty.works/raw/8VLDcg.webp"
|
||||
)
|
||||
await send_embed(context, embed, ephemeral=True)
|
||||
return
|
||||
|
||||
|
||||
result = await _translate_with_google_web(text, from_lang or "auto", to_lang)
|
||||
|
||||
|
||||
if result and result.get("translatedText"):
|
||||
detected_lang = result.get("detectedSourceLanguage", from_lang or "auto")
|
||||
|
||||
|
||||
from_lang_name = languages.get(detected_lang, detected_lang)
|
||||
to_lang_name = languages.get(to_lang, to_lang)
|
||||
|
||||
|
||||
embed = discord.Embed(
|
||||
title="Translation",
|
||||
description=f"**Original:** {text}\n\n**Translated:** {result['translatedText']}",
|
||||
color=0x7289DA,
|
||||
)
|
||||
embed.set_author(name="Utility", icon_url="https://yes.nighty.works/raw/8VLDcg.webp")
|
||||
embed.set_author(
|
||||
name="Utility", icon_url="https://yes.nighty.works/raw/8VLDcg.webp"
|
||||
)
|
||||
embed.set_footer(text=f"{from_lang_name} » {to_lang_name}")
|
||||
|
||||
|
||||
await send_embed(context, embed)
|
||||
else:
|
||||
embed = discord.Embed(
|
||||
title="Error",
|
||||
description="Translation failed. Please try again later.",
|
||||
color=0xE02B2B,
|
||||
).set_author(name="Utility", icon_url="https://yes.nighty.works/raw/8VLDcg.webp")
|
||||
).set_author(
|
||||
name="Utility", icon_url="https://yes.nighty.works/raw/8VLDcg.webp"
|
||||
)
|
||||
await send_embed(context, embed, ephemeral=True)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return translate
|
||||
return translate
|
||||
|
||||
Reference in New Issue
Block a user