@zhin.js/core 1.0.45 → 1.0.47

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. package/CHANGELOG.md +24 -0
  2. package/lib/adapter.d.ts +13 -13
  3. package/lib/adapter.d.ts.map +1 -1
  4. package/lib/adapter.js +18 -63
  5. package/lib/adapter.js.map +1 -1
  6. package/lib/bot.d.ts +8 -2
  7. package/lib/bot.d.ts.map +1 -1
  8. package/lib/built/common-adapter-tools.d.ts +10 -14
  9. package/lib/built/common-adapter-tools.d.ts.map +1 -1
  10. package/lib/built/common-adapter-tools.js +47 -21
  11. package/lib/built/common-adapter-tools.js.map +1 -1
  12. package/lib/built/message-filter.d.ts +175 -0
  13. package/lib/built/message-filter.d.ts.map +1 -0
  14. package/lib/built/message-filter.js +236 -0
  15. package/lib/built/message-filter.js.map +1 -0
  16. package/lib/index.d.ts +3 -0
  17. package/lib/index.d.ts.map +1 -1
  18. package/lib/index.js +3 -0
  19. package/lib/index.js.map +1 -1
  20. package/lib/notice.d.ts +81 -0
  21. package/lib/notice.d.ts.map +1 -0
  22. package/lib/notice.js +11 -0
  23. package/lib/notice.js.map +1 -0
  24. package/lib/plugin.d.ts +6 -0
  25. package/lib/plugin.d.ts.map +1 -1
  26. package/lib/plugin.js.map +1 -1
  27. package/lib/request.d.ts +85 -0
  28. package/lib/request.d.ts.map +1 -0
  29. package/lib/request.js +11 -0
  30. package/lib/request.js.map +1 -0
  31. package/package.json +6 -6
  32. package/src/adapter.ts +28 -81
  33. package/src/bot.ts +8 -2
  34. package/src/built/common-adapter-tools.ts +54 -25
  35. package/src/built/message-filter.ts +390 -0
  36. package/src/index.ts +3 -0
  37. package/src/notice.ts +98 -0
  38. package/src/plugin.ts +8 -0
  39. package/src/request.ts +95 -0
  40. package/tests/message-filter.test.ts +566 -0
  41. package/tests/notice.test.ts +198 -0
  42. package/tests/request.test.ts +221 -0
@@ -0,0 +1,390 @@
1
+ /**
2
+ * MessageFilterFeature — 消息过滤引擎
3
+ *
4
+ * 设计理念:
5
+ * 将过滤规则视为 Feature Item,与命令 (CommandFeature)、权限 (PermissionFeature) 同构,
6
+ * 遵循框架的 add/remove/extensions/toJSON 范式,支持插件级 CRUD 和生命周期自动回收。
7
+ *
8
+ * 核心特性:
9
+ * - 基于优先级的规则引擎(first-match-wins,类似防火墙规则)
10
+ * - 多维匹配:scope / adapter / bot / channel / sender
11
+ * - 支持精确匹配、通配符 `*`、正则 `/pattern/`
12
+ * - 通过 Dispatcher Guardrail 集成,在消息调度第一阶段拦截
13
+ * - 插件通过 `addFilterRule()` 动态注册规则,卸载时自动清理
14
+ */
15
+
16
+ import { Feature, FeatureJSON } from '../feature.js';
17
+ import { getPlugin } from '../plugin.js';
18
+ import type { Message, MessageType } from '../message.js';
19
+
20
+ // ============================================================================
21
+ // 类型定义
22
+ // ============================================================================
23
+
24
+ /** 过滤动作 */
25
+ export type FilterAction = 'allow' | 'deny';
26
+
27
+ /** 匹配模式:精确字符串 / 正则表达式 */
28
+ export type FilterPattern = string | RegExp;
29
+
30
+ /** 消息作用域 */
31
+ export type FilterScope = MessageType; // 'private' | 'group' | 'channel'
32
+
33
+ /**
34
+ * 过滤规则
35
+ *
36
+ * 每条规则描述一组匹配条件和对应的动作。
37
+ * 所有条件之间为 AND 关系(必须全部满足),每个条件内部的多个值为 OR 关系(满足任一即可)。
38
+ * 未设置的条件视为匹配所有。
39
+ *
40
+ * @example
41
+ * // 拒绝特定群的消息
42
+ * { name: 'block-spam', action: 'deny', scopes: ['group'], channels: ['123456'] }
43
+ *
44
+ * // 放行 VIP 用户(高优先级)
45
+ * { name: 'allow-vip', action: 'allow', priority: 100, senders: ['admin001'] }
46
+ *
47
+ * // 正则匹配:拒绝所有 test- 开头的频道
48
+ * { name: 'block-test', action: 'deny', scopes: ['channel'], channels: [/^test-/] }
49
+ */
50
+ export interface FilterRule {
51
+ /** 规则名称(唯一标识) */
52
+ name: string;
53
+ /** 规则描述 */
54
+ description?: string;
55
+ /** 过滤动作:allow 放行 / deny 拦截 */
56
+ action: FilterAction;
57
+ /** 优先级,数值越大越先匹配(默认 0) */
58
+ priority?: number;
59
+ /** 是否启用(默认 true) */
60
+ enabled?: boolean;
61
+ /** 匹配的消息作用域,未设置则匹配所有 */
62
+ scopes?: FilterScope[];
63
+ /** 匹配的适配器名称 */
64
+ adapters?: FilterPattern[];
65
+ /** 匹配的 Bot 名称 */
66
+ bots?: FilterPattern[];
67
+ /** 匹配的频道/群/会话 ID */
68
+ channels?: FilterPattern[];
69
+ /** 匹配的发送者 ID */
70
+ senders?: FilterPattern[];
71
+ }
72
+
73
+ /** 过滤检测结果 */
74
+ export interface FilterResult {
75
+ /** 是否允许处理 */
76
+ allowed: boolean;
77
+ /** 匹配到的规则名称(null 表示无规则匹配,走默认策略) */
78
+ matchedRule: string | null;
79
+ /** 结果原因 */
80
+ reason: string;
81
+ }
82
+
83
+ // ---- 配置类型 ----
84
+
85
+ /** 配置文件中的规则(pattern 均为 string,正则用 /pattern/ 表示) */
86
+ export interface FilterRuleConfig {
87
+ name: string;
88
+ description?: string;
89
+ action: FilterAction;
90
+ priority?: number;
91
+ enabled?: boolean;
92
+ scopes?: FilterScope[];
93
+ adapters?: string[];
94
+ bots?: string[];
95
+ channels?: string[];
96
+ senders?: string[];
97
+ }
98
+
99
+ /**
100
+ * 消息过滤配置
101
+ *
102
+ * ```yaml
103
+ * message_filter:
104
+ * default_policy: allow
105
+ * rules:
106
+ * - name: block-spam
107
+ * action: deny
108
+ * scopes: [group]
109
+ * channels: ['123456']
110
+ * - name: vip-pass
111
+ * action: allow
112
+ * priority: 100
113
+ * senders: ['admin001']
114
+ * ```
115
+ */
116
+ export interface MessageFilterConfig {
117
+ /** 默认策略:无规则匹配时的行为(默认 'allow') */
118
+ default_policy?: FilterAction;
119
+ /** 规则列表 */
120
+ rules?: FilterRuleConfig[];
121
+ }
122
+
123
+ // ============================================================================
124
+ // Plugin 扩展声明
125
+ // ============================================================================
126
+
127
+ export interface MessageFilterContextExtensions {
128
+ /** 添加过滤规则,返回 dispose 函数 */
129
+ addFilterRule(rule: FilterRule): () => void;
130
+ /** 检测消息是否应被处理 */
131
+ testFilter(message: Message<any>): FilterResult;
132
+ /** 设置默认过滤策略 */
133
+ setDefaultFilterPolicy(policy: FilterAction): void;
134
+ }
135
+
136
+ declare module '../plugin.js' {
137
+ namespace Plugin {
138
+ interface Extensions extends MessageFilterContextExtensions {}
139
+ interface Contexts {
140
+ 'message-filter': MessageFilterFeature;
141
+ }
142
+ }
143
+ }
144
+
145
+ // ============================================================================
146
+ // 规则工厂
147
+ // ============================================================================
148
+
149
+ export namespace FilterRules {
150
+ /** 创建拒绝规则 */
151
+ export function deny(name: string, conditions: Omit<FilterRule, 'name' | 'action'>): FilterRule {
152
+ return { ...conditions, name, action: 'deny' };
153
+ }
154
+
155
+ /** 创建放行规则 */
156
+ export function allow(name: string, conditions: Omit<FilterRule, 'name' | 'action'>): FilterRule {
157
+ return { ...conditions, name, action: 'allow' };
158
+ }
159
+
160
+ /**
161
+ * 创建黑名单规则组
162
+ * @returns 一条 deny 规则
163
+ */
164
+ export function blacklist(
165
+ scope: FilterScope,
166
+ ids: string[],
167
+ name?: string,
168
+ ): FilterRule {
169
+ return {
170
+ name: name ?? `${scope}-blacklist`,
171
+ action: 'deny',
172
+ scopes: [scope],
173
+ channels: ids,
174
+ };
175
+ }
176
+
177
+ /**
178
+ * 创建白名单规则组
179
+ * @returns [allow 规则, deny-catch-all 规则]
180
+ */
181
+ export function whitelist(
182
+ scope: FilterScope,
183
+ ids: string[],
184
+ name?: string,
185
+ ): [FilterRule, FilterRule] {
186
+ const baseName = name ?? `${scope}-whitelist`;
187
+ return [
188
+ { name: baseName, action: 'allow', scopes: [scope], channels: ids, priority: 1 },
189
+ { name: `${baseName}-deny-rest`, action: 'deny', scopes: [scope], priority: -100 },
190
+ ];
191
+ }
192
+ }
193
+
194
+ // ============================================================================
195
+ // Feature 实现
196
+ // ============================================================================
197
+
198
+ export class MessageFilterFeature extends Feature<FilterRule> {
199
+ readonly name = 'message-filter' as const;
200
+ readonly icon = 'Filter';
201
+ readonly desc = '消息过滤';
202
+
203
+ /** 按规则名称索引 */
204
+ readonly byName = new Map<string, FilterRule>();
205
+
206
+ #defaultPolicy: FilterAction = 'allow';
207
+ #sortedCache: FilterRule[] | null = null;
208
+
209
+ constructor(config?: MessageFilterConfig) {
210
+ super();
211
+ if (config) this.#loadConfig(config);
212
+ }
213
+
214
+ // ---- 公共 API ----
215
+
216
+ /** 默认策略 */
217
+ get defaultPolicy(): FilterAction { return this.#defaultPolicy; }
218
+ set defaultPolicy(policy: FilterAction) { this.#defaultPolicy = policy; }
219
+
220
+ /** 获取按优先级排序的启用规则列表(带缓存) */
221
+ get sortedRules(): FilterRule[] {
222
+ if (!this.#sortedCache) {
223
+ this.#sortedCache = [...this.items]
224
+ .filter(r => r.enabled !== false)
225
+ .sort((a, b) => (b.priority ?? 0) - (a.priority ?? 0));
226
+ }
227
+ return this.#sortedCache;
228
+ }
229
+
230
+ /** 按名称查询规则 */
231
+ getRule(name: string): FilterRule | undefined {
232
+ return this.byName.get(name);
233
+ }
234
+
235
+ /**
236
+ * 检测消息是否应该被处理
237
+ *
238
+ * 遍历按优先级排序的规则列表,返回第一个匹配的规则结果。
239
+ * 若无规则匹配,按默认策略决定。
240
+ */
241
+ test(message: Message<any>): FilterResult {
242
+ for (const rule of this.sortedRules) {
243
+ if (this.#matchRule(rule, message)) {
244
+ return {
245
+ allowed: rule.action === 'allow',
246
+ matchedRule: rule.name,
247
+ reason: `matched rule "${rule.name}" → ${rule.action}`,
248
+ };
249
+ }
250
+ }
251
+ return {
252
+ allowed: this.#defaultPolicy === 'allow',
253
+ matchedRule: null,
254
+ reason: `no rule matched → default policy: ${this.#defaultPolicy}`,
255
+ };
256
+ }
257
+
258
+ // ---- Feature 重写 ----
259
+
260
+ /** 添加规则,维护 byName 索引 */
261
+ add(rule: FilterRule, pluginName: string): () => void {
262
+ this.byName.set(rule.name, rule);
263
+ this.#sortedCache = null;
264
+ return super.add(rule, pluginName);
265
+ }
266
+
267
+ /** 移除规则,清理 byName 索引 */
268
+ remove(rule: FilterRule, pluginName?: string): boolean {
269
+ this.byName.delete(rule.name);
270
+ this.#sortedCache = null;
271
+ return super.remove(rule, pluginName);
272
+ }
273
+
274
+ /** 序列化为 JSON(供 Web 控制台展示) */
275
+ toJSON(pluginName?: string): FeatureJSON {
276
+ const list = pluginName ? this.getByPlugin(pluginName) : this.items;
277
+ return {
278
+ name: this.name,
279
+ icon: this.icon,
280
+ desc: this.desc,
281
+ count: list.length,
282
+ items: list.map(r => ({
283
+ name: r.name,
284
+ description: r.description,
285
+ action: r.action,
286
+ priority: r.priority ?? 0,
287
+ enabled: r.enabled !== false,
288
+ scopes: r.scopes,
289
+ adapters: r.adapters?.map(p => p instanceof RegExp ? p.source : p),
290
+ bots: r.bots?.map(p => p instanceof RegExp ? p.source : p),
291
+ channels: r.channels?.map(p => p instanceof RegExp ? p.source : p),
292
+ senders: r.senders?.map(p => p instanceof RegExp ? p.source : p),
293
+ })),
294
+ };
295
+ }
296
+
297
+ /** 插件扩展方法 */
298
+ get extensions() {
299
+ const feature = this;
300
+ return {
301
+ addFilterRule(rule: FilterRule) {
302
+ const plugin = getPlugin();
303
+ const dispose = feature.add(rule, plugin.name);
304
+ plugin.recordFeatureContribution(feature.name, rule.name);
305
+ plugin.onDispose(dispose);
306
+ return dispose;
307
+ },
308
+ testFilter(message: Message<any>) {
309
+ return feature.test(message);
310
+ },
311
+ setDefaultFilterPolicy(policy: FilterAction) {
312
+ feature.defaultPolicy = policy;
313
+ },
314
+ };
315
+ }
316
+
317
+ // ============================================================================
318
+ // 配置加载
319
+ // ============================================================================
320
+
321
+ #loadConfig(config: MessageFilterConfig): void {
322
+ if (config.default_policy) {
323
+ this.#defaultPolicy = config.default_policy;
324
+ }
325
+
326
+ if (config.rules) {
327
+ for (const rc of config.rules) {
328
+ this.add(this.#parseRuleConfig(rc), '__config__');
329
+ }
330
+ }
331
+ }
332
+
333
+ // ============================================================================
334
+ // 规则匹配
335
+ // ============================================================================
336
+
337
+ /** 解析配置中的规则字符串为 FilterPattern */
338
+ #parseRuleConfig(rc: FilterRuleConfig): FilterRule {
339
+ return {
340
+ name: rc.name,
341
+ description: rc.description,
342
+ action: rc.action,
343
+ priority: rc.priority,
344
+ enabled: rc.enabled,
345
+ scopes: rc.scopes,
346
+ adapters: rc.adapters?.map(p => this.#parsePattern(p)),
347
+ bots: rc.bots?.map(p => this.#parsePattern(p)),
348
+ channels: rc.channels?.map(p => this.#parsePattern(p)),
349
+ senders: rc.senders?.map(p => this.#parsePattern(p)),
350
+ };
351
+ }
352
+
353
+ /** 解析单个 pattern:"/regex/flags" → RegExp,其余为精确字符串 */
354
+ #parsePattern(pattern: string): FilterPattern {
355
+ const match = pattern.match(/^\/(.+)\/([gimsuy]*)$/);
356
+ if (match) {
357
+ return new RegExp(match[1], match[2]);
358
+ }
359
+ return pattern;
360
+ }
361
+
362
+ /** 检查规则是否匹配消息(所有条件 AND,条件内 OR) */
363
+ #matchRule(rule: FilterRule, message: Message<any>): boolean {
364
+ if (rule.scopes?.length && !rule.scopes.includes(message.$channel.type as FilterScope)) {
365
+ return false;
366
+ }
367
+ if (rule.adapters?.length && !this.#matchAny(rule.adapters, String(message.$adapter))) {
368
+ return false;
369
+ }
370
+ if (rule.bots?.length && !this.#matchAny(rule.bots, String(message.$bot))) {
371
+ return false;
372
+ }
373
+ if (rule.channels?.length && !this.#matchAny(rule.channels, String(message.$channel.id))) {
374
+ return false;
375
+ }
376
+ if (rule.senders?.length && !this.#matchAny(rule.senders, String(message.$sender.id))) {
377
+ return false;
378
+ }
379
+ return true;
380
+ }
381
+
382
+ /** 检查值是否与任一 pattern 匹配 */
383
+ #matchAny(patterns: FilterPattern[], value: string): boolean {
384
+ return patterns.some(p => {
385
+ if (p instanceof RegExp) return p.test(value);
386
+ if (p === '*') return true;
387
+ return p === value;
388
+ });
389
+ }
390
+ }
package/src/index.ts CHANGED
@@ -6,6 +6,8 @@ export * from './command.js'
6
6
  export * from './component.js'
7
7
  export * from './adapter.js'
8
8
  export * from './message.js'
9
+ export * from './notice.js'
10
+ export * from './request.js'
9
11
  export * from './prompt.js'
10
12
  // Models
11
13
  export * from './models/system-log.js'
@@ -18,6 +20,7 @@ export * from './built/permission.js'
18
20
  export * from './built/adapter-process.js'
19
21
  export * from './built/component.js'
20
22
  export * from './built/database.js'
23
+ export * from './built/message-filter.js'
21
24
  // Tool Service (纯工具,无副作用)
22
25
  export * from './built/tool.js'
23
26
  // AI Trigger Service (纯工具,无副作用)
package/src/notice.ts ADDED
@@ -0,0 +1,98 @@
1
+ import { Adapters } from './adapter.js';
2
+ import type { MessageSender } from './types.js';
3
+
4
+ /**
5
+ * 通知类型枚举
6
+ *
7
+ * 常见 IM 通知事件分类:
8
+ * - group_member_increase: 群成员增加(入群)
9
+ * - group_member_decrease: 群成员减少(退群/被踢)
10
+ * - group_admin_change: 群管理员变动
11
+ * - group_ban: 群禁言
12
+ * - group_recall: 群消息撤回
13
+ * - friend_recall: 好友消息撤回
14
+ * - friend_add: 新增好友
15
+ * - group_poke: 群戳一戳
16
+ * - friend_poke: 好友戳一戳
17
+ * - group_transfer: 群转让
18
+ *
19
+ * 适配器可自行扩展更多子类型
20
+ */
21
+ export type NoticeType =
22
+ | 'group_member_increase'
23
+ | 'group_member_decrease'
24
+ | 'group_admin_change'
25
+ | 'group_ban'
26
+ | 'group_recall'
27
+ | 'friend_recall'
28
+ | 'friend_add'
29
+ | 'group_poke'
30
+ | 'friend_poke'
31
+ | 'group_transfer'
32
+ | (string & {}); // 允许适配器扩展自定义类型
33
+
34
+ /**
35
+ * 通知频道信息
36
+ */
37
+ export interface NoticeChannel {
38
+ id: string;
39
+ type: 'group' | 'private' | 'channel';
40
+ }
41
+
42
+ /**
43
+ * 通知基础结构
44
+ *
45
+ * 与 MessageBase 同构设计,所有通知共享以下字段。
46
+ * 适配器通过 `Notice.from(raw, base)` 合并平台原始数据和标准字段。
47
+ *
48
+ * @example
49
+ * ```typescript
50
+ * // 适配器中格式化通知
51
+ * const notice = Notice.from(rawEvent, {
52
+ * $id: rawEvent.id,
53
+ * $adapter: 'icqq',
54
+ * $bot: botName,
55
+ * $type: 'group_member_decrease',
56
+ * $subType: 'kick',
57
+ * $channel: { id: groupId, type: 'group' },
58
+ * $operator: { id: operatorId, name: '管理员' },
59
+ * $target: { id: userId, name: '被踢者' },
60
+ * $timestamp: Date.now(),
61
+ * });
62
+ * this.adapter.emit('notice.receive', notice);
63
+ * ```
64
+ */
65
+ export interface NoticeBase {
66
+ /** 通知唯一 ID(平台提供或自生成) */
67
+ $id: string;
68
+ /** 适配器名称 */
69
+ $adapter: keyof Adapters;
70
+ /** Bot 名称 */
71
+ $bot: string;
72
+ /** 通知类型 */
73
+ $type: NoticeType;
74
+ /** 通知子类型(如 leave/kick、set/unset) */
75
+ $subType?: string;
76
+ /** 通知发生的频道/群/会话 */
77
+ $channel: NoticeChannel;
78
+ /** 操作者信息(如管理员) */
79
+ $operator?: MessageSender;
80
+ /** 被操作目标信息(如被踢的成员) */
81
+ $target?: MessageSender;
82
+ /** 通知时间戳 */
83
+ $timestamp: number;
84
+ }
85
+
86
+ /**
87
+ * 完整通知类型,支持平台原始数据扩展
88
+ */
89
+ export type Notice<T extends object = {}> = NoticeBase & T;
90
+
91
+ export namespace Notice {
92
+ /**
93
+ * 工具方法:合并自定义字段与基础通知结构
94
+ */
95
+ export function from<T extends object>(input: T, format: NoticeBase): Notice<T> {
96
+ return Object.assign(input, format);
97
+ }
98
+ }
package/src/plugin.ts CHANGED
@@ -18,6 +18,8 @@ import { MessageMiddleware, RegisteredAdapter, MaybePromise, ArrayItem, SendOpti
18
18
  import type { ConfigFeature } from "./built/config.js";
19
19
  import type { PermissionFeature } from "./built/permission.js";
20
20
  import { Adapter, Adapters } from "./adapter.js";
21
+ import { Notice } from "./notice.js";
22
+ import { Request } from "./request.js";
21
23
  import { Feature, FeatureJSON } from "./feature.js";
22
24
  import { createHash } from "crypto";
23
25
  const contextsKey = Symbol("contexts");
@@ -1024,6 +1026,12 @@ export namespace Plugin {
1024
1026
  'before.sendMessage': [SendOptions];
1025
1027
  "context.mounted": [keyof Plugin.Contexts];
1026
1028
  "context.dispose": [keyof Plugin.Contexts];
1029
+ // Notice 事件
1030
+ 'notice.receive': [Notice];
1031
+ [key: `notice.${string}`]: [Notice];
1032
+ // Request 事件
1033
+ 'request.receive': [Request];
1034
+ [key: `request.${string}`]: [Request];
1027
1035
  }
1028
1036
 
1029
1037
  /**
package/src/request.ts ADDED
@@ -0,0 +1,95 @@
1
+ import { Adapters } from './adapter.js';
2
+ import type { MessageSender, MaybePromise } from './types.js';
3
+
4
+ /**
5
+ * 请求类型枚举
6
+ *
7
+ * 常见 IM 请求事件分类:
8
+ * - friend_add: 好友添加请求
9
+ * - group_add: 主动申请入群
10
+ * - group_invite: 邀请入群请求
11
+ *
12
+ * 适配器可自行扩展更多子类型
13
+ */
14
+ export type RequestType =
15
+ | 'friend_add'
16
+ | 'group_add'
17
+ | 'group_invite'
18
+ | (string & {}); // 允许适配器扩展自定义类型
19
+
20
+ /**
21
+ * 请求频道信息
22
+ */
23
+ export interface RequestChannel {
24
+ id: string;
25
+ type: 'group' | 'private' | 'channel';
26
+ }
27
+
28
+ /**
29
+ * 请求基础结构
30
+ *
31
+ * 与 MessageBase / NoticeBase 同构设计。
32
+ * 核心区别:Request 提供 `$approve()` 和 `$reject()` 方法,用于快速处理请求。
33
+ *
34
+ * @example
35
+ * ```typescript
36
+ * // 适配器中格式化请求
37
+ * const request = Request.from(rawEvent, {
38
+ * $id: rawEvent.flag,
39
+ * $adapter: 'icqq',
40
+ * $bot: botName,
41
+ * $type: 'group_invite',
42
+ * $channel: { id: groupId, type: 'group' },
43
+ * $sender: { id: userId, name: '邀请者' },
44
+ * $comment: '请求加群消息',
45
+ * $timestamp: Date.now(),
46
+ * $approve: async (remark?) => { await api.approve(flag, remark); },
47
+ * $reject: async (reason?) => { await api.reject(flag, reason); },
48
+ * });
49
+ * this.adapter.emit('request.receive', request);
50
+ * ```
51
+ */
52
+ export interface RequestBase {
53
+ /** 请求唯一 ID / flag(平台提供的请求标识,用于后续处理) */
54
+ $id: string;
55
+ /** 适配器名称 */
56
+ $adapter: keyof Adapters;
57
+ /** Bot 名称 */
58
+ $bot: string;
59
+ /** 请求类型 */
60
+ $type: RequestType;
61
+ /** 请求子类型 */
62
+ $subType?: string;
63
+ /** 请求发生的频道/群/会话 */
64
+ $channel: RequestChannel;
65
+ /** 请求发送者 */
66
+ $sender: MessageSender;
67
+ /** 请求附言/验证消息 */
68
+ $comment?: string;
69
+ /** 请求时间戳 */
70
+ $timestamp: number;
71
+ /**
72
+ * 同意请求
73
+ * @param remark 备注信息(如好友备注)
74
+ */
75
+ $approve(remark?: string): MaybePromise<void>;
76
+ /**
77
+ * 拒绝请求
78
+ * @param reason 拒绝原因
79
+ */
80
+ $reject(reason?: string): MaybePromise<void>;
81
+ }
82
+
83
+ /**
84
+ * 完整请求类型,支持平台原始数据扩展
85
+ */
86
+ export type Request<T extends object = {}> = RequestBase & T;
87
+
88
+ export namespace Request {
89
+ /**
90
+ * 工具方法:合并自定义字段与基础请求结构
91
+ */
92
+ export function from<T extends object>(input: T, format: RequestBase): Request<T> {
93
+ return Object.assign(input, format);
94
+ }
95
+ }