| 109 | description: string = `📌 消息模式插件\n\n${help_text}`; |
| 110 | private db: any; |
| 111 | |
| 112 | constructor() { |
| 113 | super(); |
| 114 | } |
| 115 | |
| 116 | cleanup(): void { |
| 117 | // 引用重置:清空实例级 db / cache / manager 引用,便于 reload 后重新初始化。 |
| 118 | this.db = null; |
| 119 | } |
| 120 | |
| 121 | async setup(): Promise<void> { |
| 122 | await this.initDB(); |
| 123 | } |
| 124 | |
| 125 | /* ===================== 初始化数据库 ===================== */ |
| 126 | |
| 127 | private async initDB() { |
| 128 | const dbPath = resolvePluginAssetFile({ |
| 129 | plugin: "mode", |
| 130 | fileName: "config.json", |
| 131 | legacyDirs: ["messageMode"], |
| 132 | legacyFiles: [{ dir: "messageMode", fileName: "config.json" }], |
| 133 | }); |
| 134 | |
| 135 | this.db = await JSONFilePreset(dbPath, { |
| 136 | chats: {}, // per-chat 模式 |
| 137 | whitelist: [], // 白名单 chat_id[] |
| 138 | blacklist: [], // 黑名单 chat_id[] |
| 139 | globalMode: Mode.OFF, // 全局模式 |
| 140 | }); |
| 141 | } |
| 142 | |
| 143 | /* ===================== per-chat 模式读取与设置 ===================== */ |
| 144 | |
| 145 | private getChatMode(chatId: string): Mode { |
| 146 | return this.db.data.chats[chatId] || Mode.OFF; |
| 147 | } |
| 148 | |
| 149 | private async setChatMode(chatId: string, mode: Mode) { |
| 150 | this.db.data.chats[chatId] = mode; |
| 151 | await this.db.write(); |
| 152 | } |
| 153 | |
| 154 | /* ===================== 白名单 / 黑名单 ===================== */ |
| 155 | |
| 156 | private isWhite(chatId: string): boolean { |
| 157 | return this.db.data.whitelist.includes(chatId); |
| 158 | } |
| 159 | |
| 160 | private isBlack(chatId: string): boolean { |
| 161 | return this.db.data.blacklist.includes(chatId); |
| 162 | } |
| 163 | |
| 164 | /* ===================== 命令处理 ===================== */ |
| 165 | |
| 166 | cmdHandlers: Record<string, (msg: Api.Message, trigger?: Api.Message) => Promise<void>> = { |
| 167 | mode: async (msg: Api.Message) => { |
| 168 | if (!this.db) await this.initDB(); |
nothing calls this directly
no test coverage detected