* 处理diss命令
(msg: Api.Message)
| 22 | |
| 23 | /** |
| 24 | * 处理diss命令 |
| 25 | */ |
| 26 | private async handleDiss(msg: Api.Message): Promise<void> { |
| 27 | try { |
| 28 | // 发送等待消息 |
| 29 | await msg.edit({ text: "🔄 正在获取儒雅随和语录..." }); |
| 30 | |
| 31 | // 尝试最多5次请求 |
| 32 | for (let attempt = 1; attempt <= 5; attempt++) { |
| 33 | try { |
| 34 | const response = await axios.get("https://api.oddfar.com/yl/q.php?c=1009&encode=text", { |
| 35 | timeout: 10000, // 10秒超时 |
| 36 | headers: { |
| 37 | 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36' |
| 38 | } |
| 39 | }); |
| 40 | |
| 41 | if (response.status === 200 && response.data) { |
| 42 | const dissText = response.data.toString().trim(); |
| 43 | |
| 44 | if (dissText && dissText.length > 0) { |
| 45 | // 成功获取到语录,发送结果 |
| 46 | await msg.edit({ |
| 47 | text: `${htmlEscape(dissText)}`, |
| 48 | parseMode: "html" |
| 49 | }); |
| 50 | return; |
| 51 | } |
| 52 | } |
| 53 | } catch (error) { |
| 54 | const errorMessage = error instanceof Error ? error.message : String(error); |
| 55 | console.warn(`[diss] 第${attempt}次尝试失败:`, errorMessage); |
| 56 | |
| 57 | // 如果不是最后一次尝试,等待一下再重试 |
| 58 | if (attempt < 5) { |
| 59 | await new Promise(resolve => setTimeout(resolve, 1000)); |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | // 所有尝试都失败了 |
| 65 | await msg.edit({ |
| 66 | text: "❌ 出错了呜呜呜 ~ 试了好多好多次都无法访问到 API 服务器。", |
| 67 | parseMode: "html" |
| 68 | }); |
| 69 | |
| 70 | } catch (error: any) { |
| 71 | // 处理意外错误 |
| 72 | console.error('[diss] 插件执行错误:', error); |
| 73 | await msg.edit({ |
| 74 | text: `❌ 发生意外错误: ${htmlEscape(error.message || "未知错误")}`, |
| 75 | parseMode: "html" |
| 76 | }); |
| 77 | } |
| 78 | } |
| 79 |
nothing calls this directly
no test coverage detected