@alemonjs/discord 0.2.10 → 2.1.0-alpha.0

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/dist/index.html CHANGED
@@ -6,7 +6,7 @@
6
6
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
7
7
  <title>AlemonJS</title>
8
8
  <script type="module" crossorigin src="/assets/index.js"></script>
9
- <link rel="stylesheet" crossorigin href="/assets/index.css" />
9
+ <link rel="stylesheet" crossorigin href="/assets/index.css">
10
10
  </head>
11
11
 
12
12
  <body>
package/lib/index.d.ts CHANGED
@@ -1,10 +1,4 @@
1
- import * as alemonjs from 'alemonjs';
2
- import { DCClient } from './sdk/wss.js';
3
-
4
- type Client = typeof DCClient.prototype;
5
- declare const client: Client;
6
1
  declare const platform = "discord";
7
- declare const _default: alemonjs.DefinePlatformValue;
2
+ declare const _default: () => void;
8
3
 
9
- export { client, _default as default, platform };
10
- export type { Client };
4
+ export { _default as default, platform };
package/lib/index.js CHANGED
@@ -1,21 +1,11 @@
1
1
  import './env.js';
2
- import { definePlatform, getConfigValue, useUserHashKey, onProcessor } from 'alemonjs';
2
+ import { getConfigValue, cbpPlatform, useUserHashKey } from 'alemonjs';
3
3
  import { sendchannel, senduser } from './send.js';
4
4
  import { DCClient } from './sdk/wss.js';
5
5
  import { AvailableIntentsEventsEnum } from './sdk/types.js';
6
6
 
7
- const client = new Proxy({}, {
8
- get: (_, prop) => {
9
- if (prop in global.client) {
10
- const original = global.client[prop];
11
- // 防止函数内this丢失
12
- return typeof original === 'function' ? original.bind(global.client) : original;
13
- }
14
- return undefined;
15
- }
16
- });
17
7
  const platform = 'discord';
18
- var index = definePlatform(() => {
8
+ var index = () => {
19
9
  let value = getConfigValue();
20
10
  if (!value)
21
11
  value = {};
@@ -27,6 +17,12 @@ var index = definePlatform(() => {
27
17
  shard: config?.shard ?? [0, 1],
28
18
  intent: config?.intent ?? AvailableIntentsEventsEnum
29
19
  });
20
+ /**
21
+ * 连接 alemonjs 服务器。
22
+ * 向 alemonjs 推送标准信息
23
+ */
24
+ const url = `ws://127.0.0.1:${process.env?.port || config?.port || 17117}`;
25
+ const cbp = cbpPlatform(url);
30
26
  // 连接
31
27
  client.connect(config?.gatewayURL);
32
28
  const ImageURLToBuffer = async (url) => {
@@ -90,8 +86,7 @@ var index = definePlatform(() => {
90
86
  const isMaster = master_key.includes(UserKey);
91
87
  const UserAvatar = createUserAvatar(UserId, event.author.avatar);
92
88
  if (event.type == 0 && event.member) {
93
- // 处理消息
94
- onProcessor('message.create', {
89
+ const e = {
95
90
  name: 'message.create',
96
91
  // 事件类型
97
92
  Platform: platform,
@@ -112,12 +107,13 @@ var index = definePlatform(() => {
112
107
  CreateAt: Date.now(),
113
108
  // other
114
109
  tag: 'message.create',
115
- value: null
116
- }, event);
110
+ value: event
111
+ };
112
+ cbp.send(e);
117
113
  }
118
114
  else if (event.type == 0 && !event.member) {
119
115
  // 处理消息
120
- onProcessor('private.message.create', {
116
+ const e = {
121
117
  name: 'private.message.create',
122
118
  // 事件类型
123
119
  Platform: platform,
@@ -138,8 +134,9 @@ var index = definePlatform(() => {
138
134
  CreateAt: Date.now(),
139
135
  // other
140
136
  tag: 'private.message.create',
141
- value: null
142
- }, event);
137
+ value: event
138
+ };
139
+ cbp.send(e);
143
140
  }
144
141
  else ;
145
142
  });
@@ -159,7 +156,7 @@ var index = definePlatform(() => {
159
156
  const MessageText = event.data.custom_id;
160
157
  if (isPrivate) {
161
158
  // 处理消息
162
- onProcessor('private.interaction.create', {
159
+ const e = {
163
160
  name: 'private.interaction.create',
164
161
  // 事件类型
165
162
  Platform: platform,
@@ -180,12 +177,12 @@ var index = definePlatform(() => {
180
177
  CreateAt: Date.now(),
181
178
  // other
182
179
  tag: 'private.interaction.create',
183
- value: null
184
- }, event);
180
+ value: event
181
+ };
182
+ cbp.send(e);
185
183
  }
186
184
  else {
187
- // 处理消息
188
- onProcessor('interaction.create', {
185
+ const e = {
189
186
  name: 'interaction.create',
190
187
  // 事件类型
191
188
  Platform: platform,
@@ -206,85 +203,107 @@ var index = definePlatform(() => {
206
203
  CreateAt: Date.now(),
207
204
  // other
208
205
  tag: 'interaction.create',
209
- value: null
210
- }, event);
206
+ value: event
207
+ };
208
+ cbp.send(e);
211
209
  }
212
210
  client.interactionsCallback(event.id, event.token, MessageText);
213
211
  });
214
212
  // 发送错误时
215
213
  client.on('ERROR', console.error);
216
- global.client = client;
217
- return {
218
- platform,
219
- api: {
220
- active: {
221
- send: {
222
- channel: (channel_id, val) => {
223
- return sendchannel(client, { channel_id }, val);
224
- },
225
- user: async (author_id, val) => {
226
- return senduser(client, { author_id: author_id }, val);
227
- }
214
+ const api = {
215
+ active: {
216
+ send: {
217
+ channel: (channel_id, val) => {
218
+ return sendchannel(client, { channel_id }, val);
219
+ },
220
+ user: async (author_id, val) => {
221
+ return senduser(client, { author_id: author_id }, val);
228
222
  }
229
- },
230
- use: {
231
- send: async (event, val) => {
232
- if (val.length < 0) {
233
- return Promise.all([]);
234
- }
235
- const tag = event.tag;
236
- if (tag == 'message.create') {
237
- const ChannelId = event.value.channel_id;
238
- return sendchannel(client, { channel_id: ChannelId }, val);
239
- }
240
- else if (tag == 'private.message.create') {
241
- const UserId = event.value.author.id;
242
- const ChannelId = event.value.channel_id;
243
- return senduser(client, {
244
- channel_id: ChannelId,
245
- author_id: UserId
246
- }, val);
247
- }
248
- else if (tag == 'interaction.create') {
249
- const ChannelId = event.value.channel_id;
250
- return sendchannel(client, { channel_id: ChannelId }, val);
251
- }
252
- else if (tag == 'private.interaction.create') {
253
- const UserId = event.value.user.id;
254
- const ChannelId = event.value.channel_id;
255
- return senduser(client, {
256
- channel_id: ChannelId,
257
- author_id: UserId
258
- }, val);
259
- }
223
+ }
224
+ },
225
+ use: {
226
+ send: async (event, val) => {
227
+ if (val.length < 0) {
260
228
  return Promise.all([]);
261
- },
262
- mention: async (e) => {
263
- const event = e.value;
264
- const MessageMention = event.mentions.map(item => {
265
- const UserId = item.id;
266
- const avatar = event.author.avatar;
267
- const value = getConfigValue();
268
- const config = value?.discord;
269
- const master_key = config?.master_key ?? [];
270
- const UserAvatar = createUserAvatar(UserId, avatar);
271
- const UserKey = useUserHashKey({
272
- Platform: platform,
273
- UserId: UserId
274
- });
275
- return {
276
- UserId: item.id,
277
- IsMaster: master_key.includes(UserId),
278
- IsBot: item.bot,
279
- UserAvatar,
280
- UserKey
281
- };
282
- });
283
- return MessageMention;
284
229
  }
230
+ const tag = event.tag;
231
+ if (tag == 'message.create') {
232
+ const ChannelId = event.value.channel_id;
233
+ return sendchannel(client, { channel_id: ChannelId }, val);
234
+ }
235
+ else if (tag == 'private.message.create') {
236
+ const UserId = event.value.author.id;
237
+ const ChannelId = event.value.channel_id;
238
+ return senduser(client, {
239
+ channel_id: ChannelId,
240
+ author_id: UserId
241
+ }, val);
242
+ }
243
+ else if (tag == 'interaction.create') {
244
+ const ChannelId = event.value.channel_id;
245
+ return sendchannel(client, { channel_id: ChannelId }, val);
246
+ }
247
+ else if (tag == 'private.interaction.create') {
248
+ const UserId = event.value.user.id;
249
+ const ChannelId = event.value.channel_id;
250
+ return senduser(client, {
251
+ channel_id: ChannelId,
252
+ author_id: UserId
253
+ }, val);
254
+ }
255
+ return Promise.all([]);
256
+ },
257
+ mention: async (e) => {
258
+ const event = e.value;
259
+ const MessageMention = event.mentions.map(item => {
260
+ const UserId = item.id;
261
+ const avatar = event.author.avatar;
262
+ const value = getConfigValue();
263
+ const config = value?.discord;
264
+ const master_key = config?.master_key ?? [];
265
+ const UserAvatar = createUserAvatar(UserId, avatar);
266
+ const UserKey = useUserHashKey({
267
+ Platform: platform,
268
+ UserId: UserId
269
+ });
270
+ return {
271
+ UserId: item.id,
272
+ IsMaster: master_key.includes(UserId),
273
+ IsBot: item.bot,
274
+ UserAvatar,
275
+ UserKey
276
+ };
277
+ });
278
+ return MessageMention;
285
279
  }
286
280
  }
287
281
  };
288
- });
282
+ cbp.onactions(async (data, consume) => {
283
+ if (data.action === 'message.send') {
284
+ const event = data.payload.event;
285
+ const paramFormat = data.payload.params.format;
286
+ const res = await api.use.send(event, paramFormat);
287
+ consume(res);
288
+ }
289
+ else if (data.action === 'message.send.channel') {
290
+ const channel_id = data.payload.ChannelId;
291
+ const val = data.payload.params.format;
292
+ const res = await api.active.send.channel(channel_id, val);
293
+ consume(res);
294
+ }
295
+ else if (data.action === 'message.send.user') {
296
+ const user_id = data.payload.UserId;
297
+ const val = data.payload.params.format;
298
+ const res = await api.active.send.user(user_id, val);
299
+ consume(res);
300
+ }
301
+ else if (data.action === 'mention.get') {
302
+ const event = data.payload.event;
303
+ const res = await api.use.mention(event);
304
+ consume(res);
305
+ }
306
+ });
307
+ };
289
308
 
290
- export { client, index as default, platform };
309
+ export { index as default, platform };