(chatId: number, text: string, msgs: Array<{ userId: number; text: string; time: number }>)
| 340 | if (msg.text === text) { |
| 341 | senders.add(msg.userId); |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | // 条件:至少配置的人数在配置的时间内发送 |
| 346 | if (senders.size >= this.triggerConfig.minUsers) { |
| 347 | // 检查今日是否已复读 |
| 348 | if (!this.dailyHistory.has(chatId)) { |
| 349 | this.dailyHistory.set(chatId, new Set()); |
| 350 | } |
| 351 | |
| 352 | // 简单哈希(或直接用文本,如果文本不太长) |
| 353 | const contentKey = text.length > 50 ? text.substring(0, 50) + text.length : text; |
| 354 | |
| 355 | if (!this.dailyHistory.get(chatId)?.has(contentKey)) { |
| 356 | // [关键修改] 先标记为已复读,防止并发重复 |
| 357 | this.dailyHistory.get(chatId)?.add(contentKey); |
| 358 | await this.saveDailyHistory(); |
| 359 | |
| 360 | // 执行复读 |
| 361 | const client = await getGlobalClient(); |
| 362 | if (client) { |
| 363 | try { |
| 364 | await client.sendMessage(chatId, { message: text }); |
| 365 | console.log(`[AutoRepeat] Group ${chatId} repeated: ${contentKey}`); |
| 366 | } catch (e) { |
| 367 | // 发送失败则移除标记(可选,视需求而定,为了防刷通常不移除) |
| 368 | console.error(`[AutoRepeat] Failed to send: ${e}`); |
| 369 | } |
| 370 | } |
| 371 | } |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | private static async saveDailyHistory() { |
| 376 | const historyObj: Record<string, string[]> = {}; |
| 377 | for (const [gid, set] of this.dailyHistory) { |
| 378 | historyObj[gid] = Array.from(set); |
| 379 | } |
| 380 | await this.cache.saveData({ |
| 381 | daily_history: historyObj, |
| 382 | last_day_check: this.lastDayCheck |
no test coverage detected