(msg: Api.Message)
| 12 | const translateModule = await import("@vitalets/google-translate-api"); |
| 13 | translate = translateModule.translate || translateModule.default; |
| 14 | |
| 15 | if (!translate || typeof translate !== "function") { |
| 16 | await msg.edit({ |
| 17 | text: "❌ 翻译服务未正确加载,请检查网络连接或重启程序", |
| 18 | parseMode: "html", |
| 19 | }); |
| 20 | return; |
| 21 | } |
| 22 | } catch (importError: any) { |
| 23 | console.error("Failed to import translation service:", importError); |
| 24 | await msg.edit({ |
| 25 | text: `❌ <b>翻译服务加载失败:</b> ${htmlEscape(String(importError.message || importError))}`, |
| 26 | parseMode: "html", |
| 27 | }); |
| 28 | return; |
| 29 | } |
| 30 | |
| 31 | try { |
| 32 | const args = msg.message.split(" ").slice(1); // Remove command part |
| 33 | let text = ""; |
| 34 | let target = "zh-CN"; |
| 35 | |
| 36 | // Check for help command |
| 37 | if (args.length > 0 && ["h", "help"].includes(args[0].toLowerCase())) { |
| 38 | await msg.edit({ |
| 39 | text: `📘 <b>使用说明:</b> |
| 40 | |
| 41 | <b>基本用法:</b> |
| 42 | • <code>gt [文本]</code> - 翻译为中文(默认) |
| 43 | • <code>gt en [文本]</code> - 翻译为英文 |
| 44 | |
| 45 | <b>回复消息翻译:</b> |
| 46 | • <code>gt</code> 或 <code>gt en</code> |
| 47 | |
| 48 | <b>示例:</b> |
| 49 | 1. <code>gt Hello world</code> |
| 50 | 2. <code>gt en 你好,世界</code> |
| 51 | 3. 回复英文消息后 <code>gt</code>`, |
| 52 | parseMode: "html", |
| 53 | }); |
| 54 | return; |
| 55 | } |
| 56 | |
| 57 | // Check if first argument is "en" for English translation |
| 58 | if (args.length > 0 && args[0].toLowerCase() === "en") { |
| 59 | target = "en"; |
| 60 | text = args.slice(1).join(" "); |
| 61 | } else { |
| 62 | text = args.join(" "); |
| 63 | } |
| 64 | |
| 65 | // If no text provided, try to get from replied message |
| 66 | if (!text.trim()) { |
| 67 | try { |
| 68 | const reply = await safeGetReplyMessage(msg); |
| 69 | if (reply && reply.text) { |
| 70 | text = reply.text.trim(); |
| 71 | } else { |
nothing calls this directly
no test coverage detected