(msg: Api.Message)
| 205 | cmdHandlers: Record<string, (msg: Api.Message) => Promise<void>> = { |
| 206 | calc: async (msg: Api.Message) => await this.handleCalc(msg), |
| 207 | }; |
| 208 | |
| 209 | private async handleCalc(msg: Api.Message): Promise<void> { |
| 210 | try { |
| 211 | const rawText = (msg.message ?? msg.text ?? "").trim(); |
| 212 | const parts = rawText.split(/\s+/); |
| 213 | const [, ...args] = parts; |
| 214 | |
| 215 | if (!args.length) { |
| 216 | await msg.edit({ |
| 217 | text: help_text, |
| 218 | parseMode: "html", |
| 219 | linkPreview: false, |
| 220 | }); |
| 221 | return; |
| 222 | } |
| 223 | |
| 224 | const expression = args.join(" "); |
| 225 | |
| 226 | if (expression.length > MAX_EXPR_LENGTH) { |
| 227 | await msg.edit({ |
| 228 | text: `❌ <b>表达式过长</b><br/><br/>最大长度: ${MAX_EXPR_LENGTH} 字符<br/>当前长度: ${expression.length}`, |
| 229 | parseMode: "html", |
| 230 | }); |
| 231 | return; |
| 232 | } |
| 233 | |
| 234 | let result: number; |
| 235 | try { |
| 236 | result = SafeMathParser.calculate(expression); |
| 237 | } catch (error: any) { |
| 238 | await msg.edit({ |
| 239 | text: `🚫 <b>计算失败</b><br/><br/>表达式: <code>${htmlEscape(expression)}</code><br/>错误: ${htmlEscape(error?.message ?? "未知错误")}`, |
| 240 | parseMode: "html", |
| 241 | }); |
| 242 | return; |
| 243 | } |
| 244 | |
| 245 | if (!Number.isFinite(result)) { |
| 246 | await msg.edit({ |
| 247 | text: `🚫 <b>计算结果无效</b><br/><br/>表达式: <code>${htmlEscape(expression)}</code>`, |
| 248 | parseMode: "html", |
| 249 | }); |
| 250 | return; |
| 251 | } |
| 252 | |
| 253 | const formatted = this.formatResult(result); |
| 254 | |
| 255 | await msg.edit({ |
| 256 | text: `🧮 <b>计算结果</b><br/><br/><code>${htmlEscape(expression)}</code><br/>= <b>${formatted}</b>`, |
| 257 | parseMode: "html", |
| 258 | linkPreview: false, |
| 259 | }); |
| 260 | } catch (error: any) { |
| 261 | await msg.edit({ |
| 262 | text: `❌ <b>插件错误</b><br/><br/>${htmlEscape(error?.message ?? "未知错误")}`, |
| 263 | parseMode: "html", |
| 264 | }); |
no test coverage detected