| 181 | // ==================== 主插件类 ==================== |
| 182 | class ImageMonitorPlugin extends Plugin { |
| 183 | description: string = HELP_TEXT; |
| 184 | |
| 185 | cmdHandlers: Record<string, (msg: Api.Message) => Promise<void>> = { |
| 186 | im: this.handleConfigCommand.bind(this), |
| 187 | }; |
| 188 | |
| 189 | constructor() { |
| 190 | super(); |
| 191 | this.initialize(); |
| 192 | } |
| 193 | |
| 194 | private async initialize() { |
| 195 | await ConfigManager.init(); |
| 196 | console.log("[image_monitor] Plugin initialized"); |
| 197 | } |
| 198 | |
| 199 | // 消息监听器 - TeleBox会自动调用这个方法 |
| 200 | listenMessageHandler = async (msg: Api.Message, options?: { isEdited?: boolean }) => { |
| 201 | const client = await getGlobalClient(); |
| 202 | if (!client) return; |
| 203 | |
| 204 | // 获取有效的命令前缀 |
| 205 | const prefixes = getPrefixes(); |
| 206 | const text = msg.text || ""; |
| 207 | const commandParts = text.split(" "); |
| 208 | const command = commandParts[0].toLowerCase(); |
| 209 | const subCommand = commandParts[1]?.toLowerCase(); |
| 210 | |
| 211 | // 检查是否为有效的.im命令(必须以正确前缀开头) |
| 212 | const isValidImCommand = prefixes.some(prefix => command === `${prefix}im`); |
| 213 | |
| 214 | if (msg.isReply && isValidImCommand) { |
| 215 | const repliedMsg = await msg.getReplyMessage(); |
| 216 | if (!repliedMsg) { |
| 217 | await MessageManager.edit(msg, "❌ 未找到被回复的消息。"); |
| 218 | return; |
| 219 | } |
| 220 | |
| 221 | const config = await ConfigManager.getConfig(); |
| 222 | if (!config.bannedStickerIds) config.bannedStickerIds = {}; |
| 223 | const action = (subCommand === 'ban' || subCommand === 'delete') ? subCommand as Action : config.defaultAction; |
| 224 | |
| 225 | const media = repliedMsg.media; |
| 226 | if (!media) { |
| 227 | await MessageManager.edit(msg, "❌ 该回复不是图片、媒体或贴纸。请回复包含图片/媒体/贴纸的消息后再使用 <code>.im</code>。"); |
| 228 | return; |
| 229 | } |
| 230 | |
| 231 | try { |
| 232 | if (media instanceof Api.MessageMediaDocument) { |
| 233 | const docRaw = media.document; |
| 234 | if (docRaw instanceof Api.Document) { |
| 235 | const isSticker = Array.isArray(docRaw.attributes) && docRaw.attributes.some(a => a instanceof Api.DocumentAttributeSticker); |
| 236 | if (isSticker) { |
| 237 | const stickerId = String(docRaw.id); |
| 238 | config.bannedStickerIds[stickerId] = action; |
| 239 | await ConfigManager.saveConfig(); |
| 240 | await MessageManager.edit(msg, `✅ 已添加贴纸ID: <code>${htmlEscape(stickerId)}</code>,操作: <code>${action}</code>`); |
nothing calls this directly
no test coverage detected