How koishi-plugin/onebot-manager works
Project Type and Purpose
• This is an open-source Koishi plugin (“koishi-plugin-onebot-manager”) written in TypeScript.
• It plugs into a Koishi bot instance (via the OneBot v11 adapter), listens for group/friend events, applies auto-approval rules or routes events to human operators, and implements a suite of group-management commands.
File Structure Analysis
• Top-level files:
• package.json (declares dependencies on koishi, koishi-plugin-adapter-onebot, TypeScript, etc.)
• tsconfig.json (TypeScript compiler options)
• readme.md (detailed feature & configuration docs)
• src/ directory (the entire plugin code):
• index.ts – plugin entrypoint, registers event listeners & commands
• listener.ts – subscribes to OneBot events (join, leave, admin changes, kicks)
• request.ts – processes incoming friend/invite/group-join requests, applies auto-rules, escalates to manual review
• command.ts – defines text commands for manual review (y<n>, na, qgroup …) and group management actions (mute, kick, tag, essence, etc.)
• utils.ts – shared helper functions (regex matching, placeholder formatting, request tracking)
Key Architectural Patterns
• Event-driven: the plugin reacts to incoming OneBot events via Koishi’s event bus.
• Command pattern: administrative commands are parsed and dispatched to handlers in command.ts.
• Rule evaluation pipeline in request.ts: automatic vs. manual branching.
Main Components
• External Chat Platform (OneBot adapter)
• Koishi Core (event bus & command dispatcher)
• OneBot-Manager Plugin
• Event Listeners (listener.ts)
• Request Processor (request.ts)
• Command Handlers (command.ts)
• Utilities & Config Module (utils.ts + Koishi plugin config)
• Notification Sink (notifyTarget: group or private chat)
• Target Group Environment (actual QQ group where management actions occur)
Component Interactions
1. OneBot Adapter emits an event (friend request, group join, invite, admin change, group exit).
2. Koishi Core forwards the event to all registered plugins.
3. OneBot-Manager.listener.ts receives the event.
4. For request events:
– request.ts evaluates auto-approval rules (regex, level, whitelist, group size).
– If auto-approved → plugin calls OneBot API to accept.
– Else → plugin formats a notification and sends it to notifyTarget.
– The pending request is stored in memory (with an ID).
5. For command events (commands prefixed qgroup, y#, n#, ya, na):
– Koishi’s command dispatcher invokes command.ts.
– command.ts identifies the request by ID or executes bulk operations.
– Plugin calls OneBot API for accept/reject or group-management commands.
6. Event notifications (mute, kick, admin set/unset) are similarly listened for in listener.ts and forwarded to notifyTarget.
Technologies & Frameworks to Label
• TypeScript & Node.js
• Koishi Core & Command Framework
• koishi-plugin-adapter-onebot (OneBot v11 API)
• In-memory store (for tracking manual-review requests)
• Regular expressions (for auto-approval rules)