@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/CHANGELOG.md +16 -0
- package/lib/adapter.d.ts +34 -0
- package/lib/adapter.d.ts.map +1 -0
- package/lib/adapter.js +332 -0
- package/lib/adapter.js.map +1 -0
- package/lib/bot.d.ts +122 -0
- package/lib/bot.d.ts.map +1 -0
- package/lib/bot.js +607 -0
- package/lib/bot.js.map +1 -0
- package/lib/index.d.ts +4 -178
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +8 -948
- package/lib/index.js.map +1 -1
- package/lib/types.d.ts +32 -0
- package/lib/types.d.ts.map +1 -0
- package/lib/types.js +8 -0
- package/lib/types.js.map +1 -0
- package/package.json +9 -5
- package/skills/kook/SKILL.md +18 -0
- package/src/adapter.ts +360 -0
- package/src/bot.ts +692 -0
- package/src/index.ts +31 -0
- package/src/types.ts +36 -0
package/src/bot.ts
ADDED
|
@@ -0,0 +1,692 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* KOOK Bot 实现
|
|
3
|
+
*/
|
|
4
|
+
import {
|
|
5
|
+
Client,
|
|
6
|
+
PrivateMessageEvent,
|
|
7
|
+
ChannelMessageEvent,
|
|
8
|
+
MessageSegment,
|
|
9
|
+
User,
|
|
10
|
+
Guild,
|
|
11
|
+
GuildMember,
|
|
12
|
+
parseGroupId,
|
|
13
|
+
} from "kook-client";
|
|
14
|
+
import path from "path";
|
|
15
|
+
import {
|
|
16
|
+
Bot,
|
|
17
|
+
Message,
|
|
18
|
+
SendOptions,
|
|
19
|
+
SendContent,
|
|
20
|
+
MessageElement,
|
|
21
|
+
segment,
|
|
22
|
+
MessageType,
|
|
23
|
+
} from "zhin.js";
|
|
24
|
+
import type { KookBotConfig, KookSenderInfo, KookRawMessage } from "./types.js";
|
|
25
|
+
import { KookPermission } from "./types.js";
|
|
26
|
+
import type { KookAdapter } from "./adapter.js";
|
|
27
|
+
|
|
28
|
+
export class KookBot extends Client implements Bot<KookBotConfig, KookRawMessage> {
|
|
29
|
+
$connected: boolean = false;
|
|
30
|
+
adapter: KookAdapter;
|
|
31
|
+
|
|
32
|
+
get $id(): string {
|
|
33
|
+
return this.$config.name;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
get pluginLogger() {
|
|
37
|
+
return this.adapter.plugin.logger;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
constructor(adapter: KookAdapter, public $config: KookBotConfig) {
|
|
41
|
+
super({
|
|
42
|
+
token: $config.token,
|
|
43
|
+
mode: "websocket", // KOOK 默认使用 WebSocket 模式
|
|
44
|
+
data_dir: $config.data_dir || path.join(process.cwd(), "data", "kook"),
|
|
45
|
+
timeout: $config.timeout || 10000,
|
|
46
|
+
max_retry: $config.max_retry || 3,
|
|
47
|
+
ignore: $config.ignore || "bot",
|
|
48
|
+
logLevel: $config.logLevel || "info",
|
|
49
|
+
});
|
|
50
|
+
this.adapter = adapter;
|
|
51
|
+
this.setupEventListeners();
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* 设置事件监听器
|
|
56
|
+
*/
|
|
57
|
+
private setupEventListeners(): void {
|
|
58
|
+
// 监听消息事件
|
|
59
|
+
this.on("message", (msg: KookRawMessage) => {
|
|
60
|
+
try {
|
|
61
|
+
const message = this.$formatMessage(msg);
|
|
62
|
+
this.pluginLogger.debug(`KOOK 格式化消息: $content=${JSON.stringify(message.$content)}, $raw=${message.$raw}`);
|
|
63
|
+
this.adapter.emit("message.receive", message);
|
|
64
|
+
|
|
65
|
+
// 根据消息类型触发特定事件
|
|
66
|
+
const eventMap: Record<MessageType, string> = {
|
|
67
|
+
private: "message.private.receive",
|
|
68
|
+
group: "message.group.receive",
|
|
69
|
+
channel: "message.channel.receive",
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
const specificEvent = eventMap[msg.message_type];
|
|
73
|
+
if (specificEvent) {
|
|
74
|
+
this.adapter.emit(specificEvent as any, message);
|
|
75
|
+
}
|
|
76
|
+
} catch (error) {
|
|
77
|
+
this.pluginLogger.error(`处理 KOOK 消息失败:`, error);
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
// 监听连接事件
|
|
82
|
+
this.on("connect" as any, () => {
|
|
83
|
+
this.$connected = true;
|
|
84
|
+
this.pluginLogger.info(`KOOK Bot ${this.$id} 已连接`);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
// 监听断开事件
|
|
88
|
+
this.on("disconnect" as any, () => {
|
|
89
|
+
this.$connected = false;
|
|
90
|
+
this.pluginLogger.warn(`KOOK Bot ${this.$id} 已断开`);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
// 监听错误事件
|
|
94
|
+
this.on("error" as any, (error: Error) => {
|
|
95
|
+
this.pluginLogger.error(`KOOK Bot ${this.$id} 错误:`, error);
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* 将 KOOK 消息转换为标准消息格式
|
|
101
|
+
*/
|
|
102
|
+
$formatMessage(msg: KookRawMessage): Message<KookRawMessage> {
|
|
103
|
+
const channelId = msg.message_type === "channel"
|
|
104
|
+
? (msg as ChannelMessageEvent).channel_id
|
|
105
|
+
: msg.author_id;
|
|
106
|
+
|
|
107
|
+
// 获取发送者的权限信息
|
|
108
|
+
const senderInfo = this.getSenderInfo(msg);
|
|
109
|
+
|
|
110
|
+
// 频道消息需要获取 guild_id
|
|
111
|
+
let guildId: string | undefined;
|
|
112
|
+
if (msg.message_type === "channel") {
|
|
113
|
+
const channelMsg = msg as ChannelMessageEvent;
|
|
114
|
+
// 尝试从 channel 获取 guild_id
|
|
115
|
+
try {
|
|
116
|
+
const channel = channelMsg.channel;
|
|
117
|
+
guildId = (channel?.info as any)?.guild_id;
|
|
118
|
+
} catch {
|
|
119
|
+
// 如果获取失败,尝试从缓存中查找
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const message: Message<KookRawMessage> = Message.from(msg, {
|
|
124
|
+
$id: msg.message_id.toString(),
|
|
125
|
+
$adapter: "kook" as const,
|
|
126
|
+
$bot: this.$id,
|
|
127
|
+
|
|
128
|
+
$sender: senderInfo,
|
|
129
|
+
|
|
130
|
+
$channel: {
|
|
131
|
+
id: channelId.toString(),
|
|
132
|
+
type: msg.message_type === "channel" ? "channel" : "private",
|
|
133
|
+
// 频道消息包含服务器ID
|
|
134
|
+
...(guildId ? { guild_id: guildId } : {}),
|
|
135
|
+
},
|
|
136
|
+
|
|
137
|
+
$content: this.parseMessageContent(msg.message),
|
|
138
|
+
$raw: msg.raw_message,
|
|
139
|
+
$timestamp: msg.timestamp,
|
|
140
|
+
|
|
141
|
+
$recall: async () => {
|
|
142
|
+
await this.$recallMessage(message.$id);
|
|
143
|
+
},
|
|
144
|
+
|
|
145
|
+
$reply: async (content: SendContent, quote?: string | boolean): Promise<string> => {
|
|
146
|
+
const elements = Array.isArray(content) ? content : [content];
|
|
147
|
+
const finalContent: MessageElement[] = [];
|
|
148
|
+
|
|
149
|
+
if (quote) {
|
|
150
|
+
finalContent.push({
|
|
151
|
+
type: "reply",
|
|
152
|
+
data: {
|
|
153
|
+
id: typeof quote === "boolean" ? message.$id : quote,
|
|
154
|
+
},
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
finalContent.push(...elements.map(el =>
|
|
159
|
+
typeof el === 'string' ? { type: 'text' as const, data: { text: el } } : el
|
|
160
|
+
));
|
|
161
|
+
|
|
162
|
+
return await this.adapter.sendMessage({
|
|
163
|
+
...message.$channel,
|
|
164
|
+
context: "kook",
|
|
165
|
+
bot: this.$id,
|
|
166
|
+
content: finalContent,
|
|
167
|
+
});
|
|
168
|
+
},
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
return message;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* 获取发送者的详细权限信息
|
|
176
|
+
*/
|
|
177
|
+
private getSenderInfo(msg: KookRawMessage): KookSenderInfo {
|
|
178
|
+
const authorInfo = msg.author?.info;
|
|
179
|
+
const senderInfo: KookSenderInfo = {
|
|
180
|
+
id: msg.author_id.toString(),
|
|
181
|
+
name: authorInfo?.nickname || authorInfo?.username || "未知用户",
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
// 频道消息才有权限信息
|
|
185
|
+
if (msg.message_type === "channel") {
|
|
186
|
+
const channelMsg = msg as ChannelMessageEvent;
|
|
187
|
+
|
|
188
|
+
// 从 author.info 中获取权限信息(如果 kook-client 提供)
|
|
189
|
+
if (authorInfo) {
|
|
190
|
+
// 尝试获取角色列表
|
|
191
|
+
senderInfo.roles = (authorInfo as any).roles || [];
|
|
192
|
+
|
|
193
|
+
// 尝试获取 guild_id 并检查是否为服务器主人
|
|
194
|
+
try {
|
|
195
|
+
const channel = channelMsg.channel;
|
|
196
|
+
const guildId = (channel?.info as any)?.guild_id;
|
|
197
|
+
if (guildId) {
|
|
198
|
+
const guildInfo = this.guilds?.get(guildId);
|
|
199
|
+
if (guildInfo) {
|
|
200
|
+
senderInfo.isGuildOwner = guildInfo.user_id === msg.author_id;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
} catch {
|
|
204
|
+
// 忽略获取失败
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// 根据 permission 字段判断(如果有)
|
|
208
|
+
const permission = (authorInfo as any).permission as KookPermission | undefined;
|
|
209
|
+
if (permission !== undefined) {
|
|
210
|
+
senderInfo.permission = permission;
|
|
211
|
+
senderInfo.isAdmin = permission === KookPermission.Admin ||
|
|
212
|
+
permission === KookPermission.Owner ||
|
|
213
|
+
permission === KookPermission.ChannelAdmin;
|
|
214
|
+
senderInfo.isGuildOwner = senderInfo.isGuildOwner || permission === KookPermission.Owner;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
return senderInfo;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// ==================== 频道管理 API ====================
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* 踢出用户
|
|
226
|
+
* @param guildId 服务器ID
|
|
227
|
+
* @param userId 用户ID
|
|
228
|
+
*/
|
|
229
|
+
async kickUser(guildId: string, userId: string): Promise<boolean> {
|
|
230
|
+
try {
|
|
231
|
+
const guild = this.pickGuild(guildId);
|
|
232
|
+
const result = await guild.kick(userId);
|
|
233
|
+
this.pluginLogger.info(`KOOK Bot ${this.$id} 踢出用户 ${userId} 从服务器 ${guildId}`);
|
|
234
|
+
return result;
|
|
235
|
+
} catch (error) {
|
|
236
|
+
this.pluginLogger.error(`KOOK Bot ${this.$id} 踢出用户失败:`, error);
|
|
237
|
+
throw error;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* 将用户加入黑名单
|
|
243
|
+
* @param guildId 服务器ID
|
|
244
|
+
* @param userId 用户ID
|
|
245
|
+
* @param remark 备注
|
|
246
|
+
* @param delMsgDays 删除消息天数(0-7)
|
|
247
|
+
*/
|
|
248
|
+
async addToBlacklist(guildId: string, userId: string, remark?: string, delMsgDays?: number): Promise<boolean> {
|
|
249
|
+
try {
|
|
250
|
+
const member = this.pickGuildMember(guildId, userId);
|
|
251
|
+
const result = await member.addToBlackList(remark, delMsgDays);
|
|
252
|
+
this.pluginLogger.info(`KOOK Bot ${this.$id} 将用户 ${userId} 加入黑名单(服务器 ${guildId})`);
|
|
253
|
+
return result;
|
|
254
|
+
} catch (error) {
|
|
255
|
+
this.pluginLogger.error(`KOOK Bot ${this.$id} 加入黑名单失败:`, error);
|
|
256
|
+
throw error;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* 将用户从黑名单移除
|
|
262
|
+
* @param guildId 服务器ID
|
|
263
|
+
* @param userId 用户ID
|
|
264
|
+
*/
|
|
265
|
+
async removeFromBlacklist(guildId: string, userId: string): Promise<boolean> {
|
|
266
|
+
try {
|
|
267
|
+
const member = this.pickGuildMember(guildId, userId);
|
|
268
|
+
const result = await member.removeFromBlackList();
|
|
269
|
+
this.pluginLogger.info(`KOOK Bot ${this.$id} 将用户 ${userId} 从黑名单移除(服务器 ${guildId})`);
|
|
270
|
+
return result;
|
|
271
|
+
} catch (error) {
|
|
272
|
+
this.pluginLogger.error(`KOOK Bot ${this.$id} 移除黑名单失败:`, error);
|
|
273
|
+
throw error;
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* 给用户授予角色
|
|
279
|
+
* @param guildId 服务器ID
|
|
280
|
+
* @param userId 用户ID
|
|
281
|
+
* @param roleId 角色ID
|
|
282
|
+
*/
|
|
283
|
+
async grantRole(guildId: string, userId: string, roleId: string): Promise<boolean> {
|
|
284
|
+
try {
|
|
285
|
+
const member = this.pickGuildMember(guildId, userId);
|
|
286
|
+
const result = await member.grant(roleId);
|
|
287
|
+
this.pluginLogger.info(`KOOK Bot ${this.$id} 授予用户 ${userId} 角色 ${roleId}(服务器 ${guildId})`);
|
|
288
|
+
return result;
|
|
289
|
+
} catch (error) {
|
|
290
|
+
this.pluginLogger.error(`KOOK Bot ${this.$id} 授予角色失败:`, error);
|
|
291
|
+
throw error;
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* 撤销用户角色
|
|
297
|
+
* @param guildId 服务器ID
|
|
298
|
+
* @param userId 用户ID
|
|
299
|
+
* @param roleId 角色ID
|
|
300
|
+
*/
|
|
301
|
+
async revokeRole(guildId: string, userId: string, roleId: string): Promise<boolean> {
|
|
302
|
+
try {
|
|
303
|
+
const member = this.pickGuildMember(guildId, userId);
|
|
304
|
+
const result = await member.revoke(roleId);
|
|
305
|
+
this.pluginLogger.info(`KOOK Bot ${this.$id} 撤销用户 ${userId} 角色 ${roleId}(服务器 ${guildId})`);
|
|
306
|
+
return result;
|
|
307
|
+
} catch (error) {
|
|
308
|
+
this.pluginLogger.error(`KOOK Bot ${this.$id} 撤销角色失败:`, error);
|
|
309
|
+
throw error;
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
* 设置用户昵称
|
|
315
|
+
* @param guildId 服务器ID
|
|
316
|
+
* @param userId 用户ID
|
|
317
|
+
* @param nickname 新昵称
|
|
318
|
+
*/
|
|
319
|
+
async setNickname(guildId: string, userId: string, nickname: string): Promise<boolean> {
|
|
320
|
+
try {
|
|
321
|
+
const member = this.pickGuildMember(guildId, userId);
|
|
322
|
+
const result = await member.setNickname(nickname);
|
|
323
|
+
this.pluginLogger.info(`KOOK Bot ${this.$id} 设置用户 ${userId} 昵称为 "${nickname}"(服务器 ${guildId})`);
|
|
324
|
+
return result;
|
|
325
|
+
} catch (error) {
|
|
326
|
+
this.pluginLogger.error(`KOOK Bot ${this.$id} 设置昵称失败:`, error);
|
|
327
|
+
throw error;
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
/**
|
|
332
|
+
* 获取服务器角色列表
|
|
333
|
+
* @param guildId 服务器ID
|
|
334
|
+
*/
|
|
335
|
+
async getRoleList(guildId: string): Promise<Guild.Role[]> {
|
|
336
|
+
try {
|
|
337
|
+
const guild = this.pickGuild(guildId);
|
|
338
|
+
return await guild.getRoleList();
|
|
339
|
+
} catch (error) {
|
|
340
|
+
this.pluginLogger.error(`KOOK Bot ${this.$id} 获取角色列表失败:`, error);
|
|
341
|
+
throw error;
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
/**
|
|
346
|
+
* 创建角色
|
|
347
|
+
* @param guildId 服务器ID
|
|
348
|
+
* @param name 角色名称
|
|
349
|
+
*/
|
|
350
|
+
async createRole(guildId: string, name: string): Promise<Guild.Role> {
|
|
351
|
+
try {
|
|
352
|
+
const guild = this.pickGuild(guildId);
|
|
353
|
+
const role = await guild.createRole(name);
|
|
354
|
+
this.pluginLogger.info(`KOOK Bot ${this.$id} 创建角色 "${name}"(服务器 ${guildId})`);
|
|
355
|
+
return role;
|
|
356
|
+
} catch (error) {
|
|
357
|
+
this.pluginLogger.error(`KOOK Bot ${this.$id} 创建角色失败:`, error);
|
|
358
|
+
throw error;
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
/**
|
|
363
|
+
* 删除角色
|
|
364
|
+
* @param guildId 服务器ID
|
|
365
|
+
* @param roleId 角色ID
|
|
366
|
+
*/
|
|
367
|
+
async deleteRole(guildId: string, roleId: string): Promise<boolean> {
|
|
368
|
+
try {
|
|
369
|
+
const guild = this.pickGuild(guildId);
|
|
370
|
+
const result = await guild.deleteRole(roleId);
|
|
371
|
+
this.pluginLogger.info(`KOOK Bot ${this.$id} 删除角色 ${roleId}(服务器 ${guildId})`);
|
|
372
|
+
return result;
|
|
373
|
+
} catch (error) {
|
|
374
|
+
this.pluginLogger.error(`KOOK Bot ${this.$id} 删除角色失败:`, error);
|
|
375
|
+
throw error;
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
/**
|
|
380
|
+
* 获取服务器成员列表
|
|
381
|
+
* @param guildId 服务器ID
|
|
382
|
+
* @param channelId 可选的频道ID
|
|
383
|
+
*/
|
|
384
|
+
async getGuildMembers(guildId: string, channelId?: string): Promise<User.Info[]> {
|
|
385
|
+
try {
|
|
386
|
+
return await this.getGuildUserList(guildId, channelId);
|
|
387
|
+
} catch (error) {
|
|
388
|
+
this.pluginLogger.error(`KOOK Bot ${this.$id} 获取成员列表失败:`, error);
|
|
389
|
+
throw error;
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
private parseMarkdown(content: string): MessageElement[] {
|
|
393
|
+
const elements: MessageElement[] = [];
|
|
394
|
+
|
|
395
|
+
// KMarkdown 图片格式: 
|
|
396
|
+
const imageRegex = /!\[([^\]]*)\]\(([^)]+)\)/g;
|
|
397
|
+
// KMarkdown @提及格式: (met)userId(met) 或 @用户名
|
|
398
|
+
const mentionRegex = /\(met\)(\d+)\(met\)|@([^\s]+)/g;
|
|
399
|
+
// KMarkdown 表情格式: (emj)表情名(emj)[表情ID]
|
|
400
|
+
const emojiRegex = /\(emj\)([^(]+)\(emj\)\[([^\]]+)\]/g;
|
|
401
|
+
// KMarkdown 频道格式: (chn)channelId(chn)
|
|
402
|
+
const channelRegex = /\(chn\)(\d+)\(chn\)/g;
|
|
403
|
+
|
|
404
|
+
let lastIndex = 0;
|
|
405
|
+
const matches: Array<{ index: number; length: number; element: MessageElement }> = [];
|
|
406
|
+
|
|
407
|
+
// 解析图片
|
|
408
|
+
let match: RegExpExecArray | null;
|
|
409
|
+
while ((match = imageRegex.exec(content)) !== null) {
|
|
410
|
+
matches.push({
|
|
411
|
+
index: match.index,
|
|
412
|
+
length: match[0].length,
|
|
413
|
+
element: { type: "image", data: { url: match[2], alt: match[1] } }
|
|
414
|
+
});
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
// 解析 @提及
|
|
418
|
+
while ((match = mentionRegex.exec(content)) !== null) {
|
|
419
|
+
const userId = match[1] || match[2];
|
|
420
|
+
matches.push({
|
|
421
|
+
index: match.index,
|
|
422
|
+
length: match[0].length,
|
|
423
|
+
element: { type: "at", data: { id: userId } }
|
|
424
|
+
});
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
// 解析表情
|
|
428
|
+
while ((match = emojiRegex.exec(content)) !== null) {
|
|
429
|
+
matches.push({
|
|
430
|
+
index: match.index,
|
|
431
|
+
length: match[0].length,
|
|
432
|
+
element: { type: "face", data: { id: match[2], name: match[1] } }
|
|
433
|
+
});
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
// 解析频道引用
|
|
437
|
+
while ((match = channelRegex.exec(content)) !== null) {
|
|
438
|
+
matches.push({
|
|
439
|
+
index: match.index,
|
|
440
|
+
length: match[0].length,
|
|
441
|
+
element: { type: "text", data: { text: `#频道:${match[1]}` } }
|
|
442
|
+
});
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
// 按位置排序
|
|
446
|
+
matches.sort((a, b) => a.index - b.index);
|
|
447
|
+
|
|
448
|
+
// 组装消息段
|
|
449
|
+
for (const match of matches) {
|
|
450
|
+
// 添加之前的文本
|
|
451
|
+
if (match.index > lastIndex) {
|
|
452
|
+
const text = content.slice(lastIndex, match.index);
|
|
453
|
+
if (text) {
|
|
454
|
+
elements.push({ type: "text", data: { text } });
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
// 添加特殊元素
|
|
459
|
+
elements.push(match.element);
|
|
460
|
+
lastIndex = match.index + match.length;
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
// 添加剩余文本
|
|
464
|
+
if (lastIndex < content.length) {
|
|
465
|
+
const text = content.slice(lastIndex);
|
|
466
|
+
if (text) {
|
|
467
|
+
elements.push({ type: "text", data: { text } });
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
// 如果没有解析到任何特殊元素,返回纯文本
|
|
472
|
+
if (elements.length === 0) {
|
|
473
|
+
elements.push({ type: "text", data: { text: content } });
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
return elements;
|
|
477
|
+
}
|
|
478
|
+
/**
|
|
479
|
+
* 将 kook-client 的 MessageSegment[] 转换为 Zhin 的 MessageElement[]
|
|
480
|
+
*/
|
|
481
|
+
private parseMessageContent(segments: MessageSegment[]): MessageElement[] {
|
|
482
|
+
const elements: MessageElement[] = [];
|
|
483
|
+
|
|
484
|
+
for (const segment of segments) {
|
|
485
|
+
switch (segment.type) {
|
|
486
|
+
case "markdown":
|
|
487
|
+
// 检查是否包含特殊语法,如果是纯文本则直接转换
|
|
488
|
+
if (this.hasKMarkdownSyntax(segment.text)) {
|
|
489
|
+
elements.push(...this.parseMarkdown(segment.text));
|
|
490
|
+
} else {
|
|
491
|
+
elements.push({ type: "text", data: { text: segment.text } });
|
|
492
|
+
}
|
|
493
|
+
break;
|
|
494
|
+
|
|
495
|
+
case "text":
|
|
496
|
+
elements.push({ type: "text", data: { text: segment.text } });
|
|
497
|
+
break;
|
|
498
|
+
|
|
499
|
+
case "at":
|
|
500
|
+
elements.push({ type: "at", data: { id: segment.user_id } });
|
|
501
|
+
break;
|
|
502
|
+
|
|
503
|
+
case "image":
|
|
504
|
+
elements.push({
|
|
505
|
+
type: "image",
|
|
506
|
+
data: { url: segment.url, alt: segment.title || "图片" }
|
|
507
|
+
});
|
|
508
|
+
break;
|
|
509
|
+
|
|
510
|
+
case "video":
|
|
511
|
+
elements.push({ type: "video", data: { url: segment.url } });
|
|
512
|
+
break;
|
|
513
|
+
|
|
514
|
+
case "audio":
|
|
515
|
+
elements.push({ type: "audio", data: { url: segment.url } });
|
|
516
|
+
break;
|
|
517
|
+
|
|
518
|
+
case "file":
|
|
519
|
+
elements.push({
|
|
520
|
+
type: "file",
|
|
521
|
+
data: { url: segment.url, name: segment.name }
|
|
522
|
+
});
|
|
523
|
+
break;
|
|
524
|
+
|
|
525
|
+
case "reply":
|
|
526
|
+
elements.push({ type: "reply", data: { id: segment.id } });
|
|
527
|
+
break;
|
|
528
|
+
|
|
529
|
+
case "card":
|
|
530
|
+
// Card 消息暂不支持,转为提示文本
|
|
531
|
+
elements.push({ type: "text", data: { text: "[卡片消息]" } });
|
|
532
|
+
break;
|
|
533
|
+
|
|
534
|
+
default:
|
|
535
|
+
this.pluginLogger.warn(`未知的 KOOK 消息段类型: ${(segment as any).type}`);
|
|
536
|
+
break;
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
return elements.length > 0 ? elements : [{ type: "text", data: { text: "" } }];
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
/**
|
|
544
|
+
* 检查文本是否包含 KMarkdown 特殊语法
|
|
545
|
+
*/
|
|
546
|
+
private hasKMarkdownSyntax(text: string): boolean {
|
|
547
|
+
return /!\[.*?\]\(.*?\)|\(met\)|\(emj\)|\(chn\)|@\S+/.test(text);
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
/**
|
|
551
|
+
* 连接到 KOOK
|
|
552
|
+
*/
|
|
553
|
+
async $connect(): Promise<void> {
|
|
554
|
+
try {
|
|
555
|
+
await this.connect();
|
|
556
|
+
this.$connected = true;
|
|
557
|
+
this.pluginLogger.info(`KOOK Bot ${this.$id} 连接成功`);
|
|
558
|
+
} catch (error) {
|
|
559
|
+
this.pluginLogger.error(`KOOK Bot ${this.$id} 连接失败:`, error);
|
|
560
|
+
throw error;
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
/**
|
|
565
|
+
* 断开连接
|
|
566
|
+
*/
|
|
567
|
+
async $disconnect(): Promise<void> {
|
|
568
|
+
try {
|
|
569
|
+
await this.disconnect();
|
|
570
|
+
this.$connected = false;
|
|
571
|
+
this.pluginLogger.info(`KOOK Bot ${this.$id} 已断开连接`);
|
|
572
|
+
} catch (error) {
|
|
573
|
+
this.pluginLogger.error(`KOOK Bot ${this.$id} 断开连接失败:`, error);
|
|
574
|
+
throw error;
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
/**
|
|
579
|
+
* 发送消息
|
|
580
|
+
*/
|
|
581
|
+
async $sendMessage(options: SendOptions): Promise<string> {
|
|
582
|
+
try {
|
|
583
|
+
const { id, type, content } = options;
|
|
584
|
+
|
|
585
|
+
// 将消息段转换为 KOOK 格式
|
|
586
|
+
const elements: MessageElement[] = Array.isArray(content)
|
|
587
|
+
? content.map(el => typeof el === 'string' ? { type: 'text' as const, data: { text: el } } : el)
|
|
588
|
+
: [typeof content === 'string' ? { type: 'text' as const, data: { text: content } } : content];
|
|
589
|
+
|
|
590
|
+
const kookContent = this.convertToKookFormat(elements);
|
|
591
|
+
|
|
592
|
+
// 根据消息类型发送
|
|
593
|
+
let result: any;
|
|
594
|
+
if (type === "private") {
|
|
595
|
+
result = await (this as any).sendPrivateMsg(id, kookContent);
|
|
596
|
+
} else {
|
|
597
|
+
result = await (this as any).sendChannelMsg(id, kookContent);
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
return result?.msg_id || "";
|
|
601
|
+
} catch (error) {
|
|
602
|
+
this.pluginLogger.error(`KOOK Bot ${this.$id} 发送消息失败:`, error);
|
|
603
|
+
throw error;
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
/**
|
|
608
|
+
* 撤回消息
|
|
609
|
+
*/
|
|
610
|
+
async $recallMessage(messageId: string): Promise<void> {
|
|
611
|
+
try {
|
|
612
|
+
await (this as any).deleteMsg(messageId);
|
|
613
|
+
this.pluginLogger.debug(`KOOK Bot ${this.$id} 撤回消息: ${messageId}`);
|
|
614
|
+
} catch (error) {
|
|
615
|
+
this.pluginLogger.error(`KOOK Bot ${this.$id} 撤回消息失败:`, error);
|
|
616
|
+
throw error;
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
/**
|
|
621
|
+
* 将消息段转换为 KOOK KMarkdown 格式
|
|
622
|
+
* 支持:文本、图片、@提及、表情、引用等
|
|
623
|
+
*/
|
|
624
|
+
private convertToKookFormat(content: MessageElement[]): string {
|
|
625
|
+
return content
|
|
626
|
+
.map((el) => {
|
|
627
|
+
switch (el.type) {
|
|
628
|
+
case "text":
|
|
629
|
+
// 纯文本,转义特殊字符
|
|
630
|
+
return el.data.text.replace(/[\\`*_{}[\]()#+\-.!]/g, '\\$&');
|
|
631
|
+
|
|
632
|
+
case "image":
|
|
633
|
+
// 图片:
|
|
634
|
+
return ``;
|
|
635
|
+
|
|
636
|
+
case "at":
|
|
637
|
+
// @提及:(met)userId(met) 或 @all
|
|
638
|
+
if (el.data.id === "all") {
|
|
639
|
+
return "(met)all(met)";
|
|
640
|
+
}
|
|
641
|
+
return `(met)${el.data.id}(met)`;
|
|
642
|
+
|
|
643
|
+
case "face":
|
|
644
|
+
// 表情:(emj)表情名(emj)[表情ID]
|
|
645
|
+
return `(emj)${el.data.name || 'emoji'}(emj)[${el.data.id}]`;
|
|
646
|
+
|
|
647
|
+
case "reply":
|
|
648
|
+
// 引用消息(KOOK 使用 quote 参数,不在消息内容中)
|
|
649
|
+
return "";
|
|
650
|
+
|
|
651
|
+
case "video":
|
|
652
|
+
// 视频:使用链接形式
|
|
653
|
+
return `[视频](${el.data.url || el.data.file})`;
|
|
654
|
+
|
|
655
|
+
case "audio":
|
|
656
|
+
// 音频:使用链接形式
|
|
657
|
+
return `[音频](${el.data.url || el.data.file})`;
|
|
658
|
+
|
|
659
|
+
case "file":
|
|
660
|
+
// 文件:使用链接形式
|
|
661
|
+
return `[文件: ${el.data.name || '未命名'}](${el.data.url || el.data.file})`;
|
|
662
|
+
|
|
663
|
+
case "link":
|
|
664
|
+
// 链接:[文本](url)
|
|
665
|
+
return `[${el.data.text || el.data.url}](${el.data.url})`;
|
|
666
|
+
|
|
667
|
+
case "bold":
|
|
668
|
+
// 粗体:**文本**
|
|
669
|
+
return `**${el.data.text}**`;
|
|
670
|
+
|
|
671
|
+
case "italic":
|
|
672
|
+
// 斜体:*文本*
|
|
673
|
+
return `*${el.data.text}*`;
|
|
674
|
+
|
|
675
|
+
case "code":
|
|
676
|
+
// 行内代码:`代码`
|
|
677
|
+
return `\`${el.data.text}\``;
|
|
678
|
+
|
|
679
|
+
case "code_block":
|
|
680
|
+
// 代码块:```语言\n代码\n```
|
|
681
|
+
return `\`\`\`${el.data.language || ''}\n${el.data.text}\n\`\`\``;
|
|
682
|
+
|
|
683
|
+
default:
|
|
684
|
+
// 未知类型,尝试转换为文本
|
|
685
|
+
this.pluginLogger.warn(`未知的消息段类型: ${el.type}`);
|
|
686
|
+
return el.data.text || JSON.stringify(el.data);
|
|
687
|
+
}
|
|
688
|
+
})
|
|
689
|
+
.filter(Boolean)
|
|
690
|
+
.join("");
|
|
691
|
+
}
|
|
692
|
+
}
|