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