| 34 | description: string = `编码解码工具插件\n\n${help_text}`; |
| 35 | |
| 36 | cmdHandlers: Record<string, (msg: Api.Message) => Promise<void>> = { |
| 37 | // 显示帮助信息 |
| 38 | encode: async (msg: Api.Message) => { |
| 39 | await msg.edit({ text: help_text, parseMode: "html" }); |
| 40 | }, |
| 41 | |
| 42 | // Base64 编码 |
| 43 | b64encode: async (msg: Api.Message) => { |
| 44 | await this.processEncoding(msg, "Base64", "🔐", "encode", { |
| 45 | encode: (text: string) => Buffer.from(text, 'utf8').toString('base64'), |
| 46 | decode: () => { throw new Error("不支持的操作"); } |
| 47 | }); |
| 48 | }, |
| 49 | |
| 50 | // Base64 解码 |
| 51 | b64decode: async (msg: Api.Message) => { |
| 52 | await this.processEncoding(msg, "Base64", "🔐", "decode", { |
| 53 | encode: () => { throw new Error("不支持的操作"); }, |
| 54 | decode: (text: string) => { |
| 55 | try { |
| 56 | const result = Buffer.from(text, 'base64').toString('utf8'); |
| 57 | if (!result || result.includes('\uFFFD')) { |
| 58 | throw new Error("无效的 Base64 字符串"); |
| 59 | } |
| 60 | return result; |
| 61 | } catch { |
| 62 | throw new Error("无效的 Base64 字符串,请检查输入"); |
| 63 | } |
| 64 | } |
| 65 | }); |
| 66 | }, |
| 67 | |
| 68 | // URL 编码 |
| 69 | urlencode: async (msg: Api.Message) => { |
| 70 | await this.processEncoding(msg, "URL", "🌐", "encode", { |
| 71 | encode: (text: string) => encodeURIComponent(text), |
| 72 | decode: () => { throw new Error("不支持的操作"); } |
| 73 | }); |
| 74 | }, |
| 75 | |
| 76 | // URL 解码 |
| 77 | urldecode: async (msg: Api.Message) => { |
| 78 | await this.processEncoding(msg, "URL", "🌐", "decode", { |
| 79 | encode: () => { throw new Error("不支持的操作"); }, |
| 80 | decode: (text: string) => { |
| 81 | try { |
| 82 | return decodeURIComponent(text); |
| 83 | } catch { |
| 84 | throw new Error("无效的 URL 编码字符串,请检查输入"); |
| 85 | } |
| 86 | } |
| 87 | }); |
| 88 | } |
| 89 | }; |
| 90 | |
| 91 | // 统一的编码处理逻辑 |
| 92 | private async processEncoding( |
| 93 | msg: Api.Message, |
nothing calls this directly
no test coverage detected