@zhin.js/adapter-kook 1.0.51 → 1.0.53

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.
package/lib/index.js CHANGED
@@ -1,952 +1,13 @@
1
- import { Client, } from "kook-client";
2
- import path from "path";
3
- import { Adapter, Message, usePlugin, createGroupManagementTools, GROUP_MANAGEMENT_SKILL_KEYWORDS, GROUP_MANAGEMENT_SKILL_TAGS, } from "zhin.js";
4
- // KOOK 用户权限枚举
5
- export var KookPermission;
6
- (function (KookPermission) {
7
- KookPermission[KookPermission["Normal"] = 1] = "Normal";
8
- KookPermission[KookPermission["Admin"] = 2] = "Admin";
9
- KookPermission[KookPermission["Owner"] = 4] = "Owner";
10
- KookPermission[KookPermission["ChannelAdmin"] = 5] = "ChannelAdmin";
11
- })(KookPermission || (KookPermission = {}));
12
- const plugin = usePlugin();
13
- const { provide } = plugin;
14
- const logger = plugin.logger;
15
- /**
16
- * KOOK Bot 实现
17
- */
18
- export class KookBot extends Client {
19
- $config;
20
- $connected = false;
21
- adapter;
22
- get $id() {
23
- return this.$config.name;
24
- }
25
- constructor(adapter, $config) {
26
- super({
27
- token: $config.token,
28
- mode: "websocket", // KOOK 默认使用 WebSocket 模式
29
- data_dir: $config.data_dir || path.join(process.cwd(), "data", "kook"),
30
- timeout: $config.timeout || 10000,
31
- max_retry: $config.max_retry || 3,
32
- ignore: $config.ignore || "bot",
33
- logLevel: $config.logLevel || "info",
34
- });
35
- this.$config = $config;
36
- this.adapter = adapter;
37
- this.setupEventListeners();
38
- }
39
- /**
40
- * 设置事件监听器
41
- */
42
- setupEventListeners() {
43
- // 监听消息事件
44
- this.on("message", (msg) => {
45
- try {
46
- const message = this.$formatMessage(msg);
47
- logger.debug(`KOOK 格式化消息: $content=${JSON.stringify(message.$content)}, $raw=${message.$raw}`);
48
- this.adapter.emit("message.receive", message);
49
- // 根据消息类型触发特定事件
50
- const eventMap = {
51
- private: "message.private.receive",
52
- group: "message.group.receive",
53
- channel: "message.channel.receive",
54
- };
55
- const specificEvent = eventMap[msg.message_type];
56
- if (specificEvent) {
57
- this.adapter.emit(specificEvent, message);
58
- }
59
- }
60
- catch (error) {
61
- logger.error(`处理 KOOK 消息失败:`, error);
62
- }
63
- });
64
- // 监听连接事件
65
- this.on("connect", () => {
66
- this.$connected = true;
67
- logger.info(`KOOK Bot ${this.$id} 已连接`);
68
- });
69
- // 监听断开事件
70
- this.on("disconnect", () => {
71
- this.$connected = false;
72
- logger.warn(`KOOK Bot ${this.$id} 已断开`);
73
- });
74
- // 监听错误事件
75
- this.on("error", (error) => {
76
- logger.error(`KOOK Bot ${this.$id} 错误:`, error);
77
- });
78
- }
79
- /**
80
- * 将 KOOK 消息转换为标准消息格式
81
- */
82
- $formatMessage(msg) {
83
- const channelId = msg.message_type === "channel"
84
- ? msg.channel_id
85
- : msg.author_id;
86
- // 获取发送者的权限信息
87
- const senderInfo = this.getSenderInfo(msg);
88
- // 频道消息需要获取 guild_id
89
- let guildId;
90
- if (msg.message_type === "channel") {
91
- const channelMsg = msg;
92
- // 尝试从 channel 获取 guild_id
93
- try {
94
- const channel = channelMsg.channel;
95
- guildId = channel?.info?.guild_id;
96
- }
97
- catch {
98
- // 如果获取失败,尝试从缓存中查找
99
- }
100
- }
101
- const message = Message.from(msg, {
102
- $id: msg.message_id.toString(),
103
- $adapter: "kook",
104
- $bot: this.$id,
105
- $sender: senderInfo,
106
- $channel: {
107
- id: channelId.toString(),
108
- type: msg.message_type === "channel" ? "channel" : "private",
109
- // 频道消息包含服务器ID
110
- ...(guildId ? { guild_id: guildId } : {}),
111
- },
112
- $content: this.parseMessageContent(msg.message),
113
- $raw: msg.raw_message,
114
- $timestamp: msg.timestamp,
115
- $recall: async () => {
116
- await this.$recallMessage(message.$id);
117
- },
118
- $reply: async (content, quote) => {
119
- const elements = Array.isArray(content) ? content : [content];
120
- const finalContent = [];
121
- if (quote) {
122
- finalContent.push({
123
- type: "reply",
124
- data: {
125
- id: typeof quote === "boolean" ? message.$id : quote,
126
- },
127
- });
128
- }
129
- finalContent.push(...elements.map(el => typeof el === 'string' ? { type: 'text', data: { text: el } } : el));
130
- return await this.adapter.sendMessage({
131
- ...message.$channel,
132
- context: "kook",
133
- bot: this.$id,
134
- content: finalContent,
135
- });
136
- },
137
- });
138
- return message;
139
- }
140
- /**
141
- * 获取发送者的详细权限信息
142
- */
143
- getSenderInfo(msg) {
144
- const authorInfo = msg.author?.info;
145
- const senderInfo = {
146
- id: msg.author_id.toString(),
147
- name: authorInfo?.nickname || authorInfo?.username || "未知用户",
148
- };
149
- // 频道消息才有权限信息
150
- if (msg.message_type === "channel") {
151
- const channelMsg = msg;
152
- // 从 author.info 中获取权限信息(如果 kook-client 提供)
153
- if (authorInfo) {
154
- // 尝试获取角色列表
155
- senderInfo.roles = authorInfo.roles || [];
156
- // 尝试获取 guild_id 并检查是否为服务器主人
157
- try {
158
- const channel = channelMsg.channel;
159
- const guildId = channel?.info?.guild_id;
160
- if (guildId) {
161
- const guildInfo = this.guilds?.get(guildId);
162
- if (guildInfo) {
163
- senderInfo.isGuildOwner = guildInfo.user_id === msg.author_id;
164
- }
165
- }
166
- }
167
- catch {
168
- // 忽略获取失败
169
- }
170
- // 根据 permission 字段判断(如果有)
171
- const permission = authorInfo.permission;
172
- if (permission !== undefined) {
173
- senderInfo.permission = permission;
174
- senderInfo.isAdmin = permission === KookPermission.Admin ||
175
- permission === KookPermission.Owner ||
176
- permission === KookPermission.ChannelAdmin;
177
- senderInfo.isGuildOwner = senderInfo.isGuildOwner || permission === KookPermission.Owner;
178
- }
179
- }
180
- }
181
- return senderInfo;
182
- }
183
- // ==================== 频道管理 API ====================
184
- /**
185
- * 踢出用户
186
- * @param guildId 服务器ID
187
- * @param userId 用户ID
188
- */
189
- async kickUser(guildId, userId) {
190
- try {
191
- const guild = this.pickGuild(guildId);
192
- const result = await guild.kick(userId);
193
- logger.info(`KOOK Bot ${this.$id} 踢出用户 ${userId} 从服务器 ${guildId}`);
194
- return result;
195
- }
196
- catch (error) {
197
- logger.error(`KOOK Bot ${this.$id} 踢出用户失败:`, error);
198
- throw error;
199
- }
200
- }
201
- /**
202
- * 将用户加入黑名单
203
- * @param guildId 服务器ID
204
- * @param userId 用户ID
205
- * @param remark 备注
206
- * @param delMsgDays 删除消息天数(0-7)
207
- */
208
- async addToBlacklist(guildId, userId, remark, delMsgDays) {
209
- try {
210
- const member = this.pickGuildMember(guildId, userId);
211
- const result = await member.addToBlackList(remark, delMsgDays);
212
- logger.info(`KOOK Bot ${this.$id} 将用户 ${userId} 加入黑名单(服务器 ${guildId})`);
213
- return result;
214
- }
215
- catch (error) {
216
- logger.error(`KOOK Bot ${this.$id} 加入黑名单失败:`, error);
217
- throw error;
218
- }
219
- }
220
- /**
221
- * 将用户从黑名单移除
222
- * @param guildId 服务器ID
223
- * @param userId 用户ID
224
- */
225
- async removeFromBlacklist(guildId, userId) {
226
- try {
227
- const member = this.pickGuildMember(guildId, userId);
228
- const result = await member.removeFromBlackList();
229
- logger.info(`KOOK Bot ${this.$id} 将用户 ${userId} 从黑名单移除(服务器 ${guildId})`);
230
- return result;
231
- }
232
- catch (error) {
233
- logger.error(`KOOK Bot ${this.$id} 移除黑名单失败:`, error);
234
- throw error;
235
- }
236
- }
237
- /**
238
- * 给用户授予角色
239
- * @param guildId 服务器ID
240
- * @param userId 用户ID
241
- * @param roleId 角色ID
242
- */
243
- async grantRole(guildId, userId, roleId) {
244
- try {
245
- const member = this.pickGuildMember(guildId, userId);
246
- const result = await member.grant(roleId);
247
- logger.info(`KOOK Bot ${this.$id} 授予用户 ${userId} 角色 ${roleId}(服务器 ${guildId})`);
248
- return result;
249
- }
250
- catch (error) {
251
- logger.error(`KOOK Bot ${this.$id} 授予角色失败:`, error);
252
- throw error;
253
- }
254
- }
255
- /**
256
- * 撤销用户角色
257
- * @param guildId 服务器ID
258
- * @param userId 用户ID
259
- * @param roleId 角色ID
260
- */
261
- async revokeRole(guildId, userId, roleId) {
262
- try {
263
- const member = this.pickGuildMember(guildId, userId);
264
- const result = await member.revoke(roleId);
265
- logger.info(`KOOK Bot ${this.$id} 撤销用户 ${userId} 角色 ${roleId}(服务器 ${guildId})`);
266
- return result;
267
- }
268
- catch (error) {
269
- logger.error(`KOOK Bot ${this.$id} 撤销角色失败:`, error);
270
- throw error;
271
- }
272
- }
273
- /**
274
- * 设置用户昵称
275
- * @param guildId 服务器ID
276
- * @param userId 用户ID
277
- * @param nickname 新昵称
278
- */
279
- async setNickname(guildId, userId, nickname) {
280
- try {
281
- const member = this.pickGuildMember(guildId, userId);
282
- const result = await member.setNickname(nickname);
283
- logger.info(`KOOK Bot ${this.$id} 设置用户 ${userId} 昵称为 "${nickname}"(服务器 ${guildId})`);
284
- return result;
285
- }
286
- catch (error) {
287
- logger.error(`KOOK Bot ${this.$id} 设置昵称失败:`, error);
288
- throw error;
289
- }
290
- }
291
- /**
292
- * 获取服务器角色列表
293
- * @param guildId 服务器ID
294
- */
295
- async getRoleList(guildId) {
296
- try {
297
- const guild = this.pickGuild(guildId);
298
- return await guild.getRoleList();
299
- }
300
- catch (error) {
301
- logger.error(`KOOK Bot ${this.$id} 获取角色列表失败:`, error);
302
- throw error;
303
- }
304
- }
305
- /**
306
- * 创建角色
307
- * @param guildId 服务器ID
308
- * @param name 角色名称
309
- */
310
- async createRole(guildId, name) {
311
- try {
312
- const guild = this.pickGuild(guildId);
313
- const role = await guild.createRole(name);
314
- logger.info(`KOOK Bot ${this.$id} 创建角色 "${name}"(服务器 ${guildId})`);
315
- return role;
316
- }
317
- catch (error) {
318
- logger.error(`KOOK Bot ${this.$id} 创建角色失败:`, error);
319
- throw error;
320
- }
321
- }
322
- /**
323
- * 删除角色
324
- * @param guildId 服务器ID
325
- * @param roleId 角色ID
326
- */
327
- async deleteRole(guildId, roleId) {
328
- try {
329
- const guild = this.pickGuild(guildId);
330
- const result = await guild.deleteRole(roleId);
331
- logger.info(`KOOK Bot ${this.$id} 删除角色 ${roleId}(服务器 ${guildId})`);
332
- return result;
333
- }
334
- catch (error) {
335
- logger.error(`KOOK Bot ${this.$id} 删除角色失败:`, error);
336
- throw error;
337
- }
338
- }
339
- /**
340
- * 获取服务器成员列表
341
- * @param guildId 服务器ID
342
- * @param channelId 可选的频道ID
343
- */
344
- async getGuildMembers(guildId, channelId) {
345
- try {
346
- return await this.getGuildUserList(guildId, channelId);
347
- }
348
- catch (error) {
349
- logger.error(`KOOK Bot ${this.$id} 获取成员列表失败:`, error);
350
- throw error;
351
- }
352
- }
353
- parseMarkdown(content) {
354
- const elements = [];
355
- // KMarkdown 图片格式: ![alt](url)
356
- const imageRegex = /!\[([^\]]*)\]\(([^)]+)\)/g;
357
- // KMarkdown @提及格式: (met)userId(met) 或 @用户名
358
- const mentionRegex = /\(met\)(\d+)\(met\)|@([^\s]+)/g;
359
- // KMarkdown 表情格式: (emj)表情名(emj)[表情ID]
360
- const emojiRegex = /\(emj\)([^(]+)\(emj\)\[([^\]]+)\]/g;
361
- // KMarkdown 频道格式: (chn)channelId(chn)
362
- const channelRegex = /\(chn\)(\d+)\(chn\)/g;
363
- let lastIndex = 0;
364
- const matches = [];
365
- // 解析图片
366
- let match;
367
- while ((match = imageRegex.exec(content)) !== null) {
368
- matches.push({
369
- index: match.index,
370
- length: match[0].length,
371
- element: { type: "image", data: { url: match[2], alt: match[1] } }
372
- });
373
- }
374
- // 解析 @提及
375
- while ((match = mentionRegex.exec(content)) !== null) {
376
- const userId = match[1] || match[2];
377
- matches.push({
378
- index: match.index,
379
- length: match[0].length,
380
- element: { type: "at", data: { id: userId } }
381
- });
382
- }
383
- // 解析表情
384
- while ((match = emojiRegex.exec(content)) !== null) {
385
- matches.push({
386
- index: match.index,
387
- length: match[0].length,
388
- element: { type: "face", data: { id: match[2], name: match[1] } }
389
- });
390
- }
391
- // 解析频道引用
392
- while ((match = channelRegex.exec(content)) !== null) {
393
- matches.push({
394
- index: match.index,
395
- length: match[0].length,
396
- element: { type: "text", data: { text: `#频道:${match[1]}` } }
397
- });
398
- }
399
- // 按位置排序
400
- matches.sort((a, b) => a.index - b.index);
401
- // 组装消息段
402
- for (const match of matches) {
403
- // 添加之前的文本
404
- if (match.index > lastIndex) {
405
- const text = content.slice(lastIndex, match.index);
406
- if (text) {
407
- elements.push({ type: "text", data: { text } });
408
- }
409
- }
410
- // 添加特殊元素
411
- elements.push(match.element);
412
- lastIndex = match.index + match.length;
413
- }
414
- // 添加剩余文本
415
- if (lastIndex < content.length) {
416
- const text = content.slice(lastIndex);
417
- if (text) {
418
- elements.push({ type: "text", data: { text } });
419
- }
420
- }
421
- // 如果没有解析到任何特殊元素,返回纯文本
422
- if (elements.length === 0) {
423
- elements.push({ type: "text", data: { text: content } });
424
- }
425
- return elements;
426
- }
427
- /**
428
- * 将 kook-client 的 MessageSegment[] 转换为 Zhin 的 MessageElement[]
429
- */
430
- parseMessageContent(segments) {
431
- const elements = [];
432
- for (const segment of segments) {
433
- switch (segment.type) {
434
- case "markdown":
435
- // 检查是否包含特殊语法,如果是纯文本则直接转换
436
- if (this.hasKMarkdownSyntax(segment.text)) {
437
- elements.push(...this.parseMarkdown(segment.text));
438
- }
439
- else {
440
- elements.push({ type: "text", data: { text: segment.text } });
441
- }
442
- break;
443
- case "text":
444
- elements.push({ type: "text", data: { text: segment.text } });
445
- break;
446
- case "at":
447
- elements.push({ type: "at", data: { id: segment.user_id } });
448
- break;
449
- case "image":
450
- elements.push({
451
- type: "image",
452
- data: { url: segment.url, alt: segment.title || "图片" }
453
- });
454
- break;
455
- case "video":
456
- elements.push({ type: "video", data: { url: segment.url } });
457
- break;
458
- case "audio":
459
- elements.push({ type: "audio", data: { url: segment.url } });
460
- break;
461
- case "file":
462
- elements.push({
463
- type: "file",
464
- data: { url: segment.url, name: segment.name }
465
- });
466
- break;
467
- case "reply":
468
- elements.push({ type: "reply", data: { id: segment.id } });
469
- break;
470
- case "card":
471
- // Card 消息暂不支持,转为提示文本
472
- elements.push({ type: "text", data: { text: "[卡片消息]" } });
473
- break;
474
- default:
475
- logger.warn(`未知的 KOOK 消息段类型: ${segment.type}`);
476
- break;
477
- }
478
- }
479
- return elements.length > 0 ? elements : [{ type: "text", data: { text: "" } }];
480
- }
481
- /**
482
- * 检查文本是否包含 KMarkdown 特殊语法
483
- */
484
- hasKMarkdownSyntax(text) {
485
- return /!\[.*?\]\(.*?\)|\(met\)|\(emj\)|\(chn\)|@\S+/.test(text);
486
- }
487
- /**
488
- * 连接到 KOOK
489
- */
490
- async $connect() {
491
- try {
492
- await this.connect();
493
- this.$connected = true;
494
- logger.info(`KOOK Bot ${this.$id} 连接成功`);
495
- }
496
- catch (error) {
497
- logger.error(`KOOK Bot ${this.$id} 连接失败:`, error);
498
- throw error;
499
- }
500
- }
501
- /**
502
- * 断开连接
503
- */
504
- async $disconnect() {
505
- try {
506
- await this.disconnect();
507
- this.$connected = false;
508
- logger.info(`KOOK Bot ${this.$id} 已断开连接`);
509
- }
510
- catch (error) {
511
- logger.error(`KOOK Bot ${this.$id} 断开连接失败:`, error);
512
- throw error;
513
- }
514
- }
515
- /**
516
- * 发送消息
517
- */
518
- async $sendMessage(options) {
519
- try {
520
- const { id, type, content } = options;
521
- // 将消息段转换为 KOOK 格式
522
- const elements = Array.isArray(content)
523
- ? content.map(el => typeof el === 'string' ? { type: 'text', data: { text: el } } : el)
524
- : [typeof content === 'string' ? { type: 'text', data: { text: content } } : content];
525
- const kookContent = this.convertToKookFormat(elements);
526
- // 根据消息类型发送
527
- let result;
528
- if (type === "private") {
529
- result = await this.sendPrivateMsg(id, kookContent);
530
- }
531
- else {
532
- result = await this.sendChannelMsg(id, kookContent);
533
- }
534
- return result?.msg_id || "";
535
- }
536
- catch (error) {
537
- logger.error(`KOOK Bot ${this.$id} 发送消息失败:`, error);
538
- throw error;
539
- }
540
- }
541
- /**
542
- * 撤回消息
543
- */
544
- async $recallMessage(messageId) {
545
- try {
546
- await this.deleteMsg(messageId);
547
- logger.debug(`KOOK Bot ${this.$id} 撤回消息: ${messageId}`);
548
- }
549
- catch (error) {
550
- logger.error(`KOOK Bot ${this.$id} 撤回消息失败:`, error);
551
- throw error;
552
- }
553
- }
554
- /**
555
- * 将消息段转换为 KOOK KMarkdown 格式
556
- * 支持:文本、图片、@提及、表情、引用等
557
- */
558
- convertToKookFormat(content) {
559
- return content
560
- .map((el) => {
561
- switch (el.type) {
562
- case "text":
563
- // 纯文本,转义特殊字符
564
- return el.data.text.replace(/[\\`*_{}[\]()#+\-.!]/g, '\\$&');
565
- case "image":
566
- // 图片:![alt](url)
567
- return `![${el.data.alt || '图片'}](${el.data.url || el.data.file})`;
568
- case "at":
569
- // @提及:(met)userId(met) 或 @all
570
- if (el.data.id === "all") {
571
- return "(met)all(met)";
572
- }
573
- return `(met)${el.data.id}(met)`;
574
- case "face":
575
- // 表情:(emj)表情名(emj)[表情ID]
576
- return `(emj)${el.data.name || 'emoji'}(emj)[${el.data.id}]`;
577
- case "reply":
578
- // 引用消息(KOOK 使用 quote 参数,不在消息内容中)
579
- return "";
580
- case "video":
581
- // 视频:使用链接形式
582
- return `[视频](${el.data.url || el.data.file})`;
583
- case "audio":
584
- // 音频:使用链接形式
585
- return `[音频](${el.data.url || el.data.file})`;
586
- case "file":
587
- // 文件:使用链接形式
588
- return `[文件: ${el.data.name || '未命名'}](${el.data.url || el.data.file})`;
589
- case "link":
590
- // 链接:[文本](url)
591
- return `[${el.data.text || el.data.url}](${el.data.url})`;
592
- case "bold":
593
- // 粗体:**文本**
594
- return `**${el.data.text}**`;
595
- case "italic":
596
- // 斜体:*文本*
597
- return `*${el.data.text}*`;
598
- case "code":
599
- // 行内代码:`代码`
600
- return `\`${el.data.text}\``;
601
- case "code_block":
602
- // 代码块:```语言\n代码\n```
603
- return `\`\`\`${el.data.language || ''}\n${el.data.text}\n\`\`\``;
604
- default:
605
- // 未知类型,尝试转换为文本
606
- logger.warn(`未知的消息段类型: ${el.type}`);
607
- return el.data.text || JSON.stringify(el.data);
608
- }
609
- })
610
- .filter(Boolean)
611
- .join("");
612
- }
613
- }
614
1
  /**
615
- * KOOK 适配器
616
- */
617
- export class KookAdapter extends Adapter {
618
- constructor(plugin) {
619
- super(plugin, "kook", []);
620
- }
621
- createBot(config) {
622
- return new KookBot(this, config);
623
- }
624
- // ── IGroupManagement 标准群管方法 ──────────────────────────────────
625
- async kickMember(botId, sceneId, userId) {
626
- const bot = this.bots.get(botId);
627
- if (!bot)
628
- throw new Error(`Bot ${botId} 不存在`);
629
- return bot.kickUser(sceneId, userId);
630
- }
631
- async banMember(botId, sceneId, userId, reason) {
632
- const bot = this.bots.get(botId);
633
- if (!bot)
634
- throw new Error(`Bot ${botId} 不存在`);
635
- return bot.addToBlacklist(sceneId, userId, reason);
636
- }
637
- async unbanMember(botId, sceneId, userId) {
638
- const bot = this.bots.get(botId);
639
- if (!bot)
640
- throw new Error(`Bot ${botId} 不存在`);
641
- return bot.removeFromBlacklist(sceneId, userId);
642
- }
643
- async setMemberNickname(botId, sceneId, userId, nickname) {
644
- const bot = this.bots.get(botId);
645
- if (!bot)
646
- throw new Error(`Bot ${botId} 不存在`);
647
- return bot.setNickname(sceneId, userId, nickname);
648
- }
649
- async listMembers(botId, sceneId) {
650
- const bot = this.bots.get(botId);
651
- if (!bot)
652
- throw new Error(`Bot ${botId} 不存在`);
653
- const members = await bot.getGuildMembers(sceneId);
654
- return {
655
- members: members.map(m => ({
656
- id: m.id, username: m.username,
657
- nickname: m.nickname, roles: m.roles,
658
- })),
659
- count: members.length,
660
- };
661
- }
662
- // ── 生命周期 ───────────────────────────────────────────────────────
663
- async start() {
664
- this.registerKookPlatformTools();
665
- const groupTools = createGroupManagementTools(this, this.name);
666
- groupTools.forEach((t) => this.addTool(t));
667
- this.declareSkill({
668
- description: 'KOOK 服务器/频道群管理:踢人、禁言、封禁、设管理员、改服务器名、查成员等。仅昵称时请先 list_members 获取 user_id。',
669
- keywords: GROUP_MANAGEMENT_SKILL_KEYWORDS,
670
- tags: GROUP_MANAGEMENT_SKILL_TAGS,
671
- });
672
- await super.start();
673
- logger.info("KOOK 适配器已启动");
674
- }
675
- async stop() {
676
- await super.stop();
677
- logger.info("KOOK 适配器已停止");
678
- }
679
- /**
680
- * 注册 KOOK 平台特有工具(角色授予等)
681
- */
682
- registerKookPlatformTools() {
683
- // 角色管理工具 - 授予角色
684
- this.addTool({
685
- name: 'kook_grant_role',
686
- description: '给用户授予 KOOK 服务器角色',
687
- parameters: {
688
- type: 'object',
689
- properties: {
690
- bot: {
691
- type: 'string',
692
- description: 'Bot 名称',
693
- },
694
- guild_id: {
695
- type: 'string',
696
- description: '服务器 ID',
697
- },
698
- user_id: {
699
- type: 'string',
700
- description: '用户 ID',
701
- },
702
- role_id: {
703
- type: 'string',
704
- description: '角色 ID',
705
- },
706
- },
707
- required: ['bot', 'guild_id', 'user_id', 'role_id'],
708
- },
709
- platforms: ['kook'],
710
- scopes: ['channel'],
711
- permissionLevel: 'group_admin',
712
- execute: async (args, context) => {
713
- const { bot: botId, guild_id, user_id, role_id } = args;
714
- const bot = this.bots.get(botId);
715
- if (!bot)
716
- throw new Error(`Bot ${botId} 不存在`);
717
- this.checkPermission(context, 'group_admin');
718
- const success = await bot.grantRole(guild_id, user_id, role_id);
719
- return { success, message: success ? `已授予用户 ${user_id} 角色 ${role_id}` : '授予角色失败' };
720
- },
721
- });
722
- // 角色管理工具 - 撤销角色
723
- this.addTool({
724
- name: 'kook_revoke_role',
725
- description: '撤销用户的 KOOK 服务器角色',
726
- parameters: {
727
- type: 'object',
728
- properties: {
729
- bot: {
730
- type: 'string',
731
- description: 'Bot 名称',
732
- },
733
- guild_id: {
734
- type: 'string',
735
- description: '服务器 ID',
736
- },
737
- user_id: {
738
- type: 'string',
739
- description: '用户 ID',
740
- },
741
- role_id: {
742
- type: 'string',
743
- description: '角色 ID',
744
- },
745
- },
746
- required: ['bot', 'guild_id', 'user_id', 'role_id'],
747
- },
748
- platforms: ['kook'],
749
- scopes: ['channel'],
750
- permissionLevel: 'group_admin',
751
- execute: async (args, context) => {
752
- const { bot: botId, guild_id, user_id, role_id } = args;
753
- const bot = this.bots.get(botId);
754
- if (!bot)
755
- throw new Error(`Bot ${botId} 不存在`);
756
- this.checkPermission(context, 'group_admin');
757
- const success = await bot.revokeRole(guild_id, user_id, role_id);
758
- return { success, message: success ? `已撤销用户 ${user_id} 的角色 ${role_id}` : '撤销角色失败' };
759
- },
760
- });
761
- // 获取角色列表工具
762
- this.addTool({
763
- name: 'kook_list_roles',
764
- description: '获取 KOOK 服务器的角色列表',
765
- parameters: {
766
- type: 'object',
767
- properties: {
768
- bot: {
769
- type: 'string',
770
- description: 'Bot 名称',
771
- },
772
- guild_id: {
773
- type: 'string',
774
- description: '服务器 ID',
775
- },
776
- },
777
- required: ['bot', 'guild_id'],
778
- },
779
- platforms: ['kook'],
780
- scopes: ['channel'],
781
- permissionLevel: 'user', // 普通用户可查看
782
- execute: async (args) => {
783
- const { bot: botId, guild_id } = args;
784
- const bot = this.bots.get(botId);
785
- if (!bot)
786
- throw new Error(`Bot ${botId} 不存在`);
787
- const roles = await bot.getRoleList(guild_id);
788
- return {
789
- roles: roles.map(r => ({
790
- id: r.role_id,
791
- name: r.name,
792
- color: r.color,
793
- position: r.position,
794
- permissions: r.permissions,
795
- })),
796
- count: roles.length,
797
- };
798
- },
799
- });
800
- // 创建角色工具
801
- this.addTool({
802
- name: 'kook_create_role',
803
- description: '在 KOOK 服务器中创建新角色(需要服务器主人权限)',
804
- parameters: {
805
- type: 'object',
806
- properties: {
807
- bot: {
808
- type: 'string',
809
- description: 'Bot 名称',
810
- },
811
- guild_id: {
812
- type: 'string',
813
- description: '服务器 ID',
814
- },
815
- name: {
816
- type: 'string',
817
- description: '角色名称',
818
- },
819
- },
820
- required: ['bot', 'guild_id', 'name'],
821
- },
822
- platforms: ['kook'],
823
- scopes: ['channel'],
824
- permissionLevel: 'group_owner',
825
- execute: async (args, context) => {
826
- const { bot: botId, guild_id, name } = args;
827
- const bot = this.bots.get(botId);
828
- if (!bot)
829
- throw new Error(`Bot ${botId} 不存在`);
830
- this.checkPermission(context, 'group_owner');
831
- const role = await bot.createRole(guild_id, name);
832
- return {
833
- success: true,
834
- message: `已创建角色 "${name}"`,
835
- role: {
836
- id: role.role_id,
837
- name: role.name,
838
- },
839
- };
840
- },
841
- });
842
- // 删除角色工具
843
- this.addTool({
844
- name: 'kook_delete_role',
845
- description: '删除 KOOK 服务器中的角色(需要服务器主人权限)',
846
- parameters: {
847
- type: 'object',
848
- properties: {
849
- bot: {
850
- type: 'string',
851
- description: 'Bot 名称',
852
- },
853
- guild_id: {
854
- type: 'string',
855
- description: '服务器 ID',
856
- },
857
- role_id: {
858
- type: 'string',
859
- description: '角色 ID',
860
- },
861
- },
862
- required: ['bot', 'guild_id', 'role_id'],
863
- },
864
- platforms: ['kook'],
865
- scopes: ['channel'],
866
- permissionLevel: 'group_owner',
867
- execute: async (args, context) => {
868
- const { bot: botId, guild_id, role_id } = args;
869
- const bot = this.bots.get(botId);
870
- if (!bot)
871
- throw new Error(`Bot ${botId} 不存在`);
872
- this.checkPermission(context, 'group_owner');
873
- const success = await bot.deleteRole(guild_id, role_id);
874
- return { success, message: success ? `已删除角色 ${role_id}` : '删除角色失败' };
875
- },
876
- });
877
- // 黑名单管理(仅支持 add/remove,通过 GuildMember API)
878
- this.addTool({
879
- name: 'kook_blacklist',
880
- description: 'KOOK 服务器黑名单管理:添加/移除',
881
- parameters: {
882
- type: 'object',
883
- properties: {
884
- bot: { type: 'string', description: 'Bot 名称' },
885
- guild_id: { type: 'string', description: '服务器 ID' },
886
- action: { type: 'string', description: 'add|remove', enum: ['add', 'remove'] },
887
- user_id: { type: 'string', description: '用户 ID' },
888
- remark: { type: 'string', description: '备注(add 可选)' },
889
- },
890
- required: ['bot', 'guild_id', 'action', 'user_id'],
891
- },
892
- platforms: ['kook'],
893
- scopes: ['channel'],
894
- permissionLevel: 'group_admin',
895
- execute: async (args, context) => {
896
- const { bot: botId, guild_id, action, user_id, remark } = args;
897
- const bot = this.bots.get(botId);
898
- if (!bot)
899
- throw new Error(`Bot ${botId} 不存在`);
900
- this.checkPermission(context, 'group_admin');
901
- switch (action) {
902
- case 'add': {
903
- const success = await bot.addToBlacklist(guild_id, user_id, remark);
904
- return { success, message: success ? `已将 ${user_id} 加入黑名单` : '操作失败' };
905
- }
906
- case 'remove': {
907
- const success = await bot.removeFromBlacklist(guild_id, user_id);
908
- return { success, message: success ? `已将 ${user_id} 从黑名单移除` : '操作失败' };
909
- }
910
- default: return { success: false, message: `未知操作: ${action}` };
911
- }
912
- },
913
- });
914
- logger.debug('已注册 KOOK 平台管理工具');
915
- }
916
- /**
917
- * 检查执行上下文中的权限
918
- */
919
- checkPermission(context, required) {
920
- if (!context?.message)
921
- return; // 无上下文时跳过检查(命令行调用)
922
- const sender = context.message.$sender;
923
- if (!sender)
924
- return;
925
- const permissionLevels = {
926
- 'user': 0,
927
- 'group_admin': 1,
928
- 'group_owner': 2,
929
- 'bot_admin': 3,
930
- 'owner': 4,
931
- };
932
- // 获取发送者的实际权限级别
933
- let senderLevel = 'user';
934
- if (sender.isGuildOwner || sender.permission === KookPermission.Owner) {
935
- senderLevel = 'group_owner';
936
- }
937
- else if (sender.isAdmin || sender.permission === KookPermission.Admin ||
938
- sender.permission === KookPermission.ChannelAdmin) {
939
- senderLevel = 'group_admin';
940
- }
941
- // TODO: 检查是否为 bot_admin 或 owner(需要从 zhin 配置中获取)
942
- if (permissionLevels[senderLevel] < permissionLevels[required]) {
943
- throw new Error(`权限不足:需要 ${required} 权限,当前为 ${senderLevel}`);
944
- }
945
- }
946
- }
947
- /**
948
- * 注册 KOOK 适配器
2
+ * KOOK 适配器入口:类型扩展、导出、注册
949
3
  */
4
+ import { usePlugin } from "zhin.js";
5
+ import { KookAdapter } from "./adapter.js";
6
+ export * from "./types.js";
7
+ export { KookBot } from "./bot.js";
8
+ export { KookAdapter } from "./adapter.js";
9
+ const plugin = usePlugin();
10
+ const { provide } = plugin;
950
11
  provide({
951
12
  name: "kook",
952
13
  description: "KOOK Adapter",
@@ -959,5 +20,4 @@ provide({
959
20
  await adapter.stop();
960
21
  },
961
22
  });
962
- logger.debug("KOOK 适配器已加载");
963
23
  //# sourceMappingURL=index.js.map