@alemonjs/discord 0.1.1 → 0.2.1

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 CHANGED
@@ -17,7 +17,7 @@ discord:
17
17
  # 令牌
18
18
  token: ''
19
19
  # 主人
20
- master_id: null
20
+ master_key: null
21
21
  # 前缀(非必填)
22
22
  intent: null
23
23
  # 活动 非必填)
package/lib/index.d.ts CHANGED
@@ -1,7 +1,9 @@
1
+ import * as alemonjs from 'alemonjs';
1
2
  import { DCClient } from './sdk/platform/discord/sdk/wss.js';
2
3
 
3
4
  type Client = typeof DCClient.prototype;
4
5
  declare const client: Client;
5
- declare const _default: () => typeof global.alemonjs;
6
+ declare const platform = "discord";
7
+ declare const _default: () => alemonjs.ClientAPI;
6
8
 
7
- export { type Client, client, _default as default };
9
+ export { type Client, client, _default as default, platform };
package/lib/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { defineBot, getConfig, createHash, Text, At, OnProcessor, useParse } from 'alemonjs';
1
+ import { defineBot, getConfigValue, useUserHashKey, OnProcessor } from 'alemonjs';
2
2
  import 'fs';
3
3
  import 'axios';
4
4
  import 'path';
@@ -9,10 +9,18 @@ import 'file-type';
9
9
  import 'form-data';
10
10
  import { DCClient } from './sdk/platform/discord/sdk/wss.js';
11
11
 
12
- const client = global.client;
12
+ const client = new Proxy({}, {
13
+ get: (_, prop) => {
14
+ if (prop in global.client) {
15
+ return global.client[prop];
16
+ }
17
+ return undefined;
18
+ }
19
+ });
20
+ const platform = 'discord';
13
21
  var index = defineBot(() => {
14
- const cfg = getConfig();
15
- const config = cfg.value?.discord;
22
+ const value = getConfigValue();
23
+ const config = value?.discord;
16
24
  if (!config)
17
25
  return;
18
26
  // 创建客户端
@@ -26,17 +34,14 @@ var index = defineBot(() => {
26
34
  // 消除bot消息
27
35
  if (event.author?.bot)
28
36
  return;
29
- const master_id = config?.master_id ?? [];
30
- const isMaster = master_id.includes(event.author.id);
37
+ const master_key = config?.master_key ?? [];
38
+ const isMaster = master_key.includes(event.author.id);
31
39
  // 艾特消息处理
32
40
  const at_users = [];
33
41
  // 获取艾特用户
34
42
  for (const item of event.mentions) {
35
43
  at_users.push({
36
- id: item.id,
37
- name: item.username,
38
- avatar: client.userAvatar(item.id, item.avatar),
39
- bot: item.bot
44
+ id: item.id
40
45
  });
41
46
  }
42
47
  // 清除 @ 相关的消息
@@ -44,26 +49,30 @@ var index = defineBot(() => {
44
49
  for await (const item of at_users) {
45
50
  msg = msg.replace(`<@${item.id}>`, '').trim();
46
51
  }
47
- const UserKey = createHash(`gui:${event.author.id}`);
52
+ const UserId = event.author.id;
53
+ const UserKey = useUserHashKey({
54
+ Platform: platform,
55
+ UserId: UserId
56
+ });
48
57
  let url = null;
49
58
  const UserAvatar = {
50
59
  toBuffer: async () => {
51
60
  if (!url) {
52
- url = client.userAvatar(event.author.id, event.author.avatar);
61
+ url = client.userAvatar(UserId, event.author.avatar);
53
62
  }
54
63
  const arrayBuffer = await fetch(url).then(res => res.arrayBuffer());
55
64
  return Buffer.from(arrayBuffer);
56
65
  },
57
66
  toBase64: async () => {
58
67
  if (!url) {
59
- url = client.userAvatar(event.author.id, event.author.avatar);
68
+ url = client.userAvatar(UserId, event.author.avatar);
60
69
  }
61
70
  const arrayBuffer = await fetch(url).then(res => res.arrayBuffer());
62
71
  return Buffer.from(arrayBuffer).toString('base64');
63
72
  },
64
73
  toURL: async () => {
65
74
  if (!url) {
66
- url = client.userAvatar(event.author.id, event.author.avatar);
75
+ url = client.userAvatar(UserId, event.author.avatar);
67
76
  }
68
77
  return url;
69
78
  }
@@ -71,38 +80,24 @@ var index = defineBot(() => {
71
80
  // 定义消
72
81
  const e = {
73
82
  // 事件类型
74
- Platform: 'discord',
75
- // 频道
83
+ Platform: platform,
84
+ // guild
76
85
  GuildId: event.guild_id,
77
- // 子频道
78
86
  ChannelId: event.channel_id,
79
- // 是否是主人
80
- IsMaster: isMaster,
81
- // 用户Id
82
- UserId: event.author.id,
83
- // 用户名
84
- UserName: event.author.username,
87
+ // user
88
+ UserId: UserId,
85
89
  UserKey,
86
- // 用户头像
90
+ UserName: event.author.username,
87
91
  UserAvatar: UserAvatar,
88
- // 格式化数据
92
+ IsMaster: isMaster,
93
+ IsBot: false,
94
+ // message
89
95
  MessageId: event.id,
90
- // 用户消息
91
- MessageBody: [
92
- Text(msg),
93
- ...at_users.map(item => At(item.id, 'user', {
94
- name: item.name,
95
- avatar: item.avatar,
96
- bot: item.bot
97
- }))
98
- ],
99
96
  MessageText: msg,
100
- // 用户openId
101
97
  OpenId: '',
102
- // 创建时间
103
98
  CreateAt: Date.now(),
99
+ // other
104
100
  tag: 'MESSAGE_CREATE',
105
- //
106
101
  value: null
107
102
  };
108
103
  // 当访问的时候获取
@@ -115,9 +110,7 @@ var index = defineBot(() => {
115
110
  OnProcessor(e, 'message.create');
116
111
  });
117
112
  // 发送错误时
118
- client.on('ERROR', msg => {
119
- console.error(msg);
120
- });
113
+ client.on('ERROR', console.error);
121
114
  global.client = client;
122
115
  return {
123
116
  api: {
@@ -125,25 +118,103 @@ var index = defineBot(() => {
125
118
  send: (event, val) => {
126
119
  if (val.length < 0)
127
120
  return Promise.all([]);
128
- const content = useParse({
129
- MessageBody: val
130
- }, 'Text');
121
+ const content = val
122
+ .filter(item => item.type == 'Link' || item.type == 'Mention' || item.type == 'Text')
123
+ .map(item => {
124
+ if (item.type == 'Link') {
125
+ return `[${item.options?.title ?? item.value}](${item.value})`;
126
+ }
127
+ else if (item.type == 'Mention') {
128
+ if (item.value == 'everyone' ||
129
+ item.value == 'all' ||
130
+ item.value == '' ||
131
+ typeof item.value != 'string') {
132
+ return `<@everyone>`;
133
+ }
134
+ if (item.options?.belong == 'user') {
135
+ return `<@${item.value}>`;
136
+ }
137
+ else if (item.options?.belong == 'channel') {
138
+ return `<#${item.value}>`;
139
+ }
140
+ return '';
141
+ }
142
+ else if (item.type == 'Text') {
143
+ if (item.options?.style == 'block') {
144
+ return `\`${item.value}\``;
145
+ }
146
+ else if (item.options?.style == 'italic') {
147
+ return `*${item.value}*`;
148
+ }
149
+ else if (item.options?.style == 'bold') {
150
+ return `**${item.value}**`;
151
+ }
152
+ else if (item.options?.style == 'strikethrough') {
153
+ return `~~${item.value}~~`;
154
+ }
155
+ return item.value;
156
+ }
157
+ })
158
+ .join('');
131
159
  if (content) {
132
160
  return Promise.all([content].map(item => client.channelsMessages(event.ChannelId, {
133
161
  content: item
134
162
  })));
135
163
  }
136
- const images = useParse({
137
- MessageBody: val
138
- }, 'Image');
164
+ const images = val.filter(item => item.type == 'Image').map(item => item.value);
139
165
  if (images) {
140
166
  return Promise.all(images.map(item => client.channelsMessagesImage(event.ChannelId, item)));
141
167
  }
142
168
  return Promise.all([]);
169
+ },
170
+ mention: async (e) => {
171
+ const event = e.value;
172
+ const MessageMention = event.mentions.map(item => {
173
+ let url = null;
174
+ const Platform = 'discord';
175
+ const UserId = item.id;
176
+ const avatar = event.author.avatar;
177
+ const value = getConfigValue();
178
+ const config = value?.discord;
179
+ const master_key = config?.master_key ?? [];
180
+ const UserAvatar = {
181
+ toBuffer: async () => {
182
+ if (!url) {
183
+ url = client.userAvatar(UserId, avatar);
184
+ }
185
+ const arrayBuffer = await fetch(url).then(res => res.arrayBuffer());
186
+ return Buffer.from(arrayBuffer);
187
+ },
188
+ toBase64: async () => {
189
+ if (!url) {
190
+ url = client.userAvatar(UserId, avatar);
191
+ }
192
+ const arrayBuffer = await fetch(url).then(res => res.arrayBuffer());
193
+ return Buffer.from(arrayBuffer).toString('base64');
194
+ },
195
+ toURL: async () => {
196
+ if (!url) {
197
+ url = client.userAvatar(UserId, avatar);
198
+ }
199
+ return url;
200
+ }
201
+ };
202
+ return {
203
+ UserId: item.id,
204
+ IsMaster: master_key.includes(UserId),
205
+ IsBot: item.bot,
206
+ UserAvatar,
207
+ UserKey: useUserHashKey({
208
+ Platform: Platform,
209
+ UserId: UserId
210
+ })
211
+ };
212
+ });
213
+ return MessageMention;
143
214
  }
144
215
  }
145
216
  }
146
217
  };
147
218
  });
148
219
 
149
- export { client, index as default };
220
+ export { client, index as default, platform };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alemonjs/discord",
3
- "version": "0.1.1",
3
+ "version": "0.2.1",
4
4
  "description": "discord-bot",
5
5
  "author": "lemonade",
6
6
  "license": "MIT",