@alemonjs/discord 0.2.1 → 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.
@@ -0,0 +1,3 @@
1
+ declare const activate: (context: any) => void;
2
+
3
+ export { activate };
package/lib/desktop.js ADDED
@@ -0,0 +1,58 @@
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.discord', () => {
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 === 'discord.form.save') {
31
+ const CIG = data.data;
32
+ const config = getConfig();
33
+ const value = config.value ?? {};
34
+ value['discord'] = {
35
+ token: CIG.token ?? '',
36
+ master_key: CIG.master_key.split(',')
37
+ };
38
+ config.saveValue(value);
39
+ context.notification('DC 配置保存成功~');
40
+ }
41
+ else if (data.type === 'discord.init') {
42
+ let config = getConfigValue();
43
+ if (!config)
44
+ config = {};
45
+ // 发送消息
46
+ webView.postMessage({
47
+ type: 'discord.init',
48
+ data: config.qq_bot ?? {}
49
+ });
50
+ }
51
+ }
52
+ catch (e) {
53
+ console.error(e);
54
+ }
55
+ });
56
+ };
57
+
58
+ export { activate };
package/lib/index.js CHANGED
@@ -12,17 +12,19 @@ import { DCClient } from './sdk/platform/discord/sdk/wss.js';
12
12
  const client = new Proxy({}, {
13
13
  get: (_, prop) => {
14
14
  if (prop in global.client) {
15
- return global.client[prop];
15
+ const original = global.client[prop];
16
+ // 防止函数内this丢失
17
+ return typeof original === 'function' ? original.bind(global.client) : original;
16
18
  }
17
19
  return undefined;
18
20
  }
19
21
  });
20
22
  const platform = 'discord';
21
23
  var index = defineBot(() => {
22
- const value = getConfigValue();
23
- const config = value?.discord;
24
- if (!config)
25
- return;
24
+ let value = getConfigValue();
25
+ if (!value)
26
+ value = {};
27
+ const config = value[platform];
26
28
  // 创建客户端
27
29
  const client = new DCClient({
28
30
  token: config.token
@@ -79,6 +81,7 @@ var index = defineBot(() => {
79
81
  };
80
82
  // 定义消
81
83
  const e = {
84
+ name: 'message.create',
82
85
  // 事件类型
83
86
  Platform: platform,
84
87
  // guild
@@ -171,7 +174,6 @@ var index = defineBot(() => {
171
174
  const event = e.value;
172
175
  const MessageMention = event.mentions.map(item => {
173
176
  let url = null;
174
- const Platform = 'discord';
175
177
  const UserId = item.id;
176
178
  const avatar = event.author.avatar;
177
179
  const value = getConfigValue();
@@ -205,7 +207,7 @@ var index = defineBot(() => {
205
207
  IsBot: item.bot,
206
208
  UserAvatar,
207
209
  UserKey: useUserHashKey({
208
- Platform: Platform,
210
+ Platform: platform,
209
211
  UserId: UserId
210
212
  })
211
213
  };
package/package.json CHANGED
@@ -1,23 +1,46 @@
1
1
  {
2
2
  "name": "@alemonjs/discord",
3
- "version": "0.2.1",
4
- "description": "discord-bot",
3
+ "version": "0.2.3",
4
+ "description": "阿柠檬discord平台连接",
5
5
  "author": "lemonade",
6
6
  "license": "MIT",
7
7
  "type": "module",
8
8
  "main": "lib/index.js",
9
9
  "types": "lib",
10
+ "scripts": {
11
+ "build": "node bundle.js"
12
+ },
10
13
  "exports": {
11
14
  ".": {
12
15
  "import": "./lib/index.js",
13
16
  "types": "./lib/index.d.ts"
17
+ },
18
+ "./package": "./package.json",
19
+ "./desktop": "./lib/desktop.js"
20
+ },
21
+ "alemonjs": {
22
+ "desktop": {
23
+ "platform": [
24
+ {
25
+ "name": "discord"
26
+ }
27
+ ],
28
+ "commond": [
29
+ {
30
+ "name": "DC",
31
+ "commond": "open.discord"
32
+ }
33
+ ],
34
+ "sidebars": [
35
+ {
36
+ "name": "DC",
37
+ "commond": "open.discord"
38
+ }
39
+ ]
14
40
  }
15
41
  },
16
42
  "keywords": [
17
- "alemonjs",
18
- "discord",
19
- "bot",
20
- "chat-bot"
43
+ "alemonjs"
21
44
  ],
22
45
  "publishConfig": {
23
46
  "registry": "https://registry.npmjs.org",