(client: any)
| 128 | |
| 129 | const sticker = await this.getHourSticker(); |
| 130 | if (!sticker) { |
| 131 | console.error(`[${this.PLUGIN_NAME}] 无法获取贴纸`); |
| 132 | return; |
| 133 | } |
| 134 | |
| 135 | const subscriptions = this.db.data.subscriptions; |
| 136 | |
| 137 | for (const chatId of subscriptions) { |
| 138 | try { |
| 139 | // 先删除上一条消息(如果存在) |
| 140 | const lastMsgId = this.db.data.lastMessages[chatId]; |
| 141 | if (lastMsgId) { |
| 142 | try { |
| 143 | await client.deleteMessages(chatId, [lastMsgId], { revoke: true }); |
| 144 | } catch (error) { |
| 145 | // 忽略删除失败的情况(消息可能已过期) |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | // 发送新贴纸 |
| 150 | const message = await client.sendFile(chatId, { |
| 151 | file: sticker, |
| 152 | attributes: [] |
| 153 | }); |
| 154 | |
| 155 | // 记录新消息ID,用于下次删除 |
| 156 | this.db.data.lastMessages[chatId] = message.id; |
| 157 | await this.db.write(); |
| 158 | |
| 159 | console.log(`[${this.PLUGIN_NAME}] 已发送整点报时到 ${chatId}`); |
| 160 | } catch (error) { |
| 161 | console.error(`[${this.PLUGIN_NAME}] 发送失败到 ${chatId}:`, error); |
| 162 | |
| 163 | // 如果发送失败,可能是聊天不存在或没有权限,移除订阅 |
| 164 | if ((error as any).message?.includes("CHAT_WRITE_FORBIDDEN") || |
| 165 | (error as any).message?.includes("CHAT_NOT_FOUND")) { |
| 166 | this.db.data.subscriptions = this.db.data.subscriptions.filter((id: string) => id !== chatId); |
| 167 | delete this.db.data.lastMessages[chatId]; |
| 168 | await this.db.write(); |
| 169 | console.log(`[${this.PLUGIN_NAME}] 已移除无效订阅: ${chatId}`); |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | // 检查用户权限(简化版本,实际使用时可能需要更复杂的权限检查) |
| 176 | private async checkPermission(msg: Api.Message): Promise<boolean> { |
| 177 | try { |
| 178 | const client = await getGlobalClient(); |
| 179 | if (!client) return false; |
| 180 |
no test coverage detected