Commit 13b7b71

benny-dou <60535774+benny-dou@users.noreply.github.com>
2025-07-11 10:44:41
feat(chinese): add Traditional Chinese <-> Simplified Chinese conversion
1 parent d3a1e01
src/others/convert_chinese.py
@@ -0,0 +1,33 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+import zhconv
+from pyrogram.client import Client
+from pyrogram.types import Message
+
+from config import PREFIX
+from messages.sender import send2tg
+from messages.utils import equal_prefix, startswith_prefix
+
+HELP = f"""🔤**简繁转换**
+`{PREFIX.CONVERT_TO_SC}` + 文字 或 消息: 转换为简体
+`{PREFIX.CONVERT_TO_TC}` + 文字 或 消息: 转换为繁体
+"""
+
+
+async def chinese_conversion(client: Client, message: Message, **kwargs):
+    """Traditional Chinese <-> Simplified Chinese."""
+    # send docs, without reply
+    if equal_prefix(message.content, prefix=[PREFIX.CONVERT_TO_SC, PREFIX.CONVERT_TO_TC]) and not message.reply_to_message:
+        await send2tg(client, message, texts=HELP, **kwargs)
+        return
+    if not startswith_prefix(message.content, prefix=[PREFIX.CONVERT_TO_SC, PREFIX.CONVERT_TO_TC]):
+        return
+    if startswith_prefix(message.content, prefix=PREFIX.CONVERT_TO_SC):
+        texts = message.reply_to_message.content if message.reply_to_message else message.content.removeprefix(PREFIX.CONVERT_TO_SC).lstrip()
+        simplified_chinese = zhconv.convert(texts, "zh-cn")
+        await message.reply(f"🔄繁体转简体:\n{simplified_chinese}", quote=True)
+    if startswith_prefix(message.content, prefix=PREFIX.CONVERT_TO_TC):
+        texts = message.reply_to_message.content if message.reply_to_message else message.content.removeprefix(PREFIX.CONVERT_TO_TC).lstrip()
+        traditional_chinese = zhconv.convert(texts, "zh-tw")
+        await message.reply(f"🔄简体转繁体:\n{traditional_chinese}", quote=True)
src/config.py
@@ -65,6 +65,7 @@ class ENABLE:  # see fine-grained permission in `src/permission.py`
     QUERY_DANMU = os.getenv("ENABLE_QUERY_DANMU", "1").lower() in ["1", "y", "yes", "t", "true", "on"]
     FAVORITE = os.getenv("ENABLE_FAVORITE", "1").lower() in ["1", "y", "yes", "t", "true", "on"]
     TTS = os.getenv("ENABLE_TTS", "1").lower() in ["1", "y", "yes", "t", "true", "on"]
+    CONVERT_CHINESE = os.getenv("ENABLE_CONVERT_CHINESE", "1").lower() in ["1", "y", "yes", "t", "true", "on"]
 
 
 class PREFIX:
@@ -89,6 +90,8 @@ class PREFIX:
     FAYAN = os.getenv("PREFIX_FAYAN", "/fa").lower()
     HISTORY = "/history, /hist"
     TTS = os.getenv("PREFIX_TTS", "/tts").lower()
+    CONVERT_TO_TC = os.getenv("PREFIX_CONVERT_TO_TC", "/tc, /tw").lower()
+    CONVERT_TO_SC = os.getenv("PREFIX_CONVERT_TO_SC", "/sc, /cn").lower()
 
 
 class API:
src/handler.py
@@ -18,6 +18,7 @@ from messages.parser import parse_msg
 from messages.sender import send2tg
 from messages.utils import equal_prefix, startswith_prefix
 from networking import match_social_media_link
+from others.convert_chinese import chinese_conversion
 from others.download_external import download_url_in_message
 from others.extract_audio import extract_audio_file
 from others.favorite import save_favorite, send_favorite
@@ -58,6 +59,7 @@ async def handle_utilities(
     subtitle: bool = True,
     summary: bool = True,
     tts: bool = True,
+    convert_chinese: bool = True,
     wget: bool = True,
     ytb: bool = True,
     raw_img: bool = True,
@@ -84,6 +86,7 @@ async def handle_utilities(
         history (bool, optional): Enable History Search. Defaults to True.
         subtitle (bool, optional): Enable YouTube subtitle. Defaults to True.
         tts (bool, optional): Enable TTS. Defaults to True.
+        convert_chinese (bool, optional): Enable Traditional Chinese <-> Simplified Chinese. Defaults to True.
         wget (bool, optional): Enable WGET. Defaults to True.
         ocr (bool, optional): Enable OCR. Defaults to True.
         price (bool, optional): Enable Asset price. Defaults to True.
@@ -124,6 +127,8 @@ async def handle_utilities(
         await send_favorite(client, message, **kwargs)  # /fav
     if tts:
         await text_to_speech(client, message, **kwargs)  # /tts
+    if convert_chinese:
+        await chinese_conversion(client, message, **kwargs)  # /sc
     if raw_img:
         await convert_raw_img_file(client, message, **kwargs)
 
@@ -190,6 +195,8 @@ async def handle_social_media(
         PREFIX.WGET,
         PREFIX.FAYAN,
         PREFIX.TTS,
+        PREFIX.CONVERT_TO_SC,
+        PREFIX.CONVERT_TO_TC,
         FAVORITE.SAVE_PREFIX,
         FAVORITE.SEND_PREFIX,
     ]
@@ -356,6 +363,8 @@ def get_social_media_help(chat_id: int | str, ctype: str, prefix: str):
         msg += f"\n🔍**搜索Google**: `{PREFIX.SEARCH_GOOGLE}` + 关键词"
     if permission["danmu"]:
         msg += f"\n📖**查询直播合订本**: 发送 `{PREFIX.DANMU}`, `{PREFIX.FAYAN}` 查看详细教程"
+    if permission["convert_chinese"]:
+        msg += f"\n🔄**简繁转换**: `{PREFIX.CONVERT_TO_SC}` 或 `{PREFIX.CONVERT_TO_TC}`"
 
     msg += "\n\n单独发送每个命令前缀本身可查看该命令详细使用说明"
     return msg
src/permission.py
@@ -121,6 +121,7 @@ def check_service(cid: int | str, ctype: str) -> dict:
         "ytdlp": True,
         "history": True,
         "favorite": True,
+        "convert_chinese": True,
     }
 
     if ctype == "PRIVATE":
@@ -174,6 +175,8 @@ def check_service(cid: int | str, ctype: str) -> dict:
         permission["favorite"] = False
     if not ENABLE.TTS:
         permission["tts"] = False
+    if not ENABLE.CONVERT_CHINESE:
+        permission["convert_chinese"] = False
 
     """
     Set specific service