@alemonjs/kook 0.2.5 → 0.2.7

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 CHANGED
@@ -1,59 +1,61 @@
1
- import { readFileSync } from 'fs';
2
- import { dirname, join } from 'path';
3
- import { fileURLToPath } from 'url';
4
- import { getConfig, getConfigValue } from 'alemonjs';
1
+ import { readFileSync } from 'fs'
2
+ import { dirname, join } from 'path'
3
+ import { fileURLToPath } from 'url'
4
+ import { getConfig, getConfigValue } from 'alemonjs'
5
5
 
6
6
  // 当前目录
7
- const __dirname = dirname(fileURLToPath(import.meta.url));
7
+ const __dirname = dirname(fileURLToPath(import.meta.url))
8
8
  // 被激活的时候。
9
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
- }
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.*?rel="stylesheet".*?href="(.+?)".*?>/
17
+ const iconReg = /<link.*?rel="icon".*?href="(.+?)".*?>/g
18
+ // 创建 webview 路径
19
+ const styleUri = context.createExtensionDir(
20
+ join(__dirname, '../', 'dist', 'assets', 'index.css')
21
+ )
22
+ const scriptUri = context.createExtensionDir(
23
+ join(__dirname, '../', 'dist', 'assets', 'index.js')
24
+ )
25
+ // 确保路径存在
26
+ const html = readFileSync(dir, 'utf-8')
27
+ .replace(iconReg, ``)
28
+ .replace(scriptReg, `<script type="module" crossorigin src="${scriptUri}"></script>`)
29
+ .replace(styleReg, `<link rel="stylesheet" crossorigin href="${styleUri}">`)
30
+ // 立即渲染 webview
31
+ webView.loadWebView(html)
32
+ })
33
+ // 监听 webview 的消息。
34
+ webView.onMessage(data => {
35
+ try {
36
+ if (data.type === 'kook.form.save') {
37
+ const db = data.data
38
+ const config = getConfig()
39
+ const value = config.value ?? {}
40
+ value['kook'] = {
41
+ token: db.token ?? '',
42
+ master_key: db.master_key?.split(',') ?? null
52
43
  }
53
- catch (e) {
54
- console.error(e);
55
- }
56
- });
57
- };
44
+ config.saveValue(value)
45
+ context.notification('KOOK 配置保存成功~')
46
+ } else if (data.type === 'kook.init') {
47
+ let config = getConfigValue()
48
+ if (!config) config = {}
49
+ // 发送消息
50
+ webView.postMessage({
51
+ type: 'kook.init',
52
+ data: config.kook ?? {}
53
+ })
54
+ }
55
+ } catch (e) {
56
+ console.error(e)
57
+ }
58
+ })
59
+ }
58
60
 
59
- export { activate };
61
+ 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.DefineBotValue
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,5 +1,5 @@
1
- import { defineBot, getConfigValue, useUserHashKey, OnProcessor } from 'alemonjs';
2
- import 'fs';
1
+ import { defineBot, getConfigValue, useUserHashKey, onProcessor } from 'alemonjs';
2
+ import { readFileSync } from 'fs';
3
3
  import 'axios';
4
4
  import 'path';
5
5
  import 'qrcode';
@@ -83,14 +83,8 @@ var index = defineBot(() => {
83
83
  tag: 'MESSAGES_PUBLIC',
84
84
  value: null
85
85
  };
86
- // 当访问的时候获取
87
- Object.defineProperty(e, 'value', {
88
- get() {
89
- return event;
90
- }
91
- });
92
86
  // 处理消息
93
- OnProcessor(e, 'private.message.create');
87
+ onProcessor('private.message.create', e, event);
94
88
  });
95
89
  // 监听消息
96
90
  client.on('MESSAGES_PUBLIC', async (event) => {
@@ -164,14 +158,8 @@ var index = defineBot(() => {
164
158
  tag: 'MESSAGES_PUBLIC',
165
159
  value: null
166
160
  };
167
- // 当访问的时候获取
168
- Object.defineProperty(e, 'value', {
169
- get() {
170
- return event;
171
- }
172
- });
173
161
  // 处理消息
174
- OnProcessor(e, 'message.create');
162
+ onProcessor('message.create', e, event);
175
163
  });
176
164
  // 发送错误时
177
165
  client.on('ERROR', msg => {
@@ -180,18 +168,105 @@ var index = defineBot(() => {
180
168
  // 客户端全局化。
181
169
  global.client = client;
182
170
  return {
171
+ platform,
183
172
  api: {
173
+ active: {
174
+ send: {
175
+ channel: (channel_id, val) => {
176
+ if (val.length < 0)
177
+ return Promise.all([]);
178
+ const content = val
179
+ .filter(item => item.type == 'Mention' || item.type == 'Text')
180
+ .map(item => {
181
+ // if (item.type == 'Link') {
182
+ // return `[${item.options?.title ?? item.value}](${item.value})`
183
+ // } else
184
+ if (item.type == 'Mention') {
185
+ if (item.value == 'everyone' ||
186
+ item.value == 'all' ||
187
+ item.value == '' ||
188
+ typeof item.value != 'string') {
189
+ return `(met)all(met)`;
190
+ }
191
+ if (item.options?.belong == 'user') {
192
+ return `(met)${item.value}(met)`;
193
+ }
194
+ else if (item.options?.belong == 'channel') {
195
+ return `(chn)${item.value}(chn)`;
196
+ }
197
+ return '';
198
+ }
199
+ else if (item.type == 'Text') {
200
+ if (item.options?.style == 'block') {
201
+ return `\`${item.value}\``;
202
+ }
203
+ else if (item.options?.style == 'italic') {
204
+ return `*${item.value}*`;
205
+ }
206
+ else if (item.options?.style == 'bold') {
207
+ return `**${item.value}**`;
208
+ }
209
+ else if (item.options?.style == 'strikethrough') {
210
+ return `~~${item.value}~~`;
211
+ }
212
+ else if (item.options?.style == 'boldItalic') {
213
+ return `***${item.value}***`;
214
+ }
215
+ return item.value;
216
+ }
217
+ })
218
+ .join('');
219
+ if (content) {
220
+ return Promise.all([content].map(item => client.createMessage({
221
+ type: 9,
222
+ target_id: channel_id,
223
+ content: item
224
+ })));
225
+ }
226
+ const images = val.filter(item => item.type == 'Image' || item.type == 'ImageFile' || item.type == 'ImageURL');
227
+ if (images) {
228
+ return Promise.all(images.map(async (item) => {
229
+ if (item.type == 'ImageURL') {
230
+ return await client.createMessage({
231
+ type: 2,
232
+ target_id: channel_id,
233
+ content: item.value
234
+ });
235
+ }
236
+ let data = item.value;
237
+ if (item.type == 'ImageFile') {
238
+ data = readFileSync(item.value);
239
+ }
240
+ // 上传图片
241
+ const res = await client.postImage(data);
242
+ if (!res)
243
+ return Promise.resolve();
244
+ // 发送消息
245
+ return await client.createMessage({
246
+ type: 2,
247
+ target_id: channel_id,
248
+ content: res.data.url
249
+ });
250
+ }));
251
+ }
252
+ return Promise.all([]);
253
+ },
254
+ user: (channel_id, val) => {
255
+ return Promise.all([]);
256
+ }
257
+ }
258
+ },
184
259
  use: {
185
260
  send: (event, val) => {
186
261
  if (val.length < 0)
187
262
  return Promise.all([]);
188
263
  const content = val
189
- .filter(item => item.type == 'Link' || item.type == 'Mention' || item.type == 'Text')
264
+ .filter(item => item.type == 'Mention' || item.type == 'Text')
190
265
  .map(item => {
191
- if (item.type == 'Link') {
192
- return `[${item.options?.title ?? item.value}](${item.value})`;
193
- }
194
- else if (item.type == 'Mention') {
266
+ // if (item.type == 'Link') {
267
+ // return `[${item.options?.title ?? item.value}](${item.value})`
268
+ // } else
269
+ if (item.type == 'Mention') {
195
270
  if (item.value == 'everyone' ||
196
271
  item.value == 'all' ||
197
272
  item.value == '' ||
@@ -233,11 +308,22 @@ var index = defineBot(() => {
233
308
  content: item
234
309
  })));
235
310
  }
236
- const images = val.filter(item => item.type == 'Image').map(item => item.value);
311
+ const images = val.filter(item => item.type == 'Image' || item.type == 'ImageFile' || item.type == 'ImageURL');
237
312
  if (images) {
238
313
  return Promise.all(images.map(async (item) => {
314
+ if (item.type == 'ImageURL') {
315
+ return await client.createMessage({
316
+ type: 2,
317
+ target_id: event.ChannelId,
318
+ content: item.value
319
+ });
320
+ }
321
+ let data = item.value;
322
+ if (item.type == 'ImageFile') {
323
+ data = readFileSync(item.value);
324
+ }
239
325
  // 上传图片
240
- const res = await client.postImage(item);
326
+ const res = await client.postImage(data);
241
327
  if (!res)
242
328
  return Promise.resolve();
243
329
  // 发送消息