@alemonjs/onebot 2.1.0-alpha.4 → 2.1.0-alpha.6
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/lib/db.js +4 -0
- package/lib/index.js +94 -107
- package/package.json +1 -1
package/lib/db.js
ADDED
package/lib/index.js
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
import { cbpPlatform, createResult, ResultCode } from 'alemonjs';
|
|
2
2
|
import { getBufferByURL } from 'alemonjs/utils';
|
|
3
|
-
import { OneBotClient } from './sdk/wss.js';
|
|
4
3
|
import { readFileSync } from 'fs';
|
|
5
4
|
import { getOneBotConfig, getMaster, platform } from './config.js';
|
|
5
|
+
import { OneBotClient } from './sdk/wss.js';
|
|
6
|
+
import { BotMe } from './db.js';
|
|
6
7
|
export { OneBotAPI as API } from './sdk/api.js';
|
|
7
8
|
export { useClient, useValue } from './hook.js';
|
|
8
9
|
|
|
9
|
-
const MyBot = {
|
|
10
|
-
id: ''};
|
|
11
10
|
var index = () => {
|
|
12
11
|
const config = getOneBotConfig();
|
|
13
12
|
const client = new OneBotClient({
|
|
@@ -37,7 +36,7 @@ var index = () => {
|
|
|
37
36
|
};
|
|
38
37
|
client.on('META', event => {
|
|
39
38
|
if (event?.self_id) {
|
|
40
|
-
|
|
39
|
+
BotMe.id = String(event.self_id);
|
|
41
40
|
}
|
|
42
41
|
});
|
|
43
42
|
client.on('MESSAGES', event => {
|
|
@@ -96,65 +95,98 @@ var index = () => {
|
|
|
96
95
|
client.on('ERROR', event => {
|
|
97
96
|
logger.error(event);
|
|
98
97
|
});
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
98
|
+
/**
|
|
99
|
+
* @param val
|
|
100
|
+
* @returns
|
|
101
|
+
*/
|
|
102
|
+
const DataToMessage = async (val = []) => {
|
|
103
|
+
// 空元素
|
|
104
|
+
const empty = {
|
|
105
|
+
type: 'text',
|
|
106
|
+
data: {
|
|
107
|
+
text: ''
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
const message = await Promise.all(val.map(async (item) => {
|
|
111
|
+
if (item.type == 'Text') {
|
|
112
|
+
return {
|
|
113
|
+
type: 'text',
|
|
114
|
+
data: {
|
|
115
|
+
text: item.value
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
else if (item.type == 'Mention') {
|
|
120
|
+
const options = item.options || {};
|
|
121
|
+
if (options.belong === 'everyone') {
|
|
122
|
+
return {
|
|
123
|
+
type: 'at',
|
|
124
|
+
data: {
|
|
125
|
+
qq: 'all',
|
|
126
|
+
nickname: ''
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
else if (options.belong === 'user') {
|
|
131
|
+
return {
|
|
132
|
+
type: 'at',
|
|
133
|
+
data: {
|
|
134
|
+
qq: item.value
|
|
116
135
|
}
|
|
117
|
-
|
|
118
|
-
}
|
|
119
|
-
return
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
return empty;
|
|
120
139
|
}
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
const db = readFileSync(item.value);
|
|
127
|
-
data = db;
|
|
140
|
+
else if (item.type == 'Image') {
|
|
141
|
+
return {
|
|
142
|
+
type: 'image',
|
|
143
|
+
data: {
|
|
144
|
+
file: `base64://${item.value}`
|
|
128
145
|
}
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
else if (item.type == 'ImageFile') {
|
|
149
|
+
const db = readFileSync(item.value);
|
|
150
|
+
return {
|
|
151
|
+
type: 'image',
|
|
152
|
+
data: {
|
|
153
|
+
file: `base64://${db.toString('base64')}`
|
|
132
154
|
}
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
else if (item.type == 'ImageURL') {
|
|
158
|
+
const db = await getBufferByURL(item.value);
|
|
159
|
+
return {
|
|
160
|
+
type: 'image',
|
|
161
|
+
data: {
|
|
162
|
+
file: `base64://${db.toString('base64')}`
|
|
136
163
|
}
|
|
137
|
-
|
|
138
|
-
group_id: ChannelId,
|
|
139
|
-
message: [
|
|
140
|
-
{
|
|
141
|
-
type: 'image',
|
|
142
|
-
data: {
|
|
143
|
-
file: `base64://${data.toString('base64')}`
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
]
|
|
147
|
-
});
|
|
148
|
-
return createResult(ResultCode.Ok, 'client.groupOpenMessages', {
|
|
149
|
-
id: null
|
|
150
|
-
});
|
|
151
|
-
}));
|
|
164
|
+
};
|
|
152
165
|
}
|
|
166
|
+
return empty;
|
|
167
|
+
}));
|
|
168
|
+
return message;
|
|
169
|
+
};
|
|
170
|
+
/**
|
|
171
|
+
*
|
|
172
|
+
* @param ChannelId
|
|
173
|
+
* @param val
|
|
174
|
+
* @returns
|
|
175
|
+
*/
|
|
176
|
+
const sendGroup = async (ChannelId, val) => {
|
|
177
|
+
if (val.length < 0)
|
|
178
|
+
return [];
|
|
179
|
+
try {
|
|
180
|
+
const message = await DataToMessage(val);
|
|
181
|
+
const res = await client.sendGroupMessage({
|
|
182
|
+
group_id: ChannelId,
|
|
183
|
+
message: message
|
|
184
|
+
});
|
|
185
|
+
return [createResult(ResultCode.Ok, 'client.groupOpenMessages', res)];
|
|
153
186
|
}
|
|
154
187
|
catch (error) {
|
|
155
188
|
return [createResult(ResultCode.Fail, 'client.groupOpenMessages', error)];
|
|
156
189
|
}
|
|
157
|
-
return [];
|
|
158
190
|
};
|
|
159
191
|
/**
|
|
160
192
|
*
|
|
@@ -164,63 +196,18 @@ var index = () => {
|
|
|
164
196
|
*/
|
|
165
197
|
const sendPrivate = async (UserId, val) => {
|
|
166
198
|
if (val.length < 0)
|
|
167
|
-
return
|
|
168
|
-
const content = val
|
|
169
|
-
.filter(item => item.type == 'Link' || item.type == 'Mention' || item.type == 'Text')
|
|
170
|
-
.map(item => item.value)
|
|
171
|
-
.join('');
|
|
199
|
+
return [];
|
|
172
200
|
try {
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
data: {
|
|
180
|
-
text: content
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
]
|
|
184
|
-
});
|
|
185
|
-
return [createResult(ResultCode.Ok, 'client.sendPrivateMessage', res)];
|
|
186
|
-
}
|
|
187
|
-
const images = val.filter(item => item.type == 'Image' || item.type == 'ImageFile' || item.type == 'ImageURL');
|
|
188
|
-
if (images) {
|
|
189
|
-
return Promise.all(images.map(async (item) => {
|
|
190
|
-
let data = null;
|
|
191
|
-
if (item.type === 'ImageFile') {
|
|
192
|
-
const db = readFileSync(item.value);
|
|
193
|
-
data = db;
|
|
194
|
-
}
|
|
195
|
-
else if (item.type === 'ImageURL') {
|
|
196
|
-
const db = await getBufferByURL(item.value);
|
|
197
|
-
data = db;
|
|
198
|
-
}
|
|
199
|
-
else {
|
|
200
|
-
// data = item.value
|
|
201
|
-
data = Buffer.from(item.value, 'base64');
|
|
202
|
-
}
|
|
203
|
-
client.sendPrivateMessage({
|
|
204
|
-
user_id: UserId,
|
|
205
|
-
message: [
|
|
206
|
-
{
|
|
207
|
-
type: 'image',
|
|
208
|
-
data: {
|
|
209
|
-
file: `base64://${data.toString('base64')}`
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
]
|
|
213
|
-
});
|
|
214
|
-
return createResult(ResultCode.Ok, 'client.sendPrivateMessage', {
|
|
215
|
-
id: null
|
|
216
|
-
});
|
|
217
|
-
}));
|
|
218
|
-
}
|
|
201
|
+
const message = await DataToMessage(val);
|
|
202
|
+
const res = await client.sendPrivateMessage({
|
|
203
|
+
user_id: UserId,
|
|
204
|
+
message: message
|
|
205
|
+
});
|
|
206
|
+
return [createResult(ResultCode.Ok, 'client.groupOpenMessages', res)];
|
|
219
207
|
}
|
|
220
208
|
catch (error) {
|
|
221
|
-
return [createResult(ResultCode.Fail, 'client.
|
|
209
|
+
return [createResult(ResultCode.Fail, 'client.groupOpenMessages', error)];
|
|
222
210
|
}
|
|
223
|
-
return [];
|
|
224
211
|
};
|
|
225
212
|
const api = {
|
|
226
213
|
active: {
|
|
@@ -259,7 +246,7 @@ var index = () => {
|
|
|
259
246
|
if (UserId == 'all') {
|
|
260
247
|
continue;
|
|
261
248
|
}
|
|
262
|
-
if (UserId ==
|
|
249
|
+
if (UserId == BotMe.id) {
|
|
263
250
|
isBot = true;
|
|
264
251
|
}
|
|
265
252
|
const [isMaster, UserKey] = getMaster(UserId);
|