@onebots/adapter-kook 1.0.0 → 1.0.5

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/adapter.js CHANGED
@@ -27,7 +27,8 @@ export class KookAdapter extends Adapter {
27
27
  if (!account)
28
28
  throw new Error(`Account ${uin} not found`);
29
29
  const bot = account.client;
30
- const { scene_id, scene_type, message } = params;
30
+ const { scene_type, message } = params;
31
+ const sceneId = this.coerceId(params.scene_id);
31
32
  // 解析消息内容
32
33
  let content = '';
33
34
  let messageType = 9; // 默认使用 KMarkdown
@@ -63,15 +64,15 @@ export class KookAdapter extends Adapter {
63
64
  let result;
64
65
  if (scene_type === 'private' || scene_type === 'direct') {
65
66
  // 私聊消息
66
- result = await bot.sendDirectMessage(scene_id.string, content);
67
+ result = await bot.sendDirectMessage(sceneId.string, content);
67
68
  }
68
69
  else if (scene_type === 'channel') {
69
70
  // 频道消息
70
- result = await bot.sendChannelMessage(scene_id.string, content);
71
+ result = await bot.sendChannelMessage(sceneId.string, content);
71
72
  }
72
73
  else if (scene_type === 'group') {
73
74
  // 群组消息也发送到频道
74
- result = await bot.sendChannelMessage(scene_id.string, content);
75
+ result = await bot.sendChannelMessage(sceneId.string, content);
75
76
  }
76
77
  else {
77
78
  throw new Error(`KOOK 不支持的消息场景类型: ${scene_type}`);
@@ -88,11 +89,11 @@ export class KookAdapter extends Adapter {
88
89
  if (!account)
89
90
  throw new Error(`Account ${uin} not found`);
90
91
  const bot = account.client;
91
- const msgId = params.message_id.string;
92
+ const msgId = this.coerceId(params.message_id).string;
92
93
  // 根据场景类型删除消息
93
94
  // 需要从消息中获取 channel_id,这里简化处理
94
95
  // 实际应该从消息缓存或数据库中获取
95
- const channelId = params.scene_id?.string || '';
96
+ const channelId = params.scene_id != null ? this.coerceId(params.scene_id).string : '';
96
97
  if (channelId) {
97
98
  await bot.deleteMessage(channelId, msgId);
98
99
  }
@@ -105,8 +106,8 @@ export class KookAdapter extends Adapter {
105
106
  if (!account)
106
107
  throw new Error(`Account ${uin} not found`);
107
108
  const bot = account.client;
108
- const msgId = params.message_id.string;
109
- const channelId = params.scene_id?.string || '';
109
+ const msgId = this.coerceId(params.message_id).string;
110
+ const channelId = params.scene_id != null ? this.coerceId(params.scene_id).string : '';
110
111
  const msg = await bot.getMessage(channelId, msgId);
111
112
  return {
112
113
  message_id: this.createId(msg.id),
@@ -132,7 +133,7 @@ export class KookAdapter extends Adapter {
132
133
  if (!account)
133
134
  throw new Error(`Account ${uin} not found`);
134
135
  const bot = account.client;
135
- const msgId = params.message_id.string;
136
+ const msgId = this.coerceId(params.message_id).string;
136
137
  // 解析消息内容
137
138
  let content = '';
138
139
  for (const seg of params.message) {
@@ -145,7 +146,8 @@ export class KookAdapter extends Adapter {
145
146
  }
146
147
  // 更新消息需要 channelId,但参数中没有,尝试从消息中获取
147
148
  // 如果无法获取,则使用第一个可用的频道(简化处理)
148
- const channelId = params.scene_id?.string || '';
149
+ const rawScene = params.scene_id;
150
+ const channelId = rawScene != null ? this.coerceId(rawScene).string : '';
149
151
  if (!channelId) {
150
152
  throw new Error('更新消息需要 channel_id,但参数中未提供');
151
153
  }
@@ -258,6 +260,9 @@ export class KookAdapter extends Adapter {
258
260
  const bot = account.client;
259
261
  const guildId = params.group_id.string;
260
262
  const guild = await bot.getGuild(guildId);
263
+ if (!guild?.id) {
264
+ throw new Error(`获取群信息失败:无效的 guild 响应(guild_id=${guildId})`);
265
+ }
261
266
  return {
262
267
  group_id: this.createId(guild.id),
263
268
  group_name: guild.name,
@@ -476,6 +481,9 @@ export class KookAdapter extends Adapter {
476
481
  throw new Error(`Account ${uin} not found`);
477
482
  const bot = account.client;
478
483
  const guild = await bot.getGuild(params.guild_id.string);
484
+ if (!guild?.id) {
485
+ throw new Error(`获取服务器信息失败:无效的 guild 响应(guild_id=${params.guild_id.string})`);
486
+ }
479
487
  return {
480
488
  guild_id: this.createId(guild.id),
481
489
  guild_name: guild.name,
package/lib/index.js CHANGED
@@ -1,3 +1,14 @@
1
+ // 导出类型
2
+ import { AdapterRegistry } from 'onebots';
1
3
  export * from './adapter.js';
2
4
  export * from './bot.js';
5
+ const kookSchema = {
6
+ account_id: { type: 'string', required: true, label: '账号标识' },
7
+ token: { type: 'string', required: true, label: 'Bot Token' },
8
+ verifyToken: { type: 'string', label: 'Webhook 验证 Token' },
9
+ encryptKey: { type: 'string', label: '消息加密 Key' },
10
+ mode: { type: 'string', enum: ['webhook', 'websocket'], default: 'websocket', label: '连接模式' },
11
+ maxRetry: { type: 'number', default: 10, label: '最大重连次数' },
12
+ };
13
+ AdapterRegistry.registerSchema('kook', kookSchema);
3
14
  //# sourceMappingURL=index.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onebots/adapter-kook",
3
- "version": "1.0.0",
3
+ "version": "1.0.5",
4
4
  "description": "onebots KOOK(开黑了)适配器",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",
@@ -22,16 +22,20 @@
22
22
  "/lib/**/*.d.ts"
23
23
  ],
24
24
  "devDependencies": {
25
+ "@types/node": "^22.7.3",
25
26
  "tsc-alias": "latest",
26
- "typescript": "latest",
27
- "@types/node": "^22.7.3"
27
+ "typescript": "latest"
28
28
  },
29
29
  "peerDependencies": {
30
- "onebots": "1.0.0"
30
+ "onebots": "1.0.5"
31
31
  },
32
32
  "dependencies": {
33
33
  "kook-client": "^1.0.4"
34
34
  },
35
+ "repository": {
36
+ "type": "git",
37
+ "url": "git+https://github.com/lc-cn/onebots.git"
38
+ },
35
39
  "scripts": {
36
40
  "build": "rm -f *.tsbuildinfo && tsc --project tsconfig.json && tsc-alias -p tsconfig.json",
37
41
  "clean": "rm -rf lib *.tsbuildinfo"