@alemonjs/kook 0.0.2 → 0.1.0

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/README.md ADDED
@@ -0,0 +1,34 @@
1
+ # [https://alemonjs.com/](https://alemonjs.com/)
2
+
3
+ 跨平台开发的事件驱动机器人
4
+
5
+ ## USE
6
+
7
+ - kook
8
+
9
+ ```sh
10
+ yarn add @alemonjs/kook
11
+ ```
12
+
13
+ - alemon.config.yaml
14
+
15
+ ```sh
16
+ kook:
17
+ # 令牌
18
+ token: ''
19
+ ```
20
+
21
+ > 完整的
22
+
23
+ ```sh
24
+ kook:
25
+ # 令牌
26
+ token: ''
27
+ # 主人
28
+ master_id:
29
+ - ''
30
+ ```
31
+
32
+ ## Community
33
+
34
+ QQ Group 806943302
package/lib/index.d.ts CHANGED
@@ -1,5 +1,7 @@
1
- import * as alemonjs from 'alemonjs';
1
+ import { KOOKClient } from './sdk/platform/kook/sdk/wss.js';
2
2
 
3
- declare const _default: (_: alemonjs.ConfigType) => typeof global.alemonjs;
3
+ type Client = typeof KOOKClient.prototype;
4
+ declare const client: Client;
5
+ declare const _default: () => typeof global.alemonjs;
4
6
 
5
- export { _default as default };
7
+ export { type Client, client, _default as default };
package/lib/index.js CHANGED
@@ -1,7 +1,21 @@
1
- import { defineBot, Text, At, OnProcessor, useParse } from 'alemonjs';
2
- import { KOOKClient } from 'chat-space';
1
+ import { defineBot, getConfig, 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
 
4
- var index = defineBot(config => {
13
+ const client = global.client;
14
+ var index = defineBot(() => {
15
+ const cfg = getConfig();
16
+ const config = cfg.value?.kook;
17
+ if (!config)
18
+ return;
5
19
  // 创建客户端
6
20
  const client = new KOOKClient({
7
21
  token: config.token
@@ -63,6 +77,9 @@ var index = defineBot(config => {
63
77
  IsMaster: isMaster,
64
78
  // 用户ID
65
79
  UserId: event.author_id,
80
+ GuildIdName: '',
81
+ GuildIdAvatar: '',
82
+ ChannelName: '',
66
83
  // 用户名
67
84
  UserName: event.extra.author.username,
68
85
  // 用户头像
@@ -70,14 +87,17 @@ var index = defineBot(config => {
70
87
  // 格式化数据
71
88
  MsgId: event.msg_id,
72
89
  // 用户消息
73
- Megs: [
90
+ MessageBody: [
74
91
  Text(msg),
75
92
  ...at_users.map(item => At(item.id, 'user', { name: item.name, avatar: item.avatar, bot: item.bot }))
76
93
  ],
94
+ MessageText: msg,
77
95
  // 用户openId
78
96
  OpenID: data?.code,
79
97
  // 创建时间
80
98
  CreateAt: Date.now(),
99
+ // 表情
100
+ tag: 'MESSAGES_PUBLIC',
81
101
  //
82
102
  value: null
83
103
  };
@@ -94,13 +114,17 @@ var index = defineBot(config => {
94
114
  client.on('ERROR', msg => {
95
115
  console.error(msg);
96
116
  });
117
+ // 客户端全局化。
118
+ global.client = client;
97
119
  return {
98
120
  api: {
99
121
  use: {
100
122
  send: (event, val) => {
101
123
  if (val.length < 0)
102
124
  return Promise.all([]);
103
- const content = useParse(val, 'Text');
125
+ const content = useParse({
126
+ MessageBody: val
127
+ }, 'Text');
104
128
  if (content) {
105
129
  return Promise.all([content].map(item => client.createMessage({
106
130
  type: 9,
@@ -108,7 +132,9 @@ var index = defineBot(config => {
108
132
  content: item
109
133
  })));
110
134
  }
111
- const images = useParse(val, 'Image');
135
+ const images = useParse({
136
+ MessageBody: val
137
+ }, 'Image');
112
138
  if (images) {
113
139
  return Promise.all(images.map(async (item) => {
114
140
  // 上传图片
@@ -130,4 +156,4 @@ var index = defineBot(config => {
130
156
  };
131
157
  });
132
158
 
133
- export { index as default };
159
+ 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 };