@alemonjs/discord 0.1.1 → 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, 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';
@@ -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,26 +42,30 @@ var index = defineBot(() => {
44
42
  for await (const item of at_users) {
45
43
  msg = msg.replace(`<@${item.id}>`, '').trim();
46
44
  }
47
- const UserKey = createHash(`gui:${event.author.id}`);
45
+ const UserId = event.author.id;
46
+ const UserKey = useUserHashKey({
47
+ Platform: Platform,
48
+ UserId: UserId
49
+ });
48
50
  let url = null;
49
51
  const UserAvatar = {
50
52
  toBuffer: async () => {
51
53
  if (!url) {
52
- url = client.userAvatar(event.author.id, event.author.avatar);
54
+ url = client.userAvatar(UserId, event.author.avatar);
53
55
  }
54
56
  const arrayBuffer = await fetch(url).then(res => res.arrayBuffer());
55
57
  return Buffer.from(arrayBuffer);
56
58
  },
57
59
  toBase64: async () => {
58
60
  if (!url) {
59
- url = client.userAvatar(event.author.id, event.author.avatar);
61
+ url = client.userAvatar(UserId, event.author.avatar);
60
62
  }
61
63
  const arrayBuffer = await fetch(url).then(res => res.arrayBuffer());
62
64
  return Buffer.from(arrayBuffer).toString('base64');
63
65
  },
64
66
  toURL: async () => {
65
67
  if (!url) {
66
- url = client.userAvatar(event.author.id, event.author.avatar);
68
+ url = client.userAvatar(UserId, event.author.avatar);
67
69
  }
68
70
  return url;
69
71
  }
@@ -71,38 +73,24 @@ var index = defineBot(() => {
71
73
  // 定义消
72
74
  const e = {
73
75
  // 事件类型
74
- Platform: 'discord',
75
- // 频道
76
+ Platform: Platform,
77
+ // guild
76
78
  GuildId: event.guild_id,
77
- // 子频道
78
79
  ChannelId: event.channel_id,
79
- // 是否是主人
80
- IsMaster: isMaster,
81
- // 用户Id
82
- UserId: event.author.id,
83
- // 用户名
84
- UserName: event.author.username,
80
+ // user
81
+ UserId: UserId,
85
82
  UserKey,
86
- // 用户头像
83
+ UserName: event.author.username,
87
84
  UserAvatar: UserAvatar,
88
- // 格式化数据
85
+ IsMaster: isMaster,
86
+ IsBot: false,
87
+ // message
89
88
  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
89
  MessageText: msg,
100
- // 用户openId
101
90
  OpenId: '',
102
- // 创建时间
103
91
  CreateAt: Date.now(),
92
+ // other
104
93
  tag: 'MESSAGE_CREATE',
105
- //
106
94
  value: null
107
95
  };
108
96
  // 当访问的时候获取
@@ -115,9 +103,7 @@ var index = defineBot(() => {
115
103
  OnProcessor(e, 'message.create');
116
104
  });
117
105
  // 发送错误时
118
- client.on('ERROR', msg => {
119
- console.error(msg);
120
- });
106
+ client.on('ERROR', console.error);
121
107
  global.client = client;
122
108
  return {
123
109
  api: {
@@ -125,21 +111,99 @@ var index = defineBot(() => {
125
111
  send: (event, val) => {
126
112
  if (val.length < 0)
127
113
  return Promise.all([]);
128
- const content = useParse({
129
- MessageBody: val
130
- }, '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('');
131
152
  if (content) {
132
153
  return Promise.all([content].map(item => client.channelsMessages(event.ChannelId, {
133
154
  content: item
134
155
  })));
135
156
  }
136
- const images = useParse({
137
- MessageBody: val
138
- }, 'Image');
157
+ const images = val.filter(item => item.type == 'Image').map(item => item.value);
139
158
  if (images) {
140
159
  return Promise.all(images.map(item => client.channelsMessagesImage(event.ChannelId, item)));
141
160
  }
142
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;
143
207
  }
144
208
  }
145
209
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alemonjs/discord",
3
- "version": "0.1.1",
3
+ "version": "0.2.0",
4
4
  "description": "discord-bot",
5
5
  "author": "lemonade",
6
6
  "license": "MIT",