@alemonjs/kook 0.2.2 → 0.2.3

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 CHANGED
@@ -1,291 +1,292 @@
1
- import { defineBot, getConfigValue, useUserHashKey, OnProcessor } 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'
1
+ import { defineBot, getConfigValue, useUserHashKey, OnProcessor } 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';
12
12
 
13
- const platform = 'kook'
14
- const client = new Proxy(
15
- {},
16
- {
13
+ const platform = 'kook';
14
+ const client = new Proxy({}, {
17
15
  get: (_, prop) => {
18
- if (prop in global.client) {
19
- return global.client[prop]
20
- }
21
- return undefined
16
+ if (prop in global.client) {
17
+ const original = global.client[prop];
18
+ // 防止函数内this丢失
19
+ return typeof original === 'function' ? original.bind(global.client) : original;
20
+ }
21
+ return undefined;
22
22
  }
23
- }
24
- )
23
+ });
25
24
  var index = defineBot(() => {
26
- const value = getConfigValue()
27
- const config = value?.kook
28
- if (!config) return
29
- // 创建客户端
30
- const client = new KOOKClient({
31
- token: config.token
32
- })
33
- // 连接
34
- client.connect()
35
- client.on('MESSAGES_DIRECT', async event => {
36
- // 过滤机器人
37
- if (event.extra?.author?.bot) return false
38
- // 主人
39
- const master_key = config?.master_key ?? []
40
- const isMaster = master_key.includes(event.author_id)
41
- // 头像
42
- const avatar = event.extra.author.avatar
43
- // 获取消息
44
- let msg = event.content
45
- const url = avatar.substring(0, avatar.indexOf('?'))
46
- const UserAvatar = {
47
- toBuffer: async () => {
48
- const arrayBuffer = await fetch(url).then(res => res.arrayBuffer())
49
- return Buffer.from(arrayBuffer)
50
- },
51
- toBase64: async () => {
52
- const arrayBuffer = await fetch(url).then(res => res.arrayBuffer())
53
- return Buffer.from(arrayBuffer).toString('base64')
54
- },
55
- toURL: async () => {
56
- return url
57
- }
58
- }
59
- const UserId = event.author_id
60
- const UserKey = useUserHashKey({
61
- Platform: platform,
62
- UserId
63
- })
64
- // 定义消
65
- const e = {
66
- // 事件类型
67
- Platform: platform,
68
- // 用户Id
69
- UserId: UserId,
70
- UserKey,
71
- UserName: event.extra.author.username,
72
- UserAvatar: UserAvatar,
73
- IsMaster: isMaster,
74
- IsBot: false,
75
- // message
76
- MessageId: event.msg_id,
77
- MessageText: msg,
78
- OpenId: '',
79
- CreateAt: Date.now(),
80
- //
81
- tag: 'MESSAGES_PUBLIC',
82
- value: null
83
- }
84
- // 当访问的时候获取
85
- Object.defineProperty(e, 'value', {
86
- get() {
87
- return event
88
- }
89
- })
90
- // 处理消息
91
- OnProcessor(e, 'private.message.create')
92
- })
93
- // 监听消息
94
- client.on('MESSAGES_PUBLIC', async event => {
95
- // 过滤机器人
96
- if (event.extra?.author?.bot) return false
97
- // 创建私聊标记
98
- const data = await client.userChatCreate(event.extra.author.id).then(res => res?.data)
99
- // 主人
100
- const master_key = config?.master_key ?? []
101
- const isMaster = master_key.includes(event.author_id)
102
- // 头像
103
- const avatar = event.extra.author.avatar
104
- // 获取消息
105
- let msg = event.content
106
- /**
107
- * 艾特类型所得到的
108
- * 包括机器人在内
109
- */
110
- const mention_role_part = event.extra.kmarkdown?.mention_role_part ?? []
111
- for (const item of mention_role_part) {
112
- msg = msg.replace(`(rol)${item.role_id}(rol)`, '').trim()
113
- }
114
- /**
115
- * 艾特用户所得到的
116
- */
117
- const mention_part = event.extra.kmarkdown?.mention_part ?? []
118
- for (const item of mention_part) {
119
- msg = msg.replace(`(met)${item.id}(met)`, '').trim()
120
- }
121
- const url = avatar.substring(0, avatar.indexOf('?'))
122
- const UserAvatar = {
123
- toBuffer: async () => {
124
- const arrayBuffer = await fetch(url).then(res => res.arrayBuffer())
125
- return Buffer.from(arrayBuffer)
126
- },
127
- toBase64: async () => {
128
- const arrayBuffer = await fetch(url).then(res => res.arrayBuffer())
129
- return Buffer.from(arrayBuffer).toString('base64')
130
- },
131
- toURL: async () => {
132
- return url
133
- }
134
- }
135
- const UserId = event.author_id
136
- const UserKey = useUserHashKey({
137
- Platform: platform,
138
- UserId
139
- })
140
- // 定义消
141
- const e = {
142
- // 事件类型
143
- Platform: platform,
144
- //
145
- GuildId: event.extra.guild_id,
146
- ChannelId: event.target_id,
147
- // 用户Id
148
- UserId: UserId,
149
- UserKey,
150
- UserName: event.extra.author.username,
151
- UserAvatar: UserAvatar,
152
- IsMaster: isMaster,
153
- IsBot: false,
154
- // message
155
- MessageId: event.msg_id,
156
- MessageText: msg,
157
- OpenId: data?.code,
158
- CreateAt: Date.now(),
159
- //
160
- tag: 'MESSAGES_PUBLIC',
161
- value: null
162
- }
163
- // 当访问的时候获取
164
- Object.defineProperty(e, 'value', {
165
- get() {
166
- return event
167
- }
168
- })
169
- // 处理消息
170
- OnProcessor(e, 'message.create')
171
- })
172
- // 发送错误时
173
- client.on('ERROR', msg => {
174
- console.error(msg)
175
- })
176
- // 客户端全局化。
177
- global.client = client
178
- return {
179
- api: {
180
- use: {
181
- send: (event, val) => {
182
- if (val.length < 0) return Promise.all([])
183
- const content = val
184
- .filter(item => item.type == 'Link' || item.type == 'Mention' || item.type == 'Text')
185
- .map(item => {
186
- if (item.type == 'Link') {
187
- return `[${item.options?.title ?? item.value}](${item.value})`
188
- } else if (item.type == 'Mention') {
189
- if (
190
- item.value == 'everyone' ||
191
- item.value == 'all' ||
192
- item.value == '' ||
193
- typeof item.value != 'string'
194
- ) {
195
- return `(met)all(met)`
196
- }
197
- if (item.options?.belong == 'user') {
198
- return `(met)${item.value}(met)`
199
- } else if (item.options?.belong == 'channel') {
200
- return `(chn)${item.value}(chn)`
201
- }
202
- return ''
203
- } else if (item.type == 'Text') {
204
- if (item.options?.style == 'block') {
205
- return `\`${item.value}\``
206
- } else if (item.options?.style == 'italic') {
207
- return `*${item.value}*`
208
- } else if (item.options?.style == 'bold') {
209
- return `**${item.value}**`
210
- } else if (item.options?.style == 'strikethrough') {
211
- return `~~${item.value}~~`
212
- } else if (item.options?.style == 'boldItalic') {
213
- return `***${item.value}***`
25
+ const value = getConfigValue();
26
+ const config = value[platform];
27
+ // 创建客户端
28
+ const client = new KOOKClient({
29
+ token: config.token
30
+ });
31
+ // 连接
32
+ client.connect();
33
+ client.on('MESSAGES_DIRECT', async (event) => {
34
+ // 过滤机器人
35
+ if (event.extra?.author?.bot)
36
+ return false;
37
+ // 主人
38
+ const master_key = config?.master_key ?? [];
39
+ const isMaster = master_key.includes(event.author_id);
40
+ // 头像
41
+ const avatar = event.extra.author.avatar;
42
+ // 获取消息
43
+ let msg = event.content;
44
+ const url = avatar.substring(0, avatar.indexOf('?'));
45
+ const UserAvatar = {
46
+ toBuffer: async () => {
47
+ const arrayBuffer = await fetch(url).then(res => res.arrayBuffer());
48
+ return Buffer.from(arrayBuffer);
49
+ },
50
+ toBase64: async () => {
51
+ const arrayBuffer = await fetch(url).then(res => res.arrayBuffer());
52
+ return Buffer.from(arrayBuffer).toString('base64');
53
+ },
54
+ toURL: async () => {
55
+ return url;
56
+ }
57
+ };
58
+ const UserId = event.author_id;
59
+ const UserKey = useUserHashKey({
60
+ Platform: platform,
61
+ UserId
62
+ });
63
+ // 定义消
64
+ const e = {
65
+ // 事件类型
66
+ Platform: platform,
67
+ // 用户Id
68
+ UserId: UserId,
69
+ UserKey,
70
+ UserName: event.extra.author.username,
71
+ UserAvatar: UserAvatar,
72
+ IsMaster: isMaster,
73
+ IsBot: false,
74
+ // message
75
+ MessageId: event.msg_id,
76
+ MessageText: msg,
77
+ OpenId: '',
78
+ CreateAt: Date.now(),
79
+ //
80
+ tag: 'MESSAGES_PUBLIC',
81
+ value: null
82
+ };
83
+ // 当访问的时候获取
84
+ Object.defineProperty(e, 'value', {
85
+ get() {
86
+ return event;
87
+ }
88
+ });
89
+ // 处理消息
90
+ OnProcessor(e, 'private.message.create');
91
+ });
92
+ // 监听消息
93
+ client.on('MESSAGES_PUBLIC', async (event) => {
94
+ // 过滤机器人
95
+ if (event.extra?.author?.bot)
96
+ return false;
97
+ // 创建私聊标记
98
+ const data = await client.userChatCreate(event.extra.author.id).then(res => res?.data);
99
+ // 主人
100
+ const master_key = config?.master_key ?? [];
101
+ const isMaster = master_key.includes(event.author_id);
102
+ // 头像
103
+ const avatar = event.extra.author.avatar;
104
+ // 获取消息
105
+ let msg = event.content;
106
+ /**
107
+ * 艾特类型所得到的
108
+ * 包括机器人在内
109
+ */
110
+ const mention_role_part = event.extra.kmarkdown?.mention_role_part ?? [];
111
+ for (const item of mention_role_part) {
112
+ msg = msg.replace(`(rol)${item.role_id}(rol)`, '').trim();
113
+ }
114
+ /**
115
+ * 艾特用户所得到的
116
+ */
117
+ const mention_part = event.extra.kmarkdown?.mention_part ?? [];
118
+ for (const item of mention_part) {
119
+ msg = msg.replace(`(met)${item.id}(met)`, '').trim();
120
+ }
121
+ const url = avatar.substring(0, avatar.indexOf('?'));
122
+ const UserAvatar = {
123
+ toBuffer: async () => {
124
+ const arrayBuffer = await fetch(url).then(res => res.arrayBuffer());
125
+ return Buffer.from(arrayBuffer);
126
+ },
127
+ toBase64: async () => {
128
+ const arrayBuffer = await fetch(url).then(res => res.arrayBuffer());
129
+ return Buffer.from(arrayBuffer).toString('base64');
130
+ },
131
+ toURL: async () => {
132
+ return url;
133
+ }
134
+ };
135
+ const UserId = event.author_id;
136
+ const UserKey = useUserHashKey({
137
+ Platform: platform,
138
+ UserId
139
+ });
140
+ // 定义消
141
+ const e = {
142
+ // 事件类型
143
+ Platform: platform,
144
+ //
145
+ GuildId: event.extra.guild_id,
146
+ ChannelId: event.target_id,
147
+ // 用户Id
148
+ UserId: UserId,
149
+ UserKey,
150
+ UserName: event.extra.author.username,
151
+ UserAvatar: UserAvatar,
152
+ IsMaster: isMaster,
153
+ IsBot: false,
154
+ // message
155
+ MessageId: event.msg_id,
156
+ MessageText: msg,
157
+ OpenId: data?.code,
158
+ CreateAt: Date.now(),
159
+ //
160
+ tag: 'MESSAGES_PUBLIC',
161
+ value: null
162
+ };
163
+ // 当访问的时候获取
164
+ Object.defineProperty(e, 'value', {
165
+ get() {
166
+ return event;
167
+ }
168
+ });
169
+ // 处理消息
170
+ OnProcessor(e, 'message.create');
171
+ });
172
+ // 发送错误时
173
+ client.on('ERROR', msg => {
174
+ console.error(msg);
175
+ });
176
+ // 客户端全局化。
177
+ global.client = client;
178
+ return {
179
+ api: {
180
+ use: {
181
+ send: (event, val) => {
182
+ if (val.length < 0)
183
+ return Promise.all([]);
184
+ const content = val
185
+ .filter(item => item.type == 'Link' || item.type == 'Mention' || item.type == 'Text')
186
+ .map(item => {
187
+ if (item.type == 'Link') {
188
+ return `[${item.options?.title ?? item.value}](${item.value})`;
189
+ }
190
+ else if (item.type == 'Mention') {
191
+ if (item.value == 'everyone' ||
192
+ item.value == 'all' ||
193
+ item.value == '' ||
194
+ typeof item.value != 'string') {
195
+ return `(met)all(met)`;
196
+ }
197
+ if (item.options?.belong == 'user') {
198
+ return `(met)${item.value}(met)`;
199
+ }
200
+ else if (item.options?.belong == 'channel') {
201
+ return `(chn)${item.value}(chn)`;
202
+ }
203
+ return '';
204
+ }
205
+ else if (item.type == 'Text') {
206
+ if (item.options?.style == 'block') {
207
+ return `\`${item.value}\``;
208
+ }
209
+ else if (item.options?.style == 'italic') {
210
+ return `*${item.value}*`;
211
+ }
212
+ else if (item.options?.style == 'bold') {
213
+ return `**${item.value}**`;
214
+ }
215
+ else if (item.options?.style == 'strikethrough') {
216
+ return `~~${item.value}~~`;
217
+ }
218
+ else if (item.options?.style == 'boldItalic') {
219
+ return `***${item.value}***`;
220
+ }
221
+ return item.value;
222
+ }
223
+ })
224
+ .join('');
225
+ if (content) {
226
+ return Promise.all([content].map(item => client.createMessage({
227
+ type: 9,
228
+ target_id: event.ChannelId,
229
+ content: item
230
+ })));
231
+ }
232
+ const images = val.filter(item => item.type == 'Image').map(item => item.value);
233
+ if (images) {
234
+ return Promise.all(images.map(async (item) => {
235
+ // 上传图片
236
+ const res = await client.postImage(item);
237
+ if (!res)
238
+ return Promise.resolve();
239
+ // 发送消息
240
+ return await client.createMessage({
241
+ type: 2,
242
+ target_id: event.ChannelId,
243
+ content: res.data.url
244
+ });
245
+ }));
246
+ }
247
+ return Promise.all([]);
248
+ },
249
+ mention: async (e) => {
250
+ const event = e.value;
251
+ const MessageMention = [];
252
+ /**
253
+ * 艾特类型所得到的,包括机器人在内
254
+ */
255
+ const mention_role_part = event.extra.kmarkdown?.mention_role_part ?? [];
256
+ for (const item of mention_role_part) {
257
+ MessageMention.push({
258
+ UserId: item.role_id,
259
+ UserName: item.name,
260
+ UserKey: useUserHashKey({
261
+ Platform: platform,
262
+ UserId: item.role_id
263
+ }),
264
+ IsMaster: false,
265
+ IsBot: true
266
+ });
267
+ }
268
+ /**
269
+ * 艾特用户所得到的
270
+ */
271
+ const mention_part = event.extra.kmarkdown?.mention_part ?? [];
272
+ for (const item of mention_part) {
273
+ MessageMention.push({
274
+ // avatar: item.avatar,
275
+ UserId: item.id,
276
+ UserName: item.username,
277
+ UserKey: useUserHashKey({
278
+ Platform: platform,
279
+ UserId: item.role_id
280
+ }),
281
+ IsMaster: false,
282
+ IsBot: false
283
+ });
284
+ }
285
+ return MessageMention;
214
286
  }
215
- return item.value
216
- }
217
- })
218
- .join('')
219
- if (content) {
220
- return Promise.all(
221
- [content].map(item =>
222
- client.createMessage({
223
- type: 9,
224
- target_id: event.ChannelId,
225
- content: item
226
- })
227
- )
228
- )
229
- }
230
- const images = val.filter(item => item.type == 'Image').map(item => item.value)
231
- if (images) {
232
- return Promise.all(
233
- images.map(async item => {
234
- // 上传图片
235
- const res = await client.postImage(item)
236
- if (!res) return Promise.resolve()
237
- // 发送消息
238
- return await client.createMessage({
239
- type: 2,
240
- target_id: event.ChannelId,
241
- content: res.data.url
242
- })
243
- })
244
- )
245
- }
246
- return Promise.all([])
247
- },
248
- mention: async e => {
249
- const event = e.value
250
- const MessageMention = []
251
- /**
252
- * 艾特类型所得到的,包括机器人在内
253
- */
254
- const mention_role_part = event.extra.kmarkdown?.mention_role_part ?? []
255
- for (const item of mention_role_part) {
256
- MessageMention.push({
257
- UserId: item.role_id,
258
- UserName: item.name,
259
- UserKey: useUserHashKey({
260
- Platform: platform,
261
- UserId: item.role_id
262
- }),
263
- IsMaster: false,
264
- IsBot: true
265
- })
266
- }
267
- /**
268
- * 艾特用户所得到的
269
- */
270
- const mention_part = event.extra.kmarkdown?.mention_part ?? []
271
- for (const item of mention_part) {
272
- MessageMention.push({
273
- // avatar: item.avatar,
274
- UserId: item.id,
275
- UserName: item.username,
276
- UserKey: useUserHashKey({
277
- Platform: platform,
278
- UserId: item.role_id
279
- }),
280
- IsMaster: false,
281
- IsBot: false
282
- })
283
- }
284
- return MessageMention
287
+ }
285
288
  }
286
- }
287
- }
288
- }
289
- })
289
+ };
290
+ });
290
291
 
291
- export { client, index as default, platform }
292
+ export { client, index as default, platform };
@@ -2,44 +2,45 @@
2
2
  * 基础配置结构
3
3
  */
4
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) this.#data[key] = val
16
- return this
17
- }
18
- /**
19
- *
20
- * @param key
21
- * @returns
22
- */
23
- has(key) {
24
- if (Object.prototype.hasOwnProperty.call(this.#data, key));
25
- return false
26
- }
27
- /**
28
- * 读取配置
29
- * @param key
30
- * @returns
31
- */
32
- all() {
33
- return this.#data
34
- }
35
- /**
36
- * 读取配置
37
- * @param key
38
- * @returns
39
- */
40
- get(key) {
41
- return this.#data[key]
42
- }
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
+ }
43
44
  }
44
45
 
45
- export { BaseConfig }
46
+ export { BaseConfig };