@alemonjs/kook 0.1.0 → 0.1.1
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 +84 -20
- package/lib/sdk/platform/kook/sdk/wss.js +5 -5
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { defineBot, getConfig, Text, At, OnProcessor, useParse } from 'alemonjs';
|
|
1
|
+
import { defineBot, getConfig, createHash, Text, At, OnProcessor, useParse } from 'alemonjs';
|
|
2
2
|
import 'fs';
|
|
3
3
|
import 'axios';
|
|
4
4
|
import 'path';
|
|
@@ -22,6 +22,65 @@ var index = defineBot(() => {
|
|
|
22
22
|
});
|
|
23
23
|
// 连接
|
|
24
24
|
client.connect();
|
|
25
|
+
client.on('MESSAGES_DIRECT', async (event) => {
|
|
26
|
+
// 过滤机器人
|
|
27
|
+
if (event.extra?.author?.bot)
|
|
28
|
+
return false;
|
|
29
|
+
// 主人
|
|
30
|
+
const master_id = config?.master_id ?? [];
|
|
31
|
+
const isMaster = master_id.includes(event.author_id);
|
|
32
|
+
// 头像
|
|
33
|
+
const avatar = event.extra.author.avatar;
|
|
34
|
+
// 获取消息
|
|
35
|
+
let msg = event.content;
|
|
36
|
+
// 艾特消息处理
|
|
37
|
+
const at_users = [];
|
|
38
|
+
const url = avatar.substring(0, avatar.indexOf('?'));
|
|
39
|
+
const UserAvatar = {
|
|
40
|
+
toBuffer: async () => {
|
|
41
|
+
const arrayBuffer = await fetch(url).then(res => res.arrayBuffer());
|
|
42
|
+
return Buffer.from(arrayBuffer);
|
|
43
|
+
},
|
|
44
|
+
toBase64: async () => {
|
|
45
|
+
const arrayBuffer = await fetch(url).then(res => res.arrayBuffer());
|
|
46
|
+
return Buffer.from(arrayBuffer).toString('base64');
|
|
47
|
+
},
|
|
48
|
+
toURL: async () => {
|
|
49
|
+
return url;
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
const UserKey = createHash(`qq:${event.author_id}`);
|
|
53
|
+
// 定义消
|
|
54
|
+
const e = {
|
|
55
|
+
// 事件类型
|
|
56
|
+
Platform: 'kook',
|
|
57
|
+
// 用户Id
|
|
58
|
+
UserId: event.author_id,
|
|
59
|
+
UserKey,
|
|
60
|
+
UserName: event.extra.author.username,
|
|
61
|
+
UserAvatar: UserAvatar,
|
|
62
|
+
IsMaster: isMaster,
|
|
63
|
+
// message
|
|
64
|
+
MessageId: event.msg_id,
|
|
65
|
+
MessageBody: [
|
|
66
|
+
Text(msg),
|
|
67
|
+
...at_users.map(item => At(item.id, 'user', { name: item.name, avatar: item.avatar, bot: item.bot }))
|
|
68
|
+
],
|
|
69
|
+
MessageText: msg,
|
|
70
|
+
CreateAt: Date.now(),
|
|
71
|
+
//
|
|
72
|
+
tag: 'MESSAGES_PUBLIC',
|
|
73
|
+
value: null
|
|
74
|
+
};
|
|
75
|
+
// 当访问的时候获取
|
|
76
|
+
Object.defineProperty(e, 'value', {
|
|
77
|
+
get() {
|
|
78
|
+
return event;
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
// 处理消息
|
|
82
|
+
OnProcessor(e, 'private.message.create');
|
|
83
|
+
});
|
|
25
84
|
// 监听消息
|
|
26
85
|
client.on('MESSAGES_PUBLIC', async (event) => {
|
|
27
86
|
// 过滤机器人
|
|
@@ -65,40 +124,45 @@ var index = defineBot(() => {
|
|
|
65
124
|
});
|
|
66
125
|
msg = msg.replace(`(met)${item.id}(met)`, '').trim();
|
|
67
126
|
}
|
|
127
|
+
const url = avatar.substring(0, avatar.indexOf('?'));
|
|
128
|
+
const UserAvatar = {
|
|
129
|
+
toBuffer: async () => {
|
|
130
|
+
const arrayBuffer = await fetch(url).then(res => res.arrayBuffer());
|
|
131
|
+
return Buffer.from(arrayBuffer);
|
|
132
|
+
},
|
|
133
|
+
toBase64: async () => {
|
|
134
|
+
const arrayBuffer = await fetch(url).then(res => res.arrayBuffer());
|
|
135
|
+
return Buffer.from(arrayBuffer).toString('base64');
|
|
136
|
+
},
|
|
137
|
+
toURL: async () => {
|
|
138
|
+
return url;
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
const UserKey = createHash(`qq:${event.author_id}`);
|
|
68
142
|
// 定义消
|
|
69
143
|
const e = {
|
|
70
144
|
// 事件类型
|
|
71
145
|
Platform: 'kook',
|
|
72
|
-
//
|
|
146
|
+
//
|
|
73
147
|
GuildId: event.extra.guild_id,
|
|
74
|
-
// 子频道
|
|
75
148
|
ChannelId: event.target_id,
|
|
76
|
-
//
|
|
77
|
-
IsMaster: isMaster,
|
|
78
|
-
// 用户ID
|
|
149
|
+
// 用户Id
|
|
79
150
|
UserId: event.author_id,
|
|
80
|
-
|
|
81
|
-
GuildIdAvatar: '',
|
|
82
|
-
ChannelName: '',
|
|
83
|
-
// 用户名
|
|
151
|
+
UserKey,
|
|
84
152
|
UserName: event.extra.author.username,
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
//
|
|
88
|
-
|
|
89
|
-
// 用户消息
|
|
153
|
+
UserAvatar: UserAvatar,
|
|
154
|
+
IsMaster: isMaster,
|
|
155
|
+
// message
|
|
156
|
+
MessageId: event.msg_id,
|
|
90
157
|
MessageBody: [
|
|
91
158
|
Text(msg),
|
|
92
159
|
...at_users.map(item => At(item.id, 'user', { name: item.name, avatar: item.avatar, bot: item.bot }))
|
|
93
160
|
],
|
|
94
161
|
MessageText: msg,
|
|
95
|
-
|
|
96
|
-
OpenID: data?.code,
|
|
97
|
-
// 创建时间
|
|
162
|
+
OpenId: data?.code,
|
|
98
163
|
CreateAt: Date.now(),
|
|
99
|
-
// 表情
|
|
100
|
-
tag: 'MESSAGES_PUBLIC',
|
|
101
164
|
//
|
|
165
|
+
tag: 'MESSAGES_PUBLIC',
|
|
102
166
|
value: null
|
|
103
167
|
};
|
|
104
168
|
// 当访问的时候获取
|
|
@@ -6,8 +6,8 @@ import { ConversationMap } from './conversation.js';
|
|
|
6
6
|
class KOOKClient extends KOOKAPI {
|
|
7
7
|
// 标记是否已连接
|
|
8
8
|
#isConnected = false;
|
|
9
|
-
// 存储 session
|
|
10
|
-
#
|
|
9
|
+
// 存储 session Id
|
|
10
|
+
#sessionId = null;
|
|
11
11
|
// 存储最新的消息序号
|
|
12
12
|
#lastMessageSN = 0;
|
|
13
13
|
/**
|
|
@@ -86,7 +86,7 @@ class KOOKClient extends KOOKAPI {
|
|
|
86
86
|
1: ({ d }) => {
|
|
87
87
|
if (d && d.code === 0) {
|
|
88
88
|
console.info('[ws] ok');
|
|
89
|
-
this.#
|
|
89
|
+
this.#sessionId = d.session_id;
|
|
90
90
|
this.#isConnected = true;
|
|
91
91
|
}
|
|
92
92
|
else {
|
|
@@ -112,8 +112,8 @@ class KOOKClient extends KOOKAPI {
|
|
|
112
112
|
* 断开当前连接并进行重新连接
|
|
113
113
|
*/
|
|
114
114
|
this.#isConnected = false;
|
|
115
|
-
this.#
|
|
116
|
-
console.info('[ws]
|
|
115
|
+
this.#sessionId = null;
|
|
116
|
+
console.info('[ws] sessionId', this.#sessionId);
|
|
117
117
|
},
|
|
118
118
|
6: () => {
|
|
119
119
|
console.info('[ws] resume ack');
|