@alemonjs/kook 0.0.3 → 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.d.ts CHANGED
@@ -1,3 +1,7 @@
1
+ import { KOOKClient } from './sdk/platform/kook/sdk/wss.js';
2
+
3
+ type Client = typeof KOOKClient.prototype;
4
+ declare const client: Client;
1
5
  declare const _default: () => typeof global.alemonjs;
2
6
 
3
- export { _default as default };
7
+ export { type Client, client, _default as default };
package/lib/index.js CHANGED
@@ -1,6 +1,16 @@
1
- import { defineBot, getConfig, Text, At, OnProcessor, useParse } from 'alemonjs';
2
- import { KOOKClient } from 'chat-space';
1
+ import { defineBot, getConfig, createHash, Text, At, OnProcessor, useParse } from 'alemonjs';
2
+ import 'fs';
3
+ import 'axios';
4
+ import 'path';
5
+ import 'qrcode';
6
+ import 'public-ip';
7
+ import 'stream';
8
+ import 'file-type';
9
+ import 'form-data';
10
+ import './sdk/platform/kook/sdk/typings.js';
11
+ import { KOOKClient } from './sdk/platform/kook/sdk/wss.js';
3
12
 
13
+ const client = global.client;
4
14
  var index = defineBot(() => {
5
15
  const cfg = getConfig();
6
16
  const config = cfg.value?.kook;
@@ -12,6 +22,65 @@ var index = defineBot(() => {
12
22
  });
13
23
  // 连接
14
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
+ });
15
84
  // 监听消息
16
85
  client.on('MESSAGES_PUBLIC', async (event) => {
17
86
  // 过滤机器人
@@ -55,34 +124,45 @@ var index = defineBot(() => {
55
124
  });
56
125
  msg = msg.replace(`(met)${item.id}(met)`, '').trim();
57
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}`);
58
142
  // 定义消
59
143
  const e = {
60
144
  // 事件类型
61
145
  Platform: 'kook',
62
- // 频道
146
+ //
63
147
  GuildId: event.extra.guild_id,
64
- // 子频道
65
148
  ChannelId: event.target_id,
66
- // 是否是主人
67
- IsMaster: isMaster,
68
- // 用户ID
149
+ // 用户Id
69
150
  UserId: event.author_id,
70
- // 用户名
151
+ UserKey,
71
152
  UserName: event.extra.author.username,
72
- // 用户头像
73
- UserAvatar: avatar.substring(0, avatar.indexOf('?')),
74
- // 格式化数据
75
- MsgId: event.msg_id,
76
- // 用户消息
77
- Megs: [
153
+ UserAvatar: UserAvatar,
154
+ IsMaster: isMaster,
155
+ // message
156
+ MessageId: event.msg_id,
157
+ MessageBody: [
78
158
  Text(msg),
79
159
  ...at_users.map(item => At(item.id, 'user', { name: item.name, avatar: item.avatar, bot: item.bot }))
80
160
  ],
81
- // 用户openId
82
- OpenID: data?.code,
83
- // 创建时间
161
+ MessageText: msg,
162
+ OpenId: data?.code,
84
163
  CreateAt: Date.now(),
85
164
  //
165
+ tag: 'MESSAGES_PUBLIC',
86
166
  value: null
87
167
  };
88
168
  // 当访问的时候获取
@@ -98,13 +178,17 @@ var index = defineBot(() => {
98
178
  client.on('ERROR', msg => {
99
179
  console.error(msg);
100
180
  });
181
+ // 客户端全局化。
182
+ global.client = client;
101
183
  return {
102
184
  api: {
103
185
  use: {
104
186
  send: (event, val) => {
105
187
  if (val.length < 0)
106
188
  return Promise.all([]);
107
- const content = useParse(val, 'Text');
189
+ const content = useParse({
190
+ MessageBody: val
191
+ }, 'Text');
108
192
  if (content) {
109
193
  return Promise.all([content].map(item => client.createMessage({
110
194
  type: 9,
@@ -112,7 +196,9 @@ var index = defineBot(() => {
112
196
  content: item
113
197
  })));
114
198
  }
115
- const images = useParse(val, 'Image');
199
+ const images = useParse({
200
+ MessageBody: val
201
+ }, 'Image');
116
202
  if (images) {
117
203
  return Promise.all(images.map(async (item) => {
118
204
  // 上传图片
@@ -134,4 +220,4 @@ var index = defineBot(() => {
134
220
  };
135
221
  });
136
222
 
137
- export { index as default };
223
+ export { client, index as default };
@@ -0,0 +1,46 @@
1
+ /**
2
+ * 基础配置结构
3
+ */
4
+ class BaseConfig {
5
+ #data = null;
6
+ constructor(val) {
7
+ this.#data = val;
8
+ }
9
+ /**
10
+ * 设置配置
11
+ * @param key
12
+ * @param val
13
+ */
14
+ set(key, val) {
15
+ if (val !== undefined)
16
+ this.#data[key] = val;
17
+ return this;
18
+ }
19
+ /**
20
+ *
21
+ * @param key
22
+ * @returns
23
+ */
24
+ has(key) {
25
+ if (Object.prototype.hasOwnProperty.call(this.#data, key)) ;
26
+ return false;
27
+ }
28
+ /**
29
+ * 读取配置
30
+ * @param key
31
+ * @returns
32
+ */
33
+ all() {
34
+ return this.#data;
35
+ }
36
+ /**
37
+ * 读取配置
38
+ * @param key
39
+ * @returns
40
+ */
41
+ get(key) {
42
+ return this.#data[key];
43
+ }
44
+ }
45
+
46
+ export { BaseConfig };
@@ -0,0 +1,42 @@
1
+ import { existsSync, createReadStream } from 'fs';
2
+ import { Readable, isReadable } from 'stream';
3
+ import { basename } from 'path';
4
+ import { fileTypeFromBuffer, fileTypeFromStream } from 'file-type';
5
+
6
+ /**
7
+ * 创建form
8
+ * @param image
9
+ * @param name
10
+ * @returns
11
+ */
12
+ async function createPicFrom(image, name = 'image.jpg') {
13
+ let picData;
14
+ // 是 string
15
+ if (typeof image === 'string') {
16
+ if (!existsSync(image))
17
+ return false;
18
+ if (!name)
19
+ name = basename(image);
20
+ picData = createReadStream(image);
21
+ // 是 buffer
22
+ }
23
+ else if (Buffer.isBuffer(image)) {
24
+ if (!name)
25
+ name = 'file.' + (await fileTypeFromBuffer(image)).ext;
26
+ picData = new Readable();
27
+ picData.push(image);
28
+ picData.push(null);
29
+ // 是 文件流
30
+ }
31
+ else if (isReadable(image)) {
32
+ if (!name)
33
+ name = 'file.' + (await fileTypeFromStream(image)).ext;
34
+ picData = image;
35
+ }
36
+ else {
37
+ return false;
38
+ }
39
+ return { picData, image, name };
40
+ }
41
+
42
+ export { createPicFrom };
@@ -0,0 +1,276 @@
1
+ import * as axios from 'axios';
2
+ import { AxiosRequestConfig } from 'axios';
3
+ import { Readable } from 'stream';
4
+ import { SendMessageParams, SendDirectMessageParams } from './typings.js';
5
+
6
+ /**
7
+ * api接口
8
+ */
9
+ declare class KOOKAPI {
10
+ API_URL: string;
11
+ /**
12
+ * KOOK服务
13
+ * @param config
14
+ * @returns
15
+ */
16
+ kookService(opstoin: AxiosRequestConfig): Promise<axios.AxiosResponse<any, any>>;
17
+ /**
18
+ *
19
+ * @returns
20
+ */
21
+ gateway(): Promise<any>;
22
+ /**
23
+ * ************
24
+ * 资源床单
25
+ * ***********
26
+ */
27
+ /**
28
+ * 发送buffer资源
29
+ * @param id 私信传频道id,公信传子频道id
30
+ * @param message {消息编号,图片,内容}
31
+ * @returns
32
+ */
33
+ postImage(file: string | Buffer | Readable, Name?: string): Promise<false | {
34
+ data: {
35
+ url: string;
36
+ };
37
+ }>;
38
+ /**
39
+ * 发送buffer资源
40
+ * @param id 私信传频道id,公信传子频道id
41
+ * @param message {消息编号,图片,内容}
42
+ * @returns
43
+ */
44
+ postFile(file: Buffer, Name?: string): Promise<false | {
45
+ data: {
46
+ url: string;
47
+ };
48
+ }>;
49
+ /**
50
+ * 转存
51
+ * @param formdata
52
+ * @returns
53
+ */
54
+ createUrl(formdata: any): Promise<{
55
+ data: {
56
+ url: string;
57
+ };
58
+ }>;
59
+ /**
60
+ * *********
61
+ * 消息api
62
+ * *********
63
+ */
64
+ /**
65
+ * 创建消息
66
+ * @param data
67
+ * @returns
68
+ */
69
+ createMessage(data: SendMessageParams): Promise<{
70
+ data: {
71
+ msg_id: string;
72
+ msg_timestamp: number;
73
+ nonce: string;
74
+ };
75
+ }>;
76
+ /**
77
+ * 创建私聊消息
78
+ */
79
+ /**
80
+ * 创建消息
81
+ * @param target_id
82
+ * @returns
83
+ */
84
+ userChatCreate(target_id: string): Promise<{
85
+ data: {
86
+ code: string;
87
+ last_read_time: number;
88
+ latest_msg_time: number;
89
+ unread_count: number;
90
+ is_friend: boolean;
91
+ is_blocked: boolean;
92
+ is_target_blocked: boolean;
93
+ target_info: {
94
+ id: string;
95
+ username: string;
96
+ online: boolean;
97
+ avatar: string;
98
+ };
99
+ };
100
+ }>;
101
+ /**
102
+ * 创建消息
103
+ * @param data
104
+ * @returns
105
+ */
106
+ createDirectMessage(data: SendDirectMessageParams): Promise<{
107
+ data: {
108
+ msg_id: string;
109
+ msg_timestamp: number;
110
+ nonce: string;
111
+ };
112
+ }>;
113
+ /**
114
+ * 删除指定消息
115
+ * @param msg_id
116
+ * @returns
117
+ */
118
+ messageDelete(msg_id: string): Promise<{
119
+ data: {
120
+ code: number;
121
+ message: string;
122
+ data: any[];
123
+ };
124
+ }>;
125
+ /**
126
+ * 重编辑指定消息
127
+ * @param data
128
+ * @returns
129
+ */
130
+ messageUpdate(data: {
131
+ msg_id: string;
132
+ content: any;
133
+ quote?: string;
134
+ temp_target_id?: string;
135
+ }): Promise<{
136
+ data: {
137
+ code: number;
138
+ message: string;
139
+ data: any[];
140
+ };
141
+ }>;
142
+ /**
143
+ * 删回应
144
+ * @param data
145
+ * @returns
146
+ */
147
+ messageDeleteReaction(data: {
148
+ msg_id: string;
149
+ emoji: string;
150
+ user_id: string;
151
+ }): Promise<{
152
+ code: number;
153
+ message: string;
154
+ data: any[];
155
+ }>;
156
+ /**
157
+ * 发回应
158
+ * @param data
159
+ * @returns
160
+ */
161
+ messageAddReaction(data: {
162
+ msg_id: string;
163
+ emoji: string;
164
+ }): Promise<{
165
+ code: number;
166
+ message: string;
167
+ data: any[];
168
+ }>;
169
+ /**
170
+ * 某个回应的所有用户
171
+ * @param data
172
+ * @returns
173
+ */
174
+ messageReactionList(params: {
175
+ msg_id: string;
176
+ emoji: string;
177
+ }): Promise<{
178
+ code: number;
179
+ message: string;
180
+ data: {
181
+ id: string;
182
+ username: string;
183
+ identify_num: string;
184
+ online: boolean;
185
+ status: number;
186
+ avatar: string;
187
+ bot: boolean;
188
+ tag_info: {
189
+ color: string;
190
+ text: string;
191
+ };
192
+ nickname: string;
193
+ reaction_time: number;
194
+ };
195
+ }>;
196
+ /**
197
+ * **********
198
+ * user
199
+ * *********
200
+ */
201
+ /**
202
+ * 得到当前信息
203
+ * @param guild_id
204
+ * @param user_id
205
+ * @returns
206
+ */
207
+ userMe(): Promise<{
208
+ code: number;
209
+ message: string;
210
+ data: {
211
+ id: string;
212
+ username: string;
213
+ identify_num: string;
214
+ online: false;
215
+ os: string;
216
+ status: number;
217
+ avatar: string;
218
+ banner: string;
219
+ bot: true;
220
+ mobile_verified: true;
221
+ client_id: string;
222
+ mobile_prefix: string;
223
+ mobile: string;
224
+ invited_count: number;
225
+ };
226
+ }>;
227
+ /**
228
+ * 得到用户信息
229
+ * @param guild_id
230
+ * @param user_id
231
+ * @returns
232
+ */
233
+ userView(guild_id: string, user_id: string): Promise<{
234
+ code: number;
235
+ message: string;
236
+ data: {
237
+ id: string;
238
+ username: string;
239
+ identify_num: string;
240
+ online: false;
241
+ status: 0;
242
+ bot: true;
243
+ avatar: string;
244
+ vip_avatar: string;
245
+ mobile_verified: true;
246
+ roles: number[];
247
+ joined_at: number;
248
+ active_time: number;
249
+ };
250
+ }>;
251
+ /**
252
+ * 踢出
253
+ * @param guild_id
254
+ * @param user_id
255
+ * @returns
256
+ */
257
+ guildKickout(guild_id: string, user_id: string): Promise<{
258
+ code: number;
259
+ message: string;
260
+ data: any;
261
+ }>;
262
+ /**
263
+ * 创建角色
264
+ * @param channel_id
265
+ * @param type
266
+ * @param value
267
+ * @returns
268
+ */
269
+ channelRoleCreate(channel_id: string, type: string, value: string): Promise<{
270
+ code: number;
271
+ message: string;
272
+ data: any;
273
+ }>;
274
+ }
275
+
276
+ export { KOOKAPI };