onebots 0.4.59 → 0.4.60

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/adapter.d.ts CHANGED
@@ -5,7 +5,7 @@ import { OneBot } from "./onebot";
5
5
  import { Dict } from "@zhinjs/shared";
6
6
  import { Logger } from "log4js";
7
7
  import { V11 } from "./service/V11";
8
- export declare abstract class Adapter<T extends string = string> extends EventEmitter {
8
+ export declare abstract class Adapter<T extends string = string, Sendable = any> extends EventEmitter {
9
9
  #private;
10
10
  app: App;
11
11
  platform: T;
@@ -13,6 +13,9 @@ export declare abstract class Adapter<T extends string = string> extends EventEm
13
13
  oneBots: Map<string, OneBot>;
14
14
  icon: string;
15
15
  protected constructor(app: App, platform: T, config: Adapter.Configs[T]);
16
+ materialize(content: string): string;
17
+ toCqcode<V extends OneBot.Version>(version: V, messageArr: OneBot.Segment<V>[]): string;
18
+ fromCqcode<V extends OneBot.Version>(version: V, message: string): OneBot.Segment<V>[];
16
19
  transformMessage(uin: string, version: OneBot.Version, message: any): string | (import("./service/V12").V12.Segment | V11.Segment)[];
17
20
  getOneBot<C = any>(uin: string): OneBot<C>;
18
21
  get logger(): Logger;
@@ -37,15 +40,13 @@ export declare abstract class Adapter<T extends string = string> extends EventEm
37
40
  start(uin?: string): Promise<any>;
38
41
  stop(uin?: string, force?: boolean): Promise<any>;
39
42
  }
40
- export interface Adapter extends Adapter.Base {
43
+ export interface Adapter<T extends string = string, Sendable = any> extends Adapter.Base<Sendable> {
41
44
  call<V extends OneBot.Version>(uin: string, version: V, method: string, args?: any[]): Promise<any>;
42
45
  }
43
46
  export declare namespace Adapter {
44
- interface Base {
45
- toSegment<V extends OneBot.Version, M = any>(version: V, message: M): OneBot.Segment<V>[];
46
- fromSegment<V extends OneBot.Version>(version: V, segment: OneBot.Segment<V>): OneBot.MessageElement<V>[];
47
- toCqcode<V extends OneBot.Version>(version: V, message: OneBot.MessageElement<V>[]): string;
48
- fromCqcode<V extends OneBot.Version>(version: V, message: string): OneBot.MessageElement<V>[];
47
+ interface Base<Sendable = any> {
48
+ toSegment<V extends OneBot.Version>(version: V, message: Sendable): OneBot.Segment<V>[];
49
+ fromSegment<V extends OneBot.Version>(version: V, segment: OneBot.Segment<V>[]): Sendable;
49
50
  getSelfInfo<V extends OneBot.Version>(uin: string, version: V): OneBot.SelfInfo<V>;
50
51
  /** 格式化事件 */
51
52
  formatEventPayload<V extends OneBot.Version>(uin: string, version: V, event: string, payload: Dict): OneBot.Payload<V>;
@@ -57,12 +58,12 @@ export declare namespace Adapter {
57
58
  getFriendList<V extends OneBot.Version>(uin: string, version: V): Promise<OneBot.UserInfo<V>[]>;
58
59
  getGroupMemberList<V extends OneBot.Version>(uin: string, version: V, args: [string]): Promise<OneBot.GroupMemberInfo<V>[]>;
59
60
  /** 发送群消息 */
60
- sendGroupMessage<V extends OneBot.Version>(uin: string, version: V, args: [string, OneBot.MessageElement<V>[], string]): Promise<OneBot.MessageRet<V>>;
61
+ sendGroupMessage<V extends OneBot.Version>(uin: string, version: V, args: [string, OneBot.Segment<V>[], string]): Promise<OneBot.MessageRet<V>>;
61
62
  /** 发送私聊消息 */
62
- sendPrivateMessage<V extends OneBot.Version>(uin: string, version: V, args: [string, OneBot.MessageElement<V>[], string]): Promise<OneBot.MessageRet<V>>;
63
- sendGuildMessage<V extends OneBot.Version>(uin: string, version: V, args: [string, OneBot.MessageElement<V>[], string]): Promise<OneBot.MessageRet<V>>;
63
+ sendPrivateMessage<V extends OneBot.Version>(uin: string, version: V, args: [string, OneBot.Segment<V>[], string]): Promise<OneBot.MessageRet<V>>;
64
+ sendGuildMessage<V extends OneBot.Version>(uin: string, version: V, args: [string, OneBot.Segment<V>[], string]): Promise<OneBot.MessageRet<V>>;
64
65
  /** 发送私聊消息 */
65
- sendDirectMessage<V extends OneBot.Version>(uin: string, version: V, args: [string, OneBot.MessageElement<V>[], string]): Promise<OneBot.MessageRet<V>>;
66
+ sendDirectMessage<V extends OneBot.Version>(uin: string, version: V, args: [string, OneBot.Segment<V>[], string]): Promise<OneBot.MessageRet<V>>;
66
67
  /** 获取消息 */
67
68
  getMessage<V extends OneBot.Version>(uin: string, version: V, args: [string]): Promise<OneBot.Message<V>>;
68
69
  deleteMessage<V extends OneBot.Version>(uin: string, version: V, args: [string]): Promise<boolean>;
package/lib/adapter.js CHANGED
@@ -24,11 +24,94 @@ class Adapter extends events_1.EventEmitter {
24
24
  this.oneBots = new Map();
25
25
  _Adapter_logger.set(this, void 0);
26
26
  }
27
+ materialize(content) {
28
+ return content
29
+ .replace(/&(?!(amp|#91|#93|#44);)/g, "&amp;")
30
+ .replace(/\[/g, "&#91;")
31
+ .replace(/]/g, "&#93;")
32
+ .replace(/,/g, "&#44;");
33
+ }
34
+ toCqcode(version, messageArr) {
35
+ return []
36
+ .concat(messageArr)
37
+ .map(item => {
38
+ if (typeof item === "string")
39
+ return item;
40
+ if (item.type === "text")
41
+ return item.data?.text || item.text;
42
+ let dataStr;
43
+ if (typeof item.data === "string") {
44
+ dataStr = [`data=${this.materialize(item.data)}`];
45
+ }
46
+ else {
47
+ dataStr = Object.entries(item.data || item).map(([key, value]) => {
48
+ // is Buffer
49
+ if (value instanceof Buffer)
50
+ return `${key}=${value.toString("base64")}`;
51
+ // is Object
52
+ if (value instanceof Object)
53
+ return `${key}=${JSON.stringify(value)}`;
54
+ // is Array
55
+ if (value instanceof Array)
56
+ return `${key}=${value.map(v => JSON.stringify(v)).join(",")}`;
57
+ // is String
58
+ return `${key}=${item.data?.[key] || item[key]}`;
59
+ });
60
+ }
61
+ return `[CQ:${item.type},${dataStr.join(",")}]`;
62
+ })
63
+ .join("");
64
+ }
65
+ fromCqcode(version, message) {
66
+ const regExpMatchArray = message.match(/\[CQ:([a-z]+),([^]]+)]/);
67
+ if (!regExpMatchArray)
68
+ return [
69
+ {
70
+ type: "text",
71
+ data: {
72
+ text: message,
73
+ },
74
+ },
75
+ ];
76
+ const result = [];
77
+ while (message.length) {
78
+ const [match] = message.match(/\[CQ:([a-z]+),([^]]+)]/) || [];
79
+ if (!match)
80
+ break;
81
+ const prevText = message.substring(0, match.length);
82
+ if (prevText) {
83
+ result.push({
84
+ type: "text",
85
+ data: {
86
+ text: prevText,
87
+ },
88
+ });
89
+ }
90
+ const [type, ...valueArr] = match.substring(1, match.length - 1).split(",");
91
+ result.push({
92
+ type: type,
93
+ data: Object.fromEntries(valueArr.map(item => {
94
+ const [key, value] = item.split("=");
95
+ return [key, type === "reply" && key === "id" ? +value : value];
96
+ })),
97
+ });
98
+ message = message.substring(match.length);
99
+ }
100
+ if (message.length) {
101
+ result.push({
102
+ type: "text",
103
+ data: {
104
+ text: message,
105
+ },
106
+ });
107
+ }
108
+ return result;
109
+ }
27
110
  transformMessage(uin, version, message) {
28
111
  const onebot = this.getOneBot(uin);
29
112
  const instance = onebot.instances.find(V => V.version === version);
30
113
  return instance.config.post_message_format === "string"
31
- ? this.toCqcode(version, message)
114
+ ? this.toCqcode(version, this.toSegment(version, message))
32
115
  : this.toSegment(version, message);
33
116
  }
34
117
  getOneBot(uin) {
@@ -2,7 +2,7 @@ import { Adapter } from "../../adapter";
2
2
  import { App } from "../../server/app";
3
3
  import { OneBot } from "../../onebot";
4
4
  import { Bot, Sendable } from "node-dd-bot";
5
- export default class DingtalkAdapter extends Adapter<"dingtalk"> {
5
+ export default class DingtalkAdapter extends Adapter<"dingtalk", Sendable> {
6
6
  #private;
7
7
  constructor(app: App, config: DingtalkAdapter.Config);
8
8
  startOneBot(oneBot: OneBot<Bot>): Promise<() => void>;
@@ -14,13 +14,12 @@ export default class DingtalkAdapter extends Adapter<"dingtalk"> {
14
14
  setOffline(uin: string): Promise<void>;
15
15
  createOneBot(uin: string, protocol: Bot.Options, versions: OneBot.Config[]): OneBot;
16
16
  call(uin: string, version: string, method: string, args?: any[]): Promise<any>;
17
- sendPrivateMessage<V extends OneBot.Version>(uin: string, version: V, args: [string, OneBot.MessageElement<V>[], string]): Promise<OneBot.MessageRet<V>>;
18
- sendGroupMessage<V extends OneBot.Version>(uin: string, version: V, args: [string, OneBot.MessageElement<V>[], string]): Promise<OneBot.MessageRet<V>>;
17
+ sendPrivateMessage<V extends OneBot.Version>(uin: string, version: V, args: [string, OneBot.Segment<V>[], string]): Promise<OneBot.MessageRet<V>>;
18
+ sendGroupMessage<V extends OneBot.Version>(uin: string, version: V, args: [string, OneBot.Segment<V>[], string]): Promise<OneBot.MessageRet<V>>;
19
19
  deleteMessage(uin: string, message_id: string): Promise<boolean>;
20
- fromSegment<V extends OneBot.Version>(version: V, segment: OneBot.Segment<V> | OneBot.Segment<V>[]): OneBot.MessageElement<V>[];
21
- toSegment<V extends OneBot.Version, M = Sendable>(version: V, message: M): OneBot.Segment<V>[];
22
- fromCqcode<V extends OneBot.Version>(version: V, message: string): OneBot.MessageElement<V>[];
23
- toCqcode<V extends OneBot.Version>(version: V, messageArr: OneBot.MessageElement<V>[]): string;
20
+ fromSegment<V extends OneBot.Version>(version: V, segment: OneBot.Segment<V> | OneBot.Segment<V>[]): Sendable;
21
+ toSegment<V extends OneBot.Version>(version: V, message: Sendable): OneBot.Segment<V>[];
22
+ fromCqcode<V extends OneBot.Version>(version: V, message: string): OneBot.Segment<V>[];
24
23
  formatEventPayload<V extends OneBot.Version>(uin: string, version: V, event: string, data: any): OneBot.Payload<V>;
25
24
  start(uin: string): Promise<void>;
26
25
  stop(uin?: string): Promise<void>;
@@ -154,31 +154,27 @@ class DingtalkAdapter extends adapter_1.Adapter {
154
154
  fromSegment(version, segment) {
155
155
  return [].concat(segment).map(item => {
156
156
  if (typeof item === "string")
157
- return {
158
- type: "text",
159
- data: {
160
- text: item,
161
- },
162
- };
163
- return item;
157
+ return item;
158
+ const { type, data } = item;
159
+ return {
160
+ type,
161
+ ...data,
162
+ };
164
163
  });
165
164
  }
166
165
  toSegment(version, message) {
167
166
  return [].concat(message).map(item => {
168
- if (!item || typeof item !== "object")
167
+ if (typeof item !== "object")
169
168
  return {
170
169
  type: "text",
171
170
  data: {
172
- text: item,
171
+ text: item + "",
173
172
  },
174
173
  };
175
- const { type, data, ...other } = item;
174
+ const { type, ...data } = item;
176
175
  return {
177
176
  type,
178
- data: {
179
- ...data,
180
- ...other,
181
- },
177
+ data,
182
178
  };
183
179
  });
184
180
  }
@@ -206,31 +202,6 @@ class DingtalkAdapter extends adapter_1.Adapter {
206
202
  }
207
203
  return result;
208
204
  }
209
- toCqcode(version, messageArr) {
210
- return []
211
- .concat(messageArr)
212
- .map(item => {
213
- if (typeof item === "string")
214
- return item;
215
- if (item.type === "text")
216
- return item.data?.text || item.text;
217
- const dataStr = Object.entries(item.data).map(([key, value]) => {
218
- // is Buffer
219
- if (value instanceof Buffer)
220
- return `${key}=${value.toString("base64")}`;
221
- // is Object
222
- if (value instanceof Object)
223
- return `${key}=${JSON.stringify(value)}`;
224
- // is Array
225
- if (value instanceof Array)
226
- return `${key}=${value.map(v => JSON.stringify(v)).join(",")}`;
227
- // is String
228
- return `${key}=${item[key]}`;
229
- });
230
- return `[CQ:${item.type},${dataStr.join(",")}]`;
231
- })
232
- .join("");
233
- }
234
205
  formatEventPayload(uin, version, event, data) {
235
206
  const oneBot = this.getOneBot(uin);
236
207
  const result = {
@@ -249,7 +220,6 @@ class DingtalkAdapter extends adapter_1.Adapter {
249
220
  };
250
221
  delete result.bot;
251
222
  if (event === "message") {
252
- result.message = this.transformMessage(uin, version, result.message);
253
223
  result.alt_message = result.raw_message || "";
254
224
  }
255
225
  if (version === "V11") {
@@ -1,8 +1,8 @@
1
1
  import { Adapter } from "../../adapter";
2
2
  import { App } from "../../server/app";
3
- import { Config as IcqqConfig } from "@icqqjs/icqq";
3
+ import { Config as IcqqConfig, Sendable } from "@icqqjs/icqq";
4
4
  import { OneBot } from "../../onebot";
5
- export default class IcqqAdapter extends Adapter<"icqq"> {
5
+ export default class IcqqAdapter extends Adapter<"icqq", Sendable> {
6
6
  #private;
7
7
  constructor(app: App, config: IcqqAdapter.Config);
8
8
  setOnline(uin: string): Promise<void>;
@@ -10,17 +10,14 @@ export default class IcqqAdapter extends Adapter<"icqq"> {
10
10
  callApi<V extends OneBot.Version>(uin: string, version: V, [name, args]: [string, any[]]): any;
11
11
  createOneBot(uin: string, protocol: IcqqConfig, versions: OneBot.Config[]): OneBot;
12
12
  formatEventPayload<V extends OneBot.Version>(uin: string, version: V, event: string, data: any): OneBot.Payload<V>;
13
- sendPrivateMessage<V extends OneBot.Version>(uin: string, version: V, args: [string, OneBot.MessageElement<V>[], string?]): Promise<OneBot.MessageRet<V>>;
13
+ sendPrivateMessage<V extends OneBot.Version>(uin: string, version: V, args: [string, OneBot.Segment<V>[], string?]): Promise<OneBot.MessageRet<V>>;
14
14
  deleteMessage<V extends OneBot.Version>(uin: string, version: V, args: [string]): Promise<boolean>;
15
- sendGroupMessage<V extends OneBot.Version>(uin: string, version: V, args: [string, OneBot.MessageElement<V>[], string?]): Promise<OneBot.MessageRet<V>>;
16
- sendGuildMessage<V extends OneBot.Version>(uin: string, version: V, args: [string, OneBot.MessageElement<V>[], string?]): Promise<OneBot.MessageRet<V>>;
15
+ sendGroupMessage<V extends OneBot.Version>(uin: string, version: V, args: [string, OneBot.Segment<V>[], string?]): Promise<OneBot.MessageRet<V>>;
16
+ sendGuildMessage<V extends OneBot.Version>(uin: string, version: V, args: [string, OneBot.Segment<V>[], string?]): Promise<OneBot.MessageRet<V>>;
17
17
  getMessage<V extends OneBot.Version>(uin: string, version: V, [message_id]: [string]): Promise<OneBot.Message<V>>;
18
18
  call<V extends OneBot.Version>(uin: string, version: V, method: string, args?: any[]): Promise<any>;
19
- fromSegment<V extends OneBot.Version>(version: V, segment: OneBot.Segment<V> | OneBot.Segment<V>[]): OneBot.MessageElement<V>[];
20
- toSegment<V extends OneBot.Version, M = any>(version: V, message: M): OneBot.Segment<V>[];
21
- fromCqcode<V extends OneBot.Version>(version: V, message: string): OneBot.MessageElement<V>[];
22
- materialize(content: string): string;
23
- toCqcode<V extends OneBot.Version>(version: V, messageArr: OneBot.MessageElement<V>[]): string;
19
+ fromSegment<V extends OneBot.Version>(version: V, segment: OneBot.Segment<V> | OneBot.Segment<V>[]): Sendable;
20
+ toSegment<V extends OneBot.Version>(version: V, message: Sendable): OneBot.Segment<V>[];
24
21
  getSelfInfo<V extends OneBot.Version>(uin: string, version: V): OneBot.SelfInfo<V>;
25
22
  startOneBot(oneBot: OneBot): Promise<Function>;
26
23
  start(uin?: string): Promise<void>;
@@ -56,6 +56,7 @@ async function processMessages(uin, target_id, target_type, list) {
56
56
  result.push({
57
57
  type,
58
58
  ...data,
59
+ user_id: data.user_id,
59
60
  message: await processMessages.call(this, uin, data.user_id, "private", data.content || []),
60
61
  });
61
62
  break;
@@ -83,7 +84,7 @@ async function processMessages(uin, target_id, target_type, list) {
83
84
  if (item["file"]?.startsWith("base64://"))
84
85
  item["file"] = Buffer.from(item["file"].slice(9), "base64");
85
86
  result.push({
86
- type,
87
+ type: type,
87
88
  ...data,
88
89
  ...other,
89
90
  });
@@ -101,7 +102,7 @@ async function processMessages(uin, target_id, target_type, list) {
101
102
  }
102
103
  default: {
103
104
  result.push({
104
- type,
105
+ type: type,
105
106
  ...data,
106
107
  ...other,
107
108
  });
@@ -185,7 +186,6 @@ class IcqqAdapter extends adapter_1.Adapter {
185
186
  }
186
187
  }
187
188
  if (event === "message") {
188
- result.message = this.transformMessage(uin, version, result.message);
189
189
  result.alt_message = result.raw_message || "";
190
190
  }
191
191
  if (version === "V11" && result.message_id) {
@@ -235,11 +235,16 @@ class IcqqAdapter extends adapter_1.Adapter {
235
235
  : message_id,
236
236
  };
237
237
  }
238
- getMessage(uin, version, [message_id]) {
238
+ async getMessage(uin, version, [message_id]) {
239
239
  const oneBot = this.getOneBot(uin);
240
240
  if (!oneBot)
241
241
  throw new Error("No one");
242
- return oneBot.internal.getMsg(message_id);
242
+ let { message, ...result } = await oneBot.internal.getMsg(message_id);
243
+ const segments = this.toSegment(version, message);
244
+ return {
245
+ ...result,
246
+ message: segments,
247
+ };
243
248
  }
244
249
  call(uin, version, method, args = []) {
245
250
  try {
@@ -254,13 +259,9 @@ class IcqqAdapter extends adapter_1.Adapter {
254
259
  fromSegment(version, segment) {
255
260
  return [].concat(segment).map(item => {
256
261
  if (typeof item === "string")
257
- return {
258
- type: "text",
259
- data: {
260
- text: item,
261
- },
262
- };
263
- return item;
262
+ return item;
263
+ const { type, data } = item;
264
+ return { type, ...data };
264
265
  });
265
266
  }
266
267
  toSegment(version, message) {
@@ -279,89 +280,6 @@ class IcqqAdapter extends adapter_1.Adapter {
279
280
  };
280
281
  });
281
282
  }
282
- fromCqcode(version, message) {
283
- const regExpMatchArray = message.match(/\[CQ:([a-z]+),([^]]+)]/);
284
- if (!regExpMatchArray)
285
- return [
286
- {
287
- type: "text",
288
- data: {
289
- text: message,
290
- },
291
- },
292
- ];
293
- const result = [];
294
- while (message.length) {
295
- const [match] = message.match(/\[CQ:([a-z]+),([^]]+)]/) || [];
296
- if (!match)
297
- break;
298
- const prevText = message.substring(0, match.length);
299
- if (prevText) {
300
- result.push({
301
- type: "text",
302
- data: {
303
- text: prevText,
304
- },
305
- });
306
- }
307
- const [type, ...valueArr] = match.substring(1, match.length - 1).split(",");
308
- result.push({
309
- type: type,
310
- data: Object.fromEntries(valueArr.map(item => {
311
- const [key, value] = item.split("=");
312
- return [key, type === "reply" && key === "id" ? +value : value];
313
- })),
314
- });
315
- message = message.substring(match.length);
316
- }
317
- if (message.length) {
318
- result.push({
319
- type: "text",
320
- data: {
321
- text: message,
322
- },
323
- });
324
- }
325
- return result;
326
- }
327
- materialize(content) {
328
- return content
329
- .replace(/&(?!(amp|#91|#93|#44);)/g, "&amp;")
330
- .replace(/\[/g, "&#91;")
331
- .replace(/]/g, "&#93;")
332
- .replace(/,/g, "&#44;");
333
- }
334
- toCqcode(version, messageArr) {
335
- return []
336
- .concat(messageArr)
337
- .map(item => {
338
- if (typeof item === "string")
339
- return item;
340
- if (item.type === "text")
341
- return item.data?.text || item.text;
342
- let dataStr;
343
- if (typeof item.data === "string") {
344
- dataStr = [`data=${this.materialize(item.data)}`];
345
- }
346
- else {
347
- dataStr = Object.entries(item.data || item).map(([key, value]) => {
348
- // is Buffer
349
- if (value instanceof Buffer)
350
- return `${key}=${value.toString("base64")}`;
351
- // is Object
352
- if (value instanceof Object)
353
- return `${key}=${JSON.stringify(value)}`;
354
- // is Array
355
- if (value instanceof Array)
356
- return `${key}=${value.map(v => JSON.stringify(v)).join(",")}`;
357
- // is String
358
- return `${key}=${item.data?.[key] || item[key]}`;
359
- });
360
- }
361
- return `[CQ:${item.type},${dataStr.join(",")}]`;
362
- })
363
- .join("");
364
- }
365
283
  getSelfInfo(uin, version) {
366
284
  const client = this.oneBots.get(uin).internal;
367
285
  return {
@@ -2,23 +2,21 @@ import { Adapter } from "../../adapter";
2
2
  import { App } from "../../server/app";
3
3
  import { OneBot } from "../../onebot";
4
4
  import { Bot, Sendable } from "qq-group-bot";
5
- export default class QQAdapter extends Adapter<"qq"> {
5
+ export default class QQAdapter extends Adapter<"qq", Sendable> {
6
6
  #private;
7
7
  constructor(app: App, config: QQAdapter.Config);
8
8
  startOneBot(oneBot: OneBot<Bot>): Promise<() => void>;
9
9
  setOnline(uin: string): Promise<void>;
10
10
  setOffline(uin: string): Promise<void>;
11
11
  createOneBot(uin: string, protocol: Bot.Config, versions: OneBot.Config[]): OneBot;
12
- sendGroupMessage<V extends OneBot.Version>(uin: string, version: V, args: [string, OneBot.MessageElement<V>[], string]): Promise<OneBot.MessageRet<V>>;
13
- sendPrivateMessage<V extends OneBot.Version>(uin: string, version: V, args: [string, OneBot.MessageElement<V>[], string]): Promise<OneBot.MessageRet<V>>;
14
- sendGuildMessage<V extends OneBot.Version>(uin: string, version: V, args: [string, OneBot.MessageElement<V>[], string]): Promise<OneBot.MessageRet<V>>;
15
- sendDirectMessage<V extends OneBot.Version>(uin: string, version: V, args: [string, OneBot.MessageElement<V>[], string]): Promise<OneBot.MessageRet<V>>;
12
+ sendGroupMessage<V extends OneBot.Version>(uin: string, version: V, args: [string, OneBot.Segment<V>[], string]): Promise<OneBot.MessageRet<V>>;
13
+ sendPrivateMessage<V extends OneBot.Version>(uin: string, version: V, args: [string, OneBot.Segment<V>[], string]): Promise<OneBot.MessageRet<V>>;
14
+ sendGuildMessage<V extends OneBot.Version>(uin: string, version: V, args: [string, OneBot.Segment<V>[], string]): Promise<OneBot.MessageRet<V>>;
15
+ sendDirectMessage<V extends OneBot.Version>(uin: string, version: V, args: [string, OneBot.Segment<V>[], string]): Promise<OneBot.MessageRet<V>>;
16
16
  deleteMessage(uin: string, message_id: string): Promise<boolean>;
17
17
  call(uin: string, version: string, method: string, args?: any[]): Promise<any>;
18
- fromSegment<V extends OneBot.Version>(version: V, segment: OneBot.Segment<V> | OneBot.Segment<V>[]): OneBot.MessageElement<V>[];
18
+ fromSegment<V extends OneBot.Version>(version: V, segment: OneBot.Segment<V> | OneBot.Segment<V>[]): Sendable;
19
19
  toSegment<V extends OneBot.Version, M = Sendable>(version: V, message: M): OneBot.Segment<V>[];
20
- fromCqcode<V extends OneBot.Version>(version: V, message: string): OneBot.MessageElement<V>[];
21
- toCqcode<V extends OneBot.Version>(version: V, messageArr: OneBot.MessageElement<V>[]): string;
22
20
  formatEventPayload<V extends OneBot.Version>(uin: string, version: V, event: string, data: any): OneBot.Payload<V>;
23
21
  start(uin: string): Promise<void>;
24
22
  stop(uin?: string): Promise<void>;
@@ -178,13 +178,12 @@ class QQAdapter extends adapter_1.Adapter {
178
178
  fromSegment(version, segment) {
179
179
  return [].concat(segment).map(item => {
180
180
  if (typeof item === "string")
181
- return {
182
- type: "text",
183
- data: {
184
- text: item,
185
- },
186
- };
187
- return item;
181
+ return item;
182
+ const { type, data } = item;
183
+ return {
184
+ type,
185
+ ...data,
186
+ };
188
187
  });
189
188
  }
190
189
  toSegment(version, message) {
@@ -196,65 +195,13 @@ class QQAdapter extends adapter_1.Adapter {
196
195
  text: item,
197
196
  },
198
197
  };
199
- const { type, data, ...other } = item;
198
+ const { type, ...data } = item;
200
199
  return {
201
200
  type,
202
- data: {
203
- ...data,
204
- ...other,
205
- },
201
+ data,
206
202
  };
207
203
  });
208
204
  }
209
- fromCqcode(version, message) {
210
- const regExpMatchArray = message.match(/\[CQ:([a-z]+),(!])+]/g);
211
- if (!regExpMatchArray)
212
- return [
213
- {
214
- type: "text",
215
- data: {
216
- text: message,
217
- },
218
- },
219
- ];
220
- const result = [];
221
- for (const match of regExpMatchArray) {
222
- const [type, ...valueArr] = match.substring(1, match.length - 1).split(",");
223
- result.push({
224
- type: type,
225
- data: Object.fromEntries(valueArr.map(item => {
226
- const [key, value] = item.split("=");
227
- return [key, value];
228
- })),
229
- });
230
- }
231
- return result;
232
- }
233
- toCqcode(version, messageArr) {
234
- return []
235
- .concat(messageArr)
236
- .map(item => {
237
- if (typeof item === "string")
238
- return item;
239
- if (item.type === "text")
240
- return item.data?.text || item.text;
241
- const dataStr = Object.entries(item.data).map(([key, value]) => {
242
- // is Buffer
243
- if (value instanceof Buffer)
244
- return `${key}=${value.toString("base64")}`;
245
- // is Object
246
- if (value instanceof Object)
247
- return `${key}=${JSON.stringify(value)}`;
248
- // is Array
249
- if (value instanceof Array)
250
- return `${key}=${value.map(v => JSON.stringify(v)).join(",")}`;
251
- // is String
252
- return `${key}=${value}`;
253
- });
254
- return `[CQ:${item.type},${dataStr.join(",")}]`;
255
- })
256
- .join("");
257
- }
258
205
  formatEventPayload(uin, version, event, data) {
259
206
  const result = {
260
207
  id: data.id || Math.random().toString(36).slice(2),
@@ -275,7 +222,6 @@ class QQAdapter extends adapter_1.Adapter {
275
222
  delete result.bot;
276
223
  const oneBot = this.getOneBot(uin);
277
224
  if (event === "message") {
278
- result.message = this.transformMessage(uin, version, result.message);
279
225
  result.alt_message = result.raw_message || "";
280
226
  }
281
227
  switch (version) {
@@ -3,7 +3,7 @@ import { App } from "../../server/app";
3
3
  import { OneBot } from "../../onebot";
4
4
  import { Client, Sendable, BaseClient } from "lib-wechat";
5
5
  type WechatConfig = BaseClient.Config;
6
- export default class WechatAdapter extends Adapter<"wechat"> {
6
+ export default class WechatAdapter extends Adapter<"wechat", Sendable> {
7
7
  #private;
8
8
  constructor(app: App, config: WechatAdapter.Config);
9
9
  startOneBot(oneBot: OneBot<Client>): Promise<() => void>;
@@ -11,13 +11,11 @@ export default class WechatAdapter extends Adapter<"wechat"> {
11
11
  setOffline(uin: string): Promise<void>;
12
12
  createOneBot(uin: string, protocol: WechatConfig, versions: OneBot.Config[]): OneBot;
13
13
  call(uin: string, version: string, method: string, args?: any[]): Promise<any>;
14
- sendPrivateMessage<V extends OneBot.Version>(uin: string, version: V, args: [string, OneBot.MessageElement<V>[], string]): Promise<OneBot.MessageRet<V>>;
15
- sendGroupMessage<V extends OneBot.Version>(uin: string, version: V, args: [string, OneBot.MessageElement<V>[], string]): Promise<OneBot.MessageRet<V>>;
14
+ sendPrivateMessage<V extends OneBot.Version>(uin: string, version: V, args: [string, OneBot.Segment<V>[], string]): Promise<OneBot.MessageRet<V>>;
15
+ sendGroupMessage<V extends OneBot.Version>(uin: string, version: V, args: [string, OneBot.Segment<V>[], string]): Promise<OneBot.MessageRet<V>>;
16
16
  deleteMessage<V extends OneBot.Version>(uin: string, version: V, [str]: [string]): Promise<boolean>;
17
- fromSegment<V extends OneBot.Version>(version: V, segment: OneBot.Segment<V> | OneBot.Segment<V>[]): OneBot.MessageElement<V>[];
18
- toSegment<V extends OneBot.Version, M = Sendable>(version: V, message: M): OneBot.Segment<V>[];
19
- fromCqcode<V extends OneBot.Version>(version: V, message: string): OneBot.MessageElement<V>[];
20
- toCqcode<V extends OneBot.Version>(version: V, messageArr: OneBot.MessageElement<V>[]): string;
17
+ fromSegment<V extends OneBot.Version>(version: V, segment: OneBot.Segment<V> | OneBot.Segment<V>[]): Sendable;
18
+ toSegment<V extends OneBot.Version>(version: V, message: Sendable): OneBot.Segment<V>[];
21
19
  getFriendList<V extends OneBot.Version>(uin: string, version: V): Promise<OneBot.UserInfo<V>[]>;
22
20
  getGroupList<V extends OneBot.Version>(uin: string, version: V): Promise<OneBot.GroupInfo<V>[]>;
23
21
  formatEventPayload<V extends OneBot.Version>(uin: string, version: V, event: string, data: any): OneBot.Payload<V>;
@@ -158,13 +158,12 @@ class WechatAdapter extends adapter_1.Adapter {
158
158
  fromSegment(version, segment) {
159
159
  return [].concat(segment).map(item => {
160
160
  if (typeof item === "string")
161
- return {
162
- type: "text",
163
- data: {
164
- text: item,
165
- },
166
- };
167
- return item;
161
+ return item;
162
+ const { type, data } = item;
163
+ return {
164
+ type,
165
+ ...data,
166
+ };
168
167
  });
169
168
  }
170
169
  toSegment(version, message) {
@@ -176,65 +175,13 @@ class WechatAdapter extends adapter_1.Adapter {
176
175
  text: item,
177
176
  },
178
177
  };
179
- const { type, data, ...other } = item;
178
+ const { type, ...data } = item;
180
179
  return {
181
180
  type,
182
- data: {
183
- ...data,
184
- ...other,
185
- },
181
+ data,
186
182
  };
187
183
  });
188
184
  }
189
- fromCqcode(version, message) {
190
- const regExpMatchArray = message.match(/\[CQ:([a-z]+),(!])+]/g);
191
- if (!regExpMatchArray)
192
- return [
193
- {
194
- type: "text",
195
- data: {
196
- text: message,
197
- },
198
- },
199
- ];
200
- const result = [];
201
- for (const match of regExpMatchArray) {
202
- const [type, ...valueArr] = match.substring(1, match.length - 1).split(",");
203
- result.push({
204
- type: type,
205
- data: Object.fromEntries(valueArr.map(item => {
206
- const [key, value] = item.split("=");
207
- return [key, value];
208
- })),
209
- });
210
- }
211
- return result;
212
- }
213
- toCqcode(version, messageArr) {
214
- return []
215
- .concat(messageArr)
216
- .map(item => {
217
- if (typeof item === "string")
218
- return item;
219
- if (item.type === "text")
220
- return item.data?.text || item.text;
221
- const dataStr = Object.entries(item.data).map(([key, value]) => {
222
- // is Buffer
223
- if (value instanceof Buffer)
224
- return `${key}=${value.toString("base64")}`;
225
- // is Object
226
- if (value instanceof Object)
227
- return `${key}=${JSON.stringify(value)}`;
228
- // is Array
229
- if (value instanceof Array)
230
- return `${key}=${value.map(v => JSON.stringify(v)).join(",")}`;
231
- // is String
232
- return `${key}=${item[key]}`;
233
- });
234
- return `[CQ:${item.type},${dataStr.join(",")}]`;
235
- })
236
- .join("");
237
- }
238
185
  async getFriendList(uin, version) {
239
186
  const bot = this.getOneBot(uin);
240
187
  const result = bot.internal.getFriendList();
@@ -282,7 +229,6 @@ class WechatAdapter extends adapter_1.Adapter {
282
229
  delete result.c;
283
230
  delete result.parser;
284
231
  if (event === "message") {
285
- result.message = this.transformMessage(uin, version, result.message);
286
232
  result.alt_message = result.raw_message || "";
287
233
  }
288
234
  if (version === "V11") {
package/lib/onebot.d.ts CHANGED
@@ -61,7 +61,6 @@ export declare namespace OneBot {
61
61
  type GroupInfo<V extends Version> = V extends "V11" ? V11.GroupInfo : V12.GroupInfo;
62
62
  type UserInfo<V extends Version> = V extends "V11" ? V11.UserInfo : V12.UserInfo;
63
63
  type Message<V extends Version> = V extends "V11" ? V11.Message : V12.Message;
64
- type MessageElement<V extends Version> = V extends "V11" ? V11.MessageElement : V12.MessageElement;
65
64
  type GroupMemberInfo<V extends Version> = V extends "V11" ? V11.GroupMemberInfo : V12.GroupMemberInfo;
66
65
  type MessageRet<V extends Version> = V extends "V11" ? V11.MessageRet : V12.MessageRet;
67
66
  interface Base {
@@ -62,7 +62,7 @@ export declare class App extends Koa {
62
62
  removeAccount(platform: string, uin: string, force?: boolean): void;
63
63
  createOneBot<P extends string>(platform: P, uin: string, config: Adapter.Config): import("..").OneBot<any>;
64
64
  get oneBots(): import("..").OneBot<any>[];
65
- findOrCreateAdapter<P extends string>(platform: P, config?: Adapter.Config): void | Adapter<string>;
65
+ findOrCreateAdapter<P extends string>(platform: P, config?: Adapter.Config): void | Adapter<string, any>;
66
66
  start(): Promise<void>;
67
67
  reload(config: App.Config): Promise<void>;
68
68
  stop(): Promise<void>;
@@ -88,6 +88,6 @@ export declare namespace App {
88
88
  const defaultConfig: Config;
89
89
  function registerAdapter(name: string): void;
90
90
  function registerAdapter<T extends string>(platform: T, adapter: AdapterClass): void;
91
- function loadAdapter<T extends string>(platform: string): Class<Adapter<T>>;
91
+ function loadAdapter<T extends string>(platform: string): Class<Adapter<T, any>>;
92
92
  }
93
93
  export {};
package/lib/server/app.js CHANGED
@@ -379,16 +379,13 @@ exports.defineConfig = defineConfig;
379
379
  `onebots-adapter-${platform}`, // 别人按照规范写的
380
380
  platform, // 别人写的
381
381
  ];
382
- let adapter = null;
383
- for (const adapterName of maybeNames) {
384
- try {
385
- adapter = require(adapterName)?.default;
386
- break;
387
- }
388
- catch { }
382
+ let adapter;
383
+ try {
384
+ adapter = require(path.join(__dirname, "../adapters", platform))?.default;
385
+ }
386
+ catch (e) {
387
+ console.error(`loadAdapter(${platform}) failed:${e.message}`);
389
388
  }
390
- if (!adapter)
391
- throw new Error(`找不到对应的适配器:${platform}`);
392
389
  return adapter;
393
390
  }
394
391
  App.loadAdapter = loadAdapter;
@@ -119,17 +119,14 @@ export declare namespace V11 {
119
119
  user_id: number;
120
120
  user_name: string;
121
121
  }
122
- interface Message {
123
- }
124
122
  interface Segment {
125
123
  type: string;
126
124
  data: Dict;
127
125
  }
128
- interface MessageElement {
129
- type: string;
130
- data: Dict;
126
+ type Sendable = string | Segment | (string | Segment)[];
127
+ interface Message {
128
+ message: Sendable;
131
129
  }
132
- type Sendable = string | MessageElement | (string | MessageElement)[];
133
130
  interface MessageRet {
134
131
  message_id: number;
135
132
  }
@@ -258,12 +258,7 @@ class V11 extends service_1.Service {
258
258
  return;
259
259
  data.post_type = data.post_type || "system";
260
260
  if (data.message && data.post_type === "message") {
261
- if (this.config.post_message_format === "array") {
262
- data.message = this.adapter.toSegment("V11", data.message);
263
- }
264
- else {
265
- data.message = this.adapter.toCqcode("V11", (data.message = this.adapter.toSegment("V11", data.message)));
266
- }
261
+ data.message = this.adapter.transformMessage(this.oneBot.uin, "V11", data.message);
267
262
  }
268
263
  data.time = Math.floor(Date.now() / 1000);
269
264
  // data = transformObj(data, (key, value) => {
@@ -276,7 +271,6 @@ class V11 extends service_1.Service {
276
271
  }
277
272
  _formatEvent(data) {
278
273
  if (data.post_type === "notice") {
279
- // console.log(JSON.stringify(data))
280
274
  const data1 = { ...data };
281
275
  if (data.notice_type === "group") {
282
276
  delete data1.group;
@@ -583,13 +577,7 @@ class V11 extends service_1.Service {
583
577
  }
584
578
  params[k] = this.adapter.fromCqcode("V11", params[k]);
585
579
  }
586
- else {
587
- if (params[k][0].type == "music" && params[k][0]?.data?.type) {
588
- params[k][0].data.platform = params[k][0].data.type;
589
- delete params[k][0].data.type;
590
- }
591
- params[k] = this.adapter.fromSegment("V11", params[k]);
592
- }
580
+ params[k] = this.adapter.fromSegment("V11", params[k]);
593
581
  params["message_id"] =
594
582
  params[k].find(e => e.type === "reply")?.id || params["message_id"];
595
583
  }
@@ -67,76 +67,6 @@ export declare class V12 extends Service<"V12"> implements OneBot.Base {
67
67
  protected _webSocketHandler(ws: WebSocket): void;
68
68
  }
69
69
  export declare namespace V12 {
70
- type Sendable = string | SegmentElem | (string | SegmentElem)[];
71
- interface SegmentMap {
72
- face: {
73
- id: number;
74
- text?: string;
75
- };
76
- text: {
77
- text: string;
78
- };
79
- mention: {
80
- user_id: string;
81
- };
82
- rps: {
83
- id?: string;
84
- };
85
- dice: {
86
- id?: string;
87
- };
88
- poke: {
89
- user_id: string;
90
- };
91
- mention_all: null;
92
- image: {
93
- file_id: string;
94
- };
95
- voice: {
96
- file_id: string;
97
- };
98
- audio: {
99
- file_id: string;
100
- };
101
- file: {
102
- file_id: string;
103
- };
104
- music: {
105
- type: "163" | "qq" | "xm" | "custom";
106
- id?: string;
107
- url?: string;
108
- audio?: string;
109
- title?: string;
110
- };
111
- location: {
112
- latitude: number;
113
- longitude: number;
114
- title?: string;
115
- content?: string;
116
- };
117
- share: {
118
- url: string;
119
- title: string;
120
- content?: string;
121
- image?: string;
122
- };
123
- reply: {
124
- message_id: string;
125
- };
126
- node: {
127
- user_id: string;
128
- time?: number;
129
- user_name?: string;
130
- message: SegmentElem[];
131
- };
132
- forward: {
133
- nodes: SegmentElem<"node">[];
134
- };
135
- }
136
- type SegmentElem<K extends keyof SegmentMap = keyof SegmentMap> = {
137
- type: K;
138
- data: SegmentMap[K];
139
- };
140
70
  interface Config {
141
71
  heartbeat?: number;
142
72
  access_token?: string;
@@ -263,11 +193,14 @@ export declare namespace V12 {
263
193
  user_name: string;
264
194
  }
265
195
  interface Segment {
196
+ type: string;
197
+ data: Dict;
266
198
  }
199
+ type Sendable = string | Segment | (string | Segment)[];
267
200
  interface Message {
201
+ message: Sendable;
268
202
  }
269
- interface MessageElement extends Dict {
270
- type: string;
203
+ interface Message {
271
204
  }
272
205
  interface MessageRet {
273
206
  message_id: string;
@@ -394,6 +394,9 @@ class V12 extends service_1.Service {
394
394
  user_id: `${this.oneBot.uin}`,
395
395
  },
396
396
  });
397
+ if (payload.message && payload.type === "message") {
398
+ payload.message = this.adapter.transformMessage(this.oneBot.uin, "V12", payload.message);
399
+ }
397
400
  if (!this.filterFn(payload))
398
401
  return;
399
402
  this.emit("dispatch", payload);
@@ -442,13 +445,7 @@ class V12 extends service_1.Service {
442
445
  }
443
446
  params[k] = this.adapter.fromCqcode("V12", params[k]);
444
447
  }
445
- else {
446
- if (params[k][0].type == "music" && params[k][0]?.data?.type) {
447
- params[k][0].data.platform = params[k][0].data.type;
448
- delete params[k][0].data.type;
449
- }
450
- params[k] = this.adapter.fromSegment("V12", params[k]);
451
- }
448
+ params[k] = this.adapter.fromSegment("V12", params[k]);
452
449
  }
453
450
  args.push(params[k]);
454
451
  }
@@ -707,7 +704,6 @@ class V12 extends service_1.Service {
707
704
  }
708
705
  exports.V12 = V12;
709
706
  (function (V12) {
710
- const fileTypes = ["image", "file", "record", "video", "flash"];
711
707
  V12.defaultConfig = {
712
708
  heartbeat: 3,
713
709
  access_token: "",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "onebots",
3
- "version": "0.4.59",
3
+ "version": "0.4.60",
4
4
  "description": "基于icqq的多例oneBot实现",
5
5
  "engines": {
6
6
  "node": ">=16"