@alemonjs/onebot 2.1.0-alpha.3 → 2.1.0-alpha.5
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 +91 -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,95 @@ 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
|
+
const message = await Promise.all(val.map(async (item) => {
|
|
104
|
+
if (item.type == 'Text') {
|
|
105
|
+
return {
|
|
106
|
+
type: 'text',
|
|
107
|
+
data: {
|
|
108
|
+
text: item.value
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
else if (item.type == 'Mention') {
|
|
113
|
+
const options = item.options || {};
|
|
114
|
+
if (options.belong === 'everyone') {
|
|
115
|
+
return {
|
|
116
|
+
type: 'at',
|
|
117
|
+
data: {
|
|
118
|
+
qq: 'all',
|
|
119
|
+
nickname: ''
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
else if (options.belong === 'user') {
|
|
124
|
+
return {
|
|
125
|
+
type: 'at',
|
|
126
|
+
data: {
|
|
127
|
+
qq: item.value,
|
|
116
128
|
}
|
|
117
|
-
|
|
118
|
-
}
|
|
119
|
-
return
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
return {
|
|
132
|
+
type: 'text',
|
|
133
|
+
data: {
|
|
134
|
+
text: ''
|
|
135
|
+
}
|
|
136
|
+
};
|
|
120
137
|
}
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
const db = readFileSync(item.value);
|
|
127
|
-
data = db;
|
|
138
|
+
else if (item.type == 'Image') {
|
|
139
|
+
return {
|
|
140
|
+
type: 'image',
|
|
141
|
+
data: {
|
|
142
|
+
file: `base64://${item.value}`
|
|
128
143
|
}
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
else if (item.type == 'ImageFile') {
|
|
147
|
+
const db = readFileSync(item.value);
|
|
148
|
+
return {
|
|
149
|
+
type: 'image',
|
|
150
|
+
data: {
|
|
151
|
+
file: `base64://${db.toString('base64')}`
|
|
132
152
|
}
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
else if (item.type == 'ImageURL') {
|
|
156
|
+
const db = await getBufferByURL(item.value);
|
|
157
|
+
return {
|
|
158
|
+
type: 'image',
|
|
159
|
+
data: {
|
|
160
|
+
file: `base64://${db.toString('base64')}`
|
|
136
161
|
}
|
|
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
|
-
}));
|
|
162
|
+
};
|
|
152
163
|
}
|
|
164
|
+
}));
|
|
165
|
+
return message;
|
|
166
|
+
};
|
|
167
|
+
/**
|
|
168
|
+
*
|
|
169
|
+
* @param ChannelId
|
|
170
|
+
* @param val
|
|
171
|
+
* @returns
|
|
172
|
+
*/
|
|
173
|
+
const sendGroup = async (ChannelId, val) => {
|
|
174
|
+
if (val.length < 0)
|
|
175
|
+
return [];
|
|
176
|
+
try {
|
|
177
|
+
const message = await DataToMessage(val);
|
|
178
|
+
const res = await client.sendGroupMessage({
|
|
179
|
+
group_id: ChannelId,
|
|
180
|
+
message: message
|
|
181
|
+
});
|
|
182
|
+
return [createResult(ResultCode.Ok, 'client.groupOpenMessages', res)];
|
|
153
183
|
}
|
|
154
184
|
catch (error) {
|
|
155
185
|
return [createResult(ResultCode.Fail, 'client.groupOpenMessages', error)];
|
|
156
186
|
}
|
|
157
|
-
return [];
|
|
158
187
|
};
|
|
159
188
|
/**
|
|
160
189
|
*
|
|
@@ -164,63 +193,18 @@ var index = () => {
|
|
|
164
193
|
*/
|
|
165
194
|
const sendPrivate = async (UserId, val) => {
|
|
166
195
|
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('');
|
|
196
|
+
return [];
|
|
172
197
|
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
|
-
}
|
|
198
|
+
const message = await DataToMessage(val);
|
|
199
|
+
const res = await client.sendPrivateMessage({
|
|
200
|
+
user_id: UserId,
|
|
201
|
+
message: message
|
|
202
|
+
});
|
|
203
|
+
return [createResult(ResultCode.Ok, 'client.groupOpenMessages', res)];
|
|
219
204
|
}
|
|
220
205
|
catch (error) {
|
|
221
|
-
return [createResult(ResultCode.Fail, 'client.
|
|
206
|
+
return [createResult(ResultCode.Fail, 'client.groupOpenMessages', error)];
|
|
222
207
|
}
|
|
223
|
-
return [];
|
|
224
208
|
};
|
|
225
209
|
const api = {
|
|
226
210
|
active: {
|
|
@@ -259,7 +243,7 @@ var index = () => {
|
|
|
259
243
|
if (UserId == 'all') {
|
|
260
244
|
continue;
|
|
261
245
|
}
|
|
262
|
-
if (UserId ==
|
|
246
|
+
if (UserId == BotMe.id) {
|
|
263
247
|
isBot = true;
|
|
264
248
|
}
|
|
265
249
|
const [isMaster, UserKey] = getMaster(UserId);
|