@alemonjs/kook 0.2.2 → 0.2.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/desktop.js ADDED
@@ -0,0 +1,59 @@
1
+ import { readFileSync } from 'fs';
2
+ import { dirname, join } from 'path';
3
+ import { fileURLToPath } from 'url';
4
+ import { getConfig, getConfigValue } from 'alemonjs';
5
+
6
+ // 当前目录
7
+ const __dirname = dirname(fileURLToPath(import.meta.url));
8
+ // 被激活的时候。
9
+ const activate = context => {
10
+ // 创建一个 webview。
11
+ const webView = context.createSidebarWebView(context);
12
+ // 当命令被触发的时候。
13
+ context.onCommand('open.kook', () => {
14
+ const dir = join(__dirname, '../', 'dist', 'index.html');
15
+ const scriptReg = /<script.*?src="(.+?)".*?>/;
16
+ const styleReg = /<link.*?href="(.+?)".*?>/;
17
+ // 创建 webview 路径
18
+ const styleUri = context.createExtensionDir(join(__dirname, '../', 'dist', 'assets', 'index.css'));
19
+ const scriptUri = context.createExtensionDir(join(__dirname, '../', 'dist', 'assets', 'index.js'));
20
+ // 确保路径存在
21
+ const html = readFileSync(dir, 'utf-8')
22
+ .replace(scriptReg, `<script type="module" crossorigin src="${scriptUri}"></script>`)
23
+ .replace(styleReg, `<link rel="stylesheet" crossorigin href="${styleUri}">`);
24
+ // 立即渲染 webview
25
+ webView.loadWebView(html);
26
+ });
27
+ // 监听 webview 的消息。
28
+ webView.onMessage(data => {
29
+ try {
30
+ if (data.type === 'kook.form.save') {
31
+ const CIG = data.data;
32
+ const config = getConfig();
33
+ const value = config.value ?? {};
34
+ value['kook'] = {
35
+ token: CIG.token ?? '',
36
+ // master_key 12121,1313,1313,13 转为数组
37
+ master_key: CIG.master_key.split(',')
38
+ };
39
+ config.saveValue(value);
40
+ context.notification('KOOK 配置保存成功~');
41
+ }
42
+ else if (data.type === 'kook.init') {
43
+ let config = getConfigValue();
44
+ if (!config)
45
+ config = {};
46
+ // 发送消息
47
+ webView.postMessage({
48
+ type: 'kook.init',
49
+ data: config.qq_bot ?? {}
50
+ });
51
+ }
52
+ }
53
+ catch (e) {
54
+ console.error(e);
55
+ }
56
+ });
57
+ };
58
+
59
+ export { activate };
package/lib/index.d.ts CHANGED
@@ -1,9 +1,9 @@
1
- import * as alemonjs from 'alemonjs'
2
- import { KOOKClient } from './sdk/platform/kook/sdk/wss.js'
1
+ import * as alemonjs from 'alemonjs';
2
+ import { KOOKClient } from './sdk/platform/kook/sdk/wss.js';
3
3
 
4
- type Client = typeof KOOKClient.prototype
5
- declare const platform = 'kook'
6
- declare const client: Client
7
- declare const _default: () => alemonjs.ClientAPI
4
+ type Client = typeof KOOKClient.prototype;
5
+ declare const platform = "kook";
6
+ declare const client: Client;
7
+ declare const _default: () => alemonjs.ClientAPI;
8
8
 
9
- export { type Client, client, _default as default, platform }
9
+ export { type Client, client, _default as default, platform };
package/lib/index.js CHANGED
@@ -1,291 +1,296 @@
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
+ let value = getConfigValue();
26
+ if (!value)
27
+ value = {};
28
+ const config = value[platform];
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)
38
+ return false;
39
+ // 主人
40
+ const master_key = config?.master_key ?? [];
41
+ const isMaster = master_key.includes(event.author_id);
42
+ // 头像
43
+ const avatar = event.extra.author.avatar;
44
+ // 获取消息
45
+ let msg = event.content;
46
+ const url = avatar.substring(0, avatar.indexOf('?'));
47
+ const UserAvatar = {
48
+ toBuffer: async () => {
49
+ const arrayBuffer = await fetch(url).then(res => res.arrayBuffer());
50
+ return Buffer.from(arrayBuffer);
51
+ },
52
+ toBase64: async () => {
53
+ const arrayBuffer = await fetch(url).then(res => res.arrayBuffer());
54
+ return Buffer.from(arrayBuffer).toString('base64');
55
+ },
56
+ toURL: async () => {
57
+ return url;
58
+ }
59
+ };
60
+ const UserId = event.author_id;
61
+ const UserKey = useUserHashKey({
62
+ Platform: platform,
63
+ UserId
64
+ });
65
+ // 定义消
66
+ const e = {
67
+ name: 'private.message.create',
68
+ // 事件类型
69
+ Platform: platform,
70
+ // 用户Id
71
+ UserId: UserId,
72
+ UserKey,
73
+ UserName: event.extra.author.username,
74
+ UserAvatar: UserAvatar,
75
+ IsMaster: isMaster,
76
+ IsBot: false,
77
+ // message
78
+ MessageId: event.msg_id,
79
+ MessageText: msg,
80
+ OpenId: '',
81
+ CreateAt: Date.now(),
82
+ //
83
+ tag: 'MESSAGES_PUBLIC',
84
+ value: null
85
+ };
86
+ // 当访问的时候获取
87
+ Object.defineProperty(e, 'value', {
88
+ get() {
89
+ return event;
90
+ }
91
+ });
92
+ // 处理消息
93
+ OnProcessor(e, 'private.message.create');
94
+ });
95
+ // 监听消息
96
+ client.on('MESSAGES_PUBLIC', async (event) => {
97
+ // 过滤机器人
98
+ if (event.extra?.author?.bot)
99
+ return false;
100
+ // 创建私聊标记
101
+ const data = await client.userChatCreate(event.extra.author.id).then(res => res?.data);
102
+ // 主人
103
+ const master_key = config?.master_key ?? [];
104
+ const isMaster = master_key.includes(event.author_id);
105
+ // 头像
106
+ const avatar = event.extra.author.avatar;
107
+ // 获取消息
108
+ let msg = event.content;
109
+ /**
110
+ * 艾特类型所得到的
111
+ * 包括机器人在内
112
+ */
113
+ const mention_role_part = event.extra.kmarkdown?.mention_role_part ?? [];
114
+ for (const item of mention_role_part) {
115
+ msg = msg.replace(`(rol)${item.role_id}(rol)`, '').trim();
116
+ }
117
+ /**
118
+ * 艾特用户所得到的
119
+ */
120
+ const mention_part = event.extra.kmarkdown?.mention_part ?? [];
121
+ for (const item of mention_part) {
122
+ msg = msg.replace(`(met)${item.id}(met)`, '').trim();
123
+ }
124
+ const url = avatar.substring(0, avatar.indexOf('?'));
125
+ const UserAvatar = {
126
+ toBuffer: async () => {
127
+ const arrayBuffer = await fetch(url).then(res => res.arrayBuffer());
128
+ return Buffer.from(arrayBuffer);
129
+ },
130
+ toBase64: async () => {
131
+ const arrayBuffer = await fetch(url).then(res => res.arrayBuffer());
132
+ return Buffer.from(arrayBuffer).toString('base64');
133
+ },
134
+ toURL: async () => {
135
+ return url;
136
+ }
137
+ };
138
+ const UserId = event.author_id;
139
+ const UserKey = useUserHashKey({
140
+ Platform: platform,
141
+ UserId
142
+ });
143
+ // 定义消
144
+ const e = {
145
+ name: 'message.create',
146
+ // 事件类型
147
+ Platform: platform,
148
+ //
149
+ GuildId: event.extra.guild_id,
150
+ ChannelId: event.target_id,
151
+ // 用户Id
152
+ UserId: UserId,
153
+ UserKey,
154
+ UserName: event.extra.author.username,
155
+ UserAvatar: UserAvatar,
156
+ IsMaster: isMaster,
157
+ IsBot: false,
158
+ // message
159
+ MessageId: event.msg_id,
160
+ MessageText: msg,
161
+ OpenId: data?.code,
162
+ CreateAt: Date.now(),
163
+ //
164
+ tag: 'MESSAGES_PUBLIC',
165
+ value: null
166
+ };
167
+ // 当访问的时候获取
168
+ Object.defineProperty(e, 'value', {
169
+ get() {
170
+ return event;
171
+ }
172
+ });
173
+ // 处理消息
174
+ OnProcessor(e, 'message.create');
175
+ });
176
+ // 发送错误时
177
+ client.on('ERROR', msg => {
178
+ console.error(msg);
179
+ });
180
+ // 客户端全局化。
181
+ global.client = client;
182
+ return {
183
+ api: {
184
+ use: {
185
+ send: (event, val) => {
186
+ if (val.length < 0)
187
+ return Promise.all([]);
188
+ const content = val
189
+ .filter(item => item.type == 'Link' || item.type == 'Mention' || item.type == 'Text')
190
+ .map(item => {
191
+ if (item.type == 'Link') {
192
+ return `[${item.options?.title ?? item.value}](${item.value})`;
193
+ }
194
+ else if (item.type == 'Mention') {
195
+ if (item.value == 'everyone' ||
196
+ item.value == 'all' ||
197
+ item.value == '' ||
198
+ typeof item.value != 'string') {
199
+ return `(met)all(met)`;
200
+ }
201
+ if (item.options?.belong == 'user') {
202
+ return `(met)${item.value}(met)`;
203
+ }
204
+ else if (item.options?.belong == 'channel') {
205
+ return `(chn)${item.value}(chn)`;
206
+ }
207
+ return '';
208
+ }
209
+ else if (item.type == 'Text') {
210
+ if (item.options?.style == 'block') {
211
+ return `\`${item.value}\``;
212
+ }
213
+ else if (item.options?.style == 'italic') {
214
+ return `*${item.value}*`;
215
+ }
216
+ else if (item.options?.style == 'bold') {
217
+ return `**${item.value}**`;
218
+ }
219
+ else if (item.options?.style == 'strikethrough') {
220
+ return `~~${item.value}~~`;
221
+ }
222
+ else if (item.options?.style == 'boldItalic') {
223
+ return `***${item.value}***`;
224
+ }
225
+ return item.value;
226
+ }
227
+ })
228
+ .join('');
229
+ if (content) {
230
+ return Promise.all([content].map(item => client.createMessage({
231
+ type: 9,
232
+ target_id: event.ChannelId,
233
+ content: item
234
+ })));
235
+ }
236
+ const images = val.filter(item => item.type == 'Image').map(item => item.value);
237
+ if (images) {
238
+ return Promise.all(images.map(async (item) => {
239
+ // 上传图片
240
+ const res = await client.postImage(item);
241
+ if (!res)
242
+ return Promise.resolve();
243
+ // 发送消息
244
+ return await client.createMessage({
245
+ type: 2,
246
+ target_id: event.ChannelId,
247
+ content: res.data.url
248
+ });
249
+ }));
250
+ }
251
+ return Promise.all([]);
252
+ },
253
+ mention: async (e) => {
254
+ const event = e.value;
255
+ const MessageMention = [];
256
+ /**
257
+ * 艾特类型所得到的,包括机器人在内
258
+ */
259
+ const mention_role_part = event.extra.kmarkdown?.mention_role_part ?? [];
260
+ for (const item of mention_role_part) {
261
+ MessageMention.push({
262
+ UserId: item.role_id,
263
+ UserName: item.name,
264
+ UserKey: useUserHashKey({
265
+ Platform: platform,
266
+ UserId: item.role_id
267
+ }),
268
+ IsMaster: false,
269
+ IsBot: true
270
+ });
271
+ }
272
+ /**
273
+ * 艾特用户所得到的
274
+ */
275
+ const mention_part = event.extra.kmarkdown?.mention_part ?? [];
276
+ for (const item of mention_part) {
277
+ MessageMention.push({
278
+ // avatar: item.avatar,
279
+ UserId: item.id,
280
+ UserName: item.username,
281
+ UserKey: useUserHashKey({
282
+ Platform: platform,
283
+ UserId: item.role_id
284
+ }),
285
+ IsMaster: false,
286
+ IsBot: false
287
+ });
288
+ }
289
+ return MessageMention;
214
290
  }
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
291
+ }
285
292
  }
286
- }
287
- }
288
- }
289
- })
293
+ };
294
+ });
290
295
 
291
- export { client, index as default, platform }
296
+ export { client, index as default, platform };