onebots 0.4.22 → 0.4.25

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.
Files changed (84) hide show
  1. package/README.md +129 -72
  2. package/lib/adapter.d.ts +55 -0
  3. package/lib/adapter.js +60 -0
  4. package/lib/adapters/icqq/index.d.ts +36 -0
  5. package/lib/adapters/icqq/index.js +300 -0
  6. package/lib/adapters/icqq/utils.d.ts +6 -0
  7. package/lib/adapters/icqq/utils.js +26 -0
  8. package/lib/adapters/qq/bot.d.ts +24 -0
  9. package/lib/adapters/qq/bot.js +20 -0
  10. package/lib/adapters/qq/constans.d.ts +69 -0
  11. package/lib/adapters/qq/constans.js +128 -0
  12. package/lib/adapters/qq/elements.d.ts +125 -0
  13. package/lib/adapters/qq/elements.js +8 -0
  14. package/lib/adapters/qq/entries/channel.d.ts +17 -0
  15. package/lib/adapters/qq/entries/channel.js +2 -0
  16. package/lib/adapters/qq/entries/friend.d.ts +8 -0
  17. package/lib/adapters/qq/entries/friend.js +2 -0
  18. package/lib/adapters/qq/entries/group.d.ts +6 -0
  19. package/lib/adapters/qq/entries/group.js +2 -0
  20. package/lib/adapters/qq/entries/groupMember.d.ts +7 -0
  21. package/lib/adapters/qq/entries/groupMember.js +2 -0
  22. package/lib/adapters/qq/entries/guild.d.ts +21 -0
  23. package/lib/adapters/qq/entries/guild.js +2 -0
  24. package/lib/adapters/qq/entries/guildMember.d.ts +9 -0
  25. package/lib/adapters/qq/entries/guildMember.js +2 -0
  26. package/lib/adapters/qq/entries/user.d.ts +9 -0
  27. package/lib/adapters/qq/entries/user.js +2 -0
  28. package/lib/adapters/qq/event.d.ts +28 -0
  29. package/lib/adapters/qq/event.js +24 -0
  30. package/lib/adapters/qq/index.d.ts +35 -0
  31. package/lib/adapters/qq/index.js +170 -0
  32. package/lib/adapters/qq/message.d.ts +83 -0
  33. package/lib/adapters/qq/message.js +272 -0
  34. package/lib/adapters/qq/qqBot.d.ts +122 -0
  35. package/lib/adapters/qq/qqBot.js +438 -0
  36. package/lib/adapters/qq/sessionManager.d.ts +33 -0
  37. package/lib/adapters/qq/sessionManager.js +257 -0
  38. package/lib/adapters/qq/types.d.ts +45 -0
  39. package/lib/adapters/qq/types.js +2 -0
  40. package/lib/adapters/qq/utils.d.ts +12 -0
  41. package/lib/adapters/qq/utils.js +86 -0
  42. package/lib/bin.js +17 -2
  43. package/lib/config.sample.yaml +11 -5
  44. package/lib/db.js +6 -6
  45. package/lib/index.d.ts +5 -5
  46. package/lib/onebot.d.ts +30 -22
  47. package/lib/onebot.js +42 -165
  48. package/lib/server/app.d.ts +22 -12
  49. package/lib/server/app.js +97 -71
  50. package/lib/service/V11/action/common.d.ts +11 -12
  51. package/lib/service/V11/action/common.js +19 -65
  52. package/lib/service/V11/action/friend.d.ts +6 -14
  53. package/lib/service/V11/action/friend.js +6 -16
  54. package/lib/service/V11/action/group.d.ts +20 -23
  55. package/lib/service/V11/action/group.js +20 -30
  56. package/lib/service/V11/action/index.d.ts +1 -1
  57. package/lib/service/V11/db_entities.d.ts +1 -1
  58. package/lib/service/V11/db_entities.js +1 -1
  59. package/lib/service/V11/db_sqlite.d.ts +1 -1
  60. package/lib/service/V11/db_sqlite.js +5 -7
  61. package/lib/service/V11/index.d.ts +46 -14
  62. package/lib/service/V11/index.js +163 -134
  63. package/lib/service/V12/action/common.d.ts +3 -67
  64. package/lib/service/V12/action/common.js +9 -63
  65. package/lib/service/V12/action/friend.d.ts +4 -10
  66. package/lib/service/V12/action/friend.js +4 -12
  67. package/lib/service/V12/action/group.d.ts +19 -21
  68. package/lib/service/V12/action/group.js +19 -27
  69. package/lib/service/V12/action/guild.d.ts +40 -13
  70. package/lib/service/V12/action/guild.js +99 -11
  71. package/lib/service/V12/action/index.d.ts +1 -1
  72. package/lib/service/V12/index.d.ts +37 -13
  73. package/lib/service/V12/index.js +63 -94
  74. package/lib/service.d.ts +7 -2
  75. package/lib/service.js +5 -1
  76. package/lib/types.d.ts +2 -2
  77. package/lib/types.js +3 -3
  78. package/lib/utils.d.ts +7 -4
  79. package/lib/utils.js +18 -1
  80. package/package.json +74 -70
  81. package/lib/service/V11/utils.d.ts +0 -9
  82. package/lib/service/V11/utils.js +0 -31
  83. package/lib/service/V12/utils.d.ts +0 -8
  84. package/lib/service/V12/utils.js +0 -47
@@ -1,28 +1,116 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.GuildAction = void 0;
4
- const utils_1 = require("../../../service/V12/utils");
5
4
  class GuildAction {
5
+ async getGuildSelfInfo() {
6
+ return this.adapter.call(this.oneBot.uin, 'V12', 'getSelfInfo');
7
+ }
8
+ async getChannelPermissionOfRole(channel_id, role_id) {
9
+ return this.adapter.call(this.oneBot.uin, 'V12', 'getChannelPermissionOfRole', [channel_id, role_id]);
10
+ }
11
+ async setChannelAnnounce(guild_id, channel_id, message_id) {
12
+ return this.adapter.call(this.oneBot.uin, 'V12', 'setChannelAnnounce', [guild_id, channel_id, message_id]);
13
+ }
14
+ async updateChannelPermissionOfRole(channel_id, role_id, permission) {
15
+ return this.adapter.call(this.oneBot.uin, 'V12', 'updateChannelPermissionOfRole', [channel_id, role_id, permission]);
16
+ }
17
+ async getChannelMemberPermission(channel_id, member_id) {
18
+ return this.adapter.call(this.oneBot.uin, 'V12', 'getChannelMemberPermission', [channel_id, member_id]);
19
+ }
20
+ async updateChannelMemberPermission(channel_id, member_id, permission) {
21
+ return this.adapter.call(this.oneBot.uin, 'V12', 'updateChannelMemberPermission', [channel_id, member_id, permission]);
22
+ }
23
+ async getChannelPins(channel_id) {
24
+ return this.adapter.call(this.oneBot.uin, 'V12', 'getChannelPins', [channel_id]);
25
+ }
26
+ async pinChannelMessage(channel_id, message_id) {
27
+ return this.adapter.call(this.oneBot.uin, 'V12', 'pinChannelMessage', [channel_id, message_id]);
28
+ }
29
+ async unPinChannelMessage(channel_id, message_id) {
30
+ return this.adapter.call(this.oneBot.uin, 'V12', 'unPinChannelMessage', [channel_id, message_id]);
31
+ }
32
+ async createChannel(guild_id, channelInfo) {
33
+ return this.adapter.call(this.oneBot.uin, 'V12', 'createChannel', [guild_id, channelInfo]);
34
+ }
35
+ async updateChannel({ channel_id, ...updateInfo }) {
36
+ return this.adapter.call(this.oneBot.uin, 'V12', 'updateChannel', [channel_id, updateInfo]);
37
+ }
38
+ async deleteChannel(channel_id) {
39
+ return this.adapter.call(this.oneBot.uin, 'V12', 'deleteChannel', [channel_id]);
40
+ }
41
+ async getGuildRoles(guild_id) {
42
+ return this.adapter.call(this.oneBot.uin, 'V12', 'getGuildRoles', [guild_id]);
43
+ }
44
+ async creatGuildRole(guild_id, role) {
45
+ return this.adapter.call(this.oneBot.uin, 'V12', 'creatGuildRole', [guild_id, role]);
46
+ }
47
+ async updateGuildRole(guild_id, { id, ...role }) {
48
+ return this.adapter.call(this.oneBot.uin, 'V12', 'updateGuildRole', [guild_id, role]);
49
+ }
50
+ async deleteGuildRole(role_id) {
51
+ return this.adapter.call(this.oneBot.uin, 'V12', 'deleteGuildRole', [role_id]);
52
+ }
53
+ async getGuildAccessApis(guild_id) {
54
+ return this.adapter.call(this.oneBot.uin, 'V12', 'getGuildAccessApis', [guild_id]);
55
+ }
56
+ async applyGuildAccess(guild_id, channel_id, apiInfo, desc) {
57
+ return this.adapter.call(this.oneBot.uin, 'V12', 'applyGuildAccess', [guild_id, channel_id, apiInfo, desc]);
58
+ }
59
+ async unMuteGuild(guild_id) {
60
+ return this.adapter.call(this.oneBot.uin, 'V12', 'unMuteGuild', [guild_id]);
61
+ }
62
+ async muteGuild(guild_id, seconds, end_time) {
63
+ return this.adapter.call(this.oneBot.uin, 'V12', 'muteGuild', [guild_id, seconds, end_time]);
64
+ }
65
+ async unMuteGuildMembers(guild_id, member_ids) {
66
+ return this.adapter.call(this.oneBot.uin, 'V12', 'unMuteGuildMembers', [guild_id, member_ids]);
67
+ }
68
+ async muteGuildMembers(guild_id, member_ids, seconds, end_time) {
69
+ return this.adapter.call(this.oneBot.uin, 'V12', 'muteGuildMembers', [guild_id, member_ids, seconds, end_time]);
70
+ }
71
+ async addGuildMemberRoles(guild_id, channel_id, member_id, role_id) {
72
+ return this.adapter.call(this.oneBot.uin, 'V12', 'addGuildMemberRoles', [guild_id, channel_id, member_id, role_id]);
73
+ }
74
+ async removeGuildMemberRoles(guild_id, channel_id, member_id, role_id) {
75
+ return this.adapter.call(this.oneBot.uin, 'V12', 'removeGuildMemberRoles', [guild_id, channel_id, member_id, role_id]);
76
+ }
77
+ async kickGuildMember(guild_id, member_id, clean = 0, blacklist) {
78
+ return this.adapter.call(this.oneBot.uin, 'V12', 'kickGuildMember', [guild_id, member_id, clean, blacklist]);
79
+ }
80
+ async unMuteGuildMember(guild_id, member_id) {
81
+ return this.adapter.call(this.oneBot.uin, 'V12', 'unMuteGuildMember', [guild_id, member_id]);
82
+ }
83
+ async muteGuildMember(guild_id, member_id, seconds, end_time) {
84
+ return this.adapter.call(this.oneBot.uin, 'V12', 'muteGuildMember', [guild_id, member_id, seconds, end_time]);
85
+ }
6
86
  getGuildList() {
7
- return this.client.getGuildList();
87
+ return this.adapter.call(this.oneBot.uin, 'V12', 'getGuildList');
88
+ }
89
+ getGuildInfo(guild_id) {
90
+ return this.adapter.call(this.oneBot.uin, 'V12', 'getGuildInfo', [guild_id]);
8
91
  }
9
92
  getChannelList(guild_id) {
10
- return this.client.getChannelList(guild_id);
93
+ return this.adapter.call(this.oneBot.uin, 'V12', 'getChannelList', [guild_id]);
94
+ }
95
+ getChannelInfo(channel_id) {
96
+ return this.adapter.call(this.oneBot.uin, 'V12', 'getChannelInfo', [channel_id]);
11
97
  }
12
98
  getGuildMemberList(guild_id) {
13
- return this.client.getGuildMemberList(guild_id);
99
+ return this.adapter.call(this.oneBot.uin, 'V12', 'getGuildMemberList', [guild_id]);
14
100
  }
15
101
  /**
16
- * 发送群聊消息
17
- * @param guild_id {number} 频道id
102
+ * 发送频道消息
18
103
  * @param channel_id {string} 子频道id
19
104
  * @param message {import('icqq/lib/service').Sendable} 消息
20
105
  */
21
- async sendGuildMsg(guild_id, channel_id, message) {
22
- const { element } = await utils_1.processMessage.apply(this, [message]);
23
- if (!element.length)
24
- return;
25
- return await this.client.sendGuildMsg(guild_id, channel_id, element);
106
+ async sendGuildMsg(channel_id, message) {
107
+ return this.adapter.call(this.oneBot.uin, 'V12', 'sendGuildMessage', [channel_id, message]);
108
+ }
109
+ async createDirectSession(guild_id, user_id) {
110
+ return this.adapter.call(this.oneBot.uin, 'V12', 'createDirectSession', [guild_id, user_id]);
111
+ }
112
+ async sendDirectMsg(guild_id, message) {
113
+ return this.adapter.call(this.oneBot.uin, 'V12', 'sendDirectMessage', [guild_id, message]);
26
114
  }
27
115
  }
28
116
  exports.GuildAction = GuildAction;
@@ -4,7 +4,7 @@ import { GroupAction } from "./group";
4
4
  import { GuildAction } from "./guild";
5
5
  export interface Action extends CommonAction, FriendAction, GroupAction, GuildAction {
6
6
  }
7
- declare const Action_base: import("../../../utils").Class;
7
+ declare const Action_base: import("../../../utils").Class<any>;
8
8
  export declare class Action extends Action_base {
9
9
  }
10
10
  export {};
@@ -1,24 +1,24 @@
1
1
  /// <reference types="node" />
2
- import { Client, EventMap, MessageElem, Sendable as IcqqCanSend } from "icqq";
2
+ import { EventMap } from "icqq";
3
3
  import { Config } from './config';
4
4
  import { OneBot } from "../../onebot";
5
5
  import { Action } from "./action";
6
6
  import { Logger } from "log4js";
7
7
  import { WebSocket, WebSocketServer } from "ws";
8
8
  import { Service } from "../../service";
9
+ import { Dict } from "@zhinjs/shared";
9
10
  export declare class V12 extends Service<'V12'> implements OneBot.Base {
10
11
  oneBot: OneBot<'V12'>;
11
- client: Client;
12
- version: string;
12
+ config: OneBot.Config<'V12'>;
13
+ version: OneBot.Version;
13
14
  action: Action;
14
15
  protected timestamp: number;
15
16
  protected heartbeat?: NodeJS.Timeout;
16
17
  logger: Logger;
17
- private path;
18
18
  wss?: WebSocketServer;
19
19
  wsr: Set<WebSocket>;
20
20
  private db;
21
- constructor(oneBot: OneBot<'V12'>, client: Client, config: OneBot.Config<'V12'>);
21
+ constructor(oneBot: OneBot<'V12'>, config: OneBot.Config<'V12'>);
22
22
  get history(): V12.Payload<keyof Action>[];
23
23
  getFile(file_id: string): V12.FileInfo;
24
24
  delFile(file_id: string): boolean;
@@ -27,7 +27,7 @@ export declare class V12 extends Service<'V12'> implements OneBot.Base {
27
27
  file_id: string;
28
28
  } & V12.FileInfo)[];
29
29
  set history(value: any[]);
30
- start(path?: string): void;
30
+ start(): void;
31
31
  private startHttp;
32
32
  private startWebhook;
33
33
  private runActions;
@@ -66,8 +66,6 @@ export declare class V12 extends Service<'V12'> implements OneBot.Base {
66
66
  }
67
67
  export declare namespace V12 {
68
68
  type Sendable = string | SegmentElem | (string | SegmentElem)[];
69
- function fromSegment(msgList: Sendable[]): MessageElem[];
70
- function toSegment(msgList: IcqqCanSend): SegmentElem<keyof SegmentMap>[];
71
69
  interface SegmentMap {
72
70
  face: {
73
71
  id: number;
@@ -172,20 +170,23 @@ export declare namespace V12 {
172
170
  echo?: string;
173
171
  }
174
172
  const defaultConfig: Config;
175
- type Payload<T extends any> = {
173
+ type Payload<T = Dict> = {
176
174
  id: string;
177
175
  impl: 'onebots';
178
176
  version: 12;
179
- platform: 'qq';
177
+ platform: string;
180
178
  self: {
181
- platform: 'qq';
182
- user_id: `${number}`;
179
+ platform: string;
180
+ user_id: string;
183
181
  };
184
182
  time: number;
185
183
  type: 'meta' | 'message' | 'notice' | 'request';
186
184
  detail_type: string;
187
185
  sub_type: string;
188
186
  } & T;
187
+ type SelfInfo = {
188
+ nickname?: string;
189
+ };
189
190
  interface Protocol {
190
191
  action: string;
191
192
  params: any;
@@ -220,7 +221,7 @@ export declare namespace V12 {
220
221
  };
221
222
  function success<T extends any>(data: T, retcode?: Result<T>['retcode'], echo?: string): Result<T>;
222
223
  function error(message: string, retcode?: Result<null>['retcode'], echo?: string): Result<null>;
223
- function formatPayload<K extends keyof BotEventMap>(uin: number, type: K, data: Omit<BotEventMap[K], K>): {
224
+ function formatPayload<K extends keyof BotEventMap>(uin: string, type: K, data: Omit<BotEventMap[K], K>): {
224
225
  self_id: string;
225
226
  time: number;
226
227
  detail_type: K;
@@ -246,4 +247,27 @@ export declare namespace V12 {
246
247
  sha256?: string;
247
248
  total_size?: number;
248
249
  };
250
+ interface GroupInfo {
251
+ group_id: string;
252
+ group_name: string;
253
+ }
254
+ interface UserInfo {
255
+ user_id: string;
256
+ user_name: string;
257
+ }
258
+ interface GroupMemberInfo {
259
+ group_id: string;
260
+ user_id: string;
261
+ user_name: string;
262
+ }
263
+ interface Segment {
264
+ }
265
+ interface Message {
266
+ }
267
+ interface MessageElement extends Dict {
268
+ type: string;
269
+ }
270
+ interface MessageRet {
271
+ message_id: string;
272
+ }
249
273
  }
@@ -4,7 +4,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.V12 = void 0;
7
- const icqq_1 = require("icqq");
8
7
  const utils_1 = require("../../utils");
9
8
  const path_1 = require("path");
10
9
  const onebot_1 = require("../../onebot");
@@ -15,22 +14,21 @@ const https_1 = __importDefault(require("https"));
15
14
  const ws_1 = require("ws");
16
15
  const utils_2 = require("../../utils");
17
16
  const db_1 = require("../../db");
18
- const app_1 = require("../../server/app");
19
- const fs_1 = require("fs");
20
17
  const message_1 = require("icqq/lib/message");
21
18
  const service_1 = require("../../service");
19
+ const app_1 = require("../../server/app");
22
20
  class V12 extends service_1.Service {
23
- constructor(oneBot, client, config) {
24
- super(config);
21
+ constructor(oneBot, config) {
22
+ super(oneBot.adapter, config);
25
23
  this.oneBot = oneBot;
26
- this.client = client;
24
+ this.config = config;
27
25
  this.version = 'V12';
28
26
  this.timestamp = Date.now();
29
27
  this.wsr = new Set();
30
28
  this.db = new db_1.Database((0, path_1.join)(app_1.App.configDir, 'data', this.oneBot.uin + '.json'));
31
29
  this.db.sync({ eventBuffer: [], msgIdMap: {}, files: {} });
32
30
  this.action = new action_1.Action();
33
- this.logger = this.oneBot.app.getLogger(this.oneBot.uin, this.version);
31
+ this.logger = this.oneBot.adapter.getLogger(this.oneBot.uin, this.version);
34
32
  }
35
33
  get history() {
36
34
  return this.db.get('eventBuffer');
@@ -59,10 +57,7 @@ class V12 extends service_1.Service {
59
57
  set history(value) {
60
58
  this.db.set('eventBuffer', value);
61
59
  }
62
- start(path) {
63
- this.path = `/${this.oneBot.uin}`;
64
- if (path)
65
- this.path += path;
60
+ start() {
66
61
  if (this.config.use_http) {
67
62
  const config = typeof this.config.use_http === 'boolean' ? {} : this.config.use_http || {};
68
63
  this.startHttp({
@@ -109,6 +104,25 @@ class V12 extends service_1.Service {
109
104
  }
110
105
  this.startWsReverse(config);
111
106
  });
107
+ this.on('dispatch', (unserialized) => {
108
+ const serialized = JSON.stringify(unserialized);
109
+ for (const ws of this.wss.clients) {
110
+ ws.send(serialized, (err) => {
111
+ if (err)
112
+ this.logger.error(`正向WS(${ws.url})上报事件失败: ` + err.message);
113
+ else
114
+ this.logger.debug(`正向WS(${ws.url})上报事件成功: ` + serialized);
115
+ });
116
+ }
117
+ for (const ws of this.wsr) {
118
+ ws.send(serialized, (err) => {
119
+ if (err)
120
+ this.logger.error(`反向WS(${ws.url})上报事件失败: ` + err.message);
121
+ else
122
+ this.logger.debug(`反向WS(${ws.url})上报事件成功: ` + serialized);
123
+ });
124
+ }
125
+ });
112
126
  if (this.config.heartbeat) {
113
127
  this.heartbeat = setInterval(() => {
114
128
  this.dispatch(V12.formatPayload(this.oneBot.uin, 'heartbeat', {
@@ -118,6 +132,18 @@ class V12 extends service_1.Service {
118
132
  }));
119
133
  }, this.config.heartbeat * 1000);
120
134
  }
135
+ this.adapter.on('message.receive', (uin, event) => {
136
+ const payload = this.adapter.formatEventPayload('V12', 'message', event);
137
+ this.dispatch(payload);
138
+ });
139
+ this.adapter.on('notice.receive', (uin, event) => {
140
+ const payload = this.adapter.formatEventPayload('V12', 'notice', event);
141
+ this.dispatch(payload);
142
+ });
143
+ this.adapter.on('request.receive', (uin, event) => {
144
+ const payload = this.adapter.formatEventPayload('V12', 'request', event);
145
+ this.dispatch(payload);
146
+ });
121
147
  }
122
148
  startHttp(config) {
123
149
  this.oneBot.app.router.all(this.path, (ctx) => this.httpRequestHandler(ctx, config));
@@ -257,43 +283,15 @@ class V12 extends service_1.Service {
257
283
  }
258
284
  this._webSocketHandler(ws);
259
285
  });
260
- this.on('dispatch', (unserialized) => {
261
- const serialized = JSON.stringify(unserialized);
262
- for (const ws of this.wss.clients) {
263
- ws.send(serialized, (err) => {
264
- if (err)
265
- this.logger.error(`正向WS(${ws.url})上报事件失败: ` + err.message);
266
- else
267
- this.logger.debug(`正向WS(${ws.url})上报事件成功: ` + serialized);
268
- });
269
- }
270
- });
271
286
  }
272
287
  startWsReverse(config) {
273
288
  this._createWsr(config.url, config);
274
- this.on('dispatch', (unserialized) => {
275
- const serialized = JSON.stringify(unserialized);
276
- for (const ws of this.wsr) {
277
- ws.send(serialized, (err) => {
278
- if (err)
279
- this.logger.error(`反向WS(${ws.url})上报事件失败: ` + err.message);
280
- else
281
- this.logger.debug(`反向WS(${ws.url})上报事件成功: ` + serialized);
282
- });
283
- }
284
- });
285
289
  }
286
290
  async stop(force) {
287
- if (this.client.status === icqq_1.OnlineStatus.Online) {
288
- await this.client.terminate();
289
- }
290
291
  this.wss.close();
291
292
  for (const ws of this.wsr) {
292
293
  ws.close();
293
294
  }
294
- if (force) {
295
- (0, fs_1.rmSync)(this.client.dir, { force: true, recursive: true });
296
- }
297
295
  }
298
296
  format(event, ...args) {
299
297
  const data = (typeof args[0]) === 'object' ? args.shift() || {} : {};
@@ -338,7 +336,7 @@ class V12 extends service_1.Service {
338
336
  data.self = this.action.getSelfInfo.apply(this);
339
337
  if (!data.detail_type)
340
338
  data.detail_type = data.message_type || data.notice_type || data.request_type || data.system_type;
341
- data.message = data.type === 'message' ? V12.toSegment(data.message) : data.message;
339
+ data.message = data.type === 'message' ? this.adapter.toSegment('V12', data.message) : data.message;
342
340
  if (data.source)
343
341
  data.source = {
344
342
  ...data.source,
@@ -373,7 +371,7 @@ class V12 extends service_1.Service {
373
371
  this.emit('dispatch', payload);
374
372
  }
375
373
  async apply(req) {
376
- let { action, params, echo } = req;
374
+ let { action = "", params = {}, echo } = req;
377
375
  action = (0, utils_2.toLine)(action);
378
376
  let is_async = action.includes("_async");
379
377
  if (is_async)
@@ -405,16 +403,31 @@ class V12 extends service_1.Service {
405
403
  if (Reflect.has(params, k)) {
406
404
  if (onebot_1.BOOLS.includes(k))
407
405
  params[k] = (0, utils_2.toBool)(params[k]);
406
+ if (k === 'message') {
407
+ if (typeof params[k] === 'string') {
408
+ if (/[CQ:music,type=.+,id=.+]/.test(params[k])) {
409
+ params[k] = params[k].replace(',type=', ',platform=');
410
+ }
411
+ params[k] = this.adapter.fromCqcode('V11', params[k]);
412
+ }
413
+ else {
414
+ if (params[k][0].type == 'music' && params[k][0]?.data?.type) {
415
+ params[k][0].data.platform = params[k][0].data.type;
416
+ delete params[k][0].data.type;
417
+ }
418
+ params[k] = this.adapter.fromSegment('V11', params[k]);
419
+ }
420
+ }
408
421
  args.push(params[k]);
409
422
  }
410
423
  }
411
424
  let ret, result;
412
425
  try {
413
- console.log(method, args);
414
426
  ret = this.action[method].apply(this, args);
415
427
  }
416
428
  catch (e) {
417
- return JSON.stringify(V12.error(e.message));
429
+ this.logger.error(e);
430
+ return "API 调用异常";
418
431
  }
419
432
  if (ret instanceof Promise) {
420
433
  if (is_async) {
@@ -429,6 +442,8 @@ class V12 extends service_1.Service {
429
442
  }
430
443
  if (result.data instanceof Map)
431
444
  result.data = [...result.data.values()];
445
+ if (result.data?.message)
446
+ result.data.message = this.adapter.toSegment('V12', result.data.message);
432
447
  if (echo) {
433
448
  result.echo = echo;
434
449
  }
@@ -531,16 +546,16 @@ class V12 extends service_1.Service {
531
546
  }
532
547
  if (event.detail_type === "group") {
533
548
  if (res.delete)
534
- this.client.deleteMsg(event.message_id);
549
+ this.adapter.call(this.oneBot.uin, 'V12', 'deleteMsg', [event.message_id]);
535
550
  if (res.kick && !event.anonymous)
536
- this.client.setGroupKick(event.group_id, event.user_id, res.reject_add_request);
551
+ this.adapter.call(this.oneBot.uin, 'V12', 'setGroupKick', [event.group_id, event.user_id, res.reject_add_request]);
537
552
  if (res.ban)
538
- this.client.setGroupBan(event.group_id, event.user_id, res.ban_duration > 0 ? res.ban_duration : 1800);
553
+ this.adapter.call(this.oneBot.uin, 'V12', 'setGroupBan', [event.group_id, event.user_id, res.ban_duration > 0 ? res.ban_duration : 1800]);
539
554
  }
540
555
  }
541
556
  if (event.type === "request" && "approve" in res) {
542
557
  const action = event.detail_type === "friend" ? "setFriendAddRequest" : "setGroupAddRequest";
543
- this.client[action](event.flag, res.approve, res.reason ? res.reason : "", !!res.block);
558
+ this.adapter.call(this.oneBot.uin, 'V12', action, [event.flag, res.approve, res.reason ? res.reason : "", !!res.block]);
544
559
  }
545
560
  }
546
561
  /**
@@ -639,52 +654,6 @@ class V12 extends service_1.Service {
639
654
  exports.V12 = V12;
640
655
  (function (V12) {
641
656
  const fileTypes = ['image', "file", 'record', 'video', 'flash'];
642
- function fromSegment(msgList) {
643
- msgList = [].concat(msgList);
644
- return msgList.map((msg) => {
645
- if (typeof msg !== 'object')
646
- msg = String(msg);
647
- if (typeof msg === 'string') {
648
- return { type: 'text', text: msg };
649
- }
650
- const { type, data = {}, ...other } = msg;
651
- Object.assign(data, other);
652
- if (type === 'music' && !data['platform']) {
653
- data['platform'] = data['type'];
654
- delete data['type'];
655
- }
656
- if (type === 'mention')
657
- data['qq'] = Number(data['user_id']);
658
- if (fileTypes.includes(type) && !data['file']) {
659
- data['file'] = data['file_id'];
660
- delete data['file_id'];
661
- }
662
- return {
663
- type: type.replace('mention', 'at').replace('at_all', 'at'),
664
- ...other,
665
- ...data
666
- };
667
- });
668
- }
669
- V12.fromSegment = fromSegment;
670
- function toSegment(msgList) {
671
- msgList = [].concat(msgList);
672
- return msgList.map((msg) => {
673
- if (typeof msg === 'string')
674
- return { type: 'text', data: { text: msg } };
675
- let { type, ...other } = msg;
676
- if (fileTypes.includes(type))
677
- other['file_id'] = other['file'];
678
- return {
679
- type: type === 'at' ? other['qq'] ? 'mention' : "mention_all" : type,
680
- data: {
681
- ...other,
682
- user_id: other['qq']
683
- }
684
- };
685
- });
686
- }
687
- V12.toSegment = toSegment;
688
657
  V12.defaultConfig = {
689
658
  heartbeat: 3,
690
659
  access_token: '',
@@ -719,7 +688,7 @@ exports.V12 = V12;
719
688
  V12.error = error;
720
689
  function formatPayload(uin, type, data) {
721
690
  return {
722
- self_id: uin + '',
691
+ self_id: uin,
723
692
  time: Math.floor(Date.now() / 1000),
724
693
  detail_type: type,
725
694
  type: 'meta',
package/lib/service.d.ts CHANGED
@@ -2,12 +2,17 @@
2
2
  import { EventEmitter } from "events";
3
3
  import { OneBot } from "./onebot";
4
4
  import { Dict } from "@zhinjs/shared";
5
+ import { Adapter } from "./adapter";
5
6
  export interface Service<V extends OneBot.Version> {
6
7
  filterFn(event: Dict): boolean;
7
8
  }
8
9
  export declare class Service<V extends OneBot.Version> extends EventEmitter {
9
- config: OneBot.Config<V>;
10
- constructor(config: OneBot.Config<V>);
10
+ adapter: Adapter;
11
+ config: OneBot.Config;
12
+ oneBot: OneBot;
13
+ version: OneBot.Version;
14
+ protected get path(): string;
15
+ constructor(adapter: Adapter, config: OneBot.Config);
11
16
  }
12
17
  export declare namespace Service {
13
18
  type MaybeArray<T = any> = T | T[];
package/lib/service.js CHANGED
@@ -3,8 +3,12 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Service = void 0;
4
4
  const events_1 = require("events");
5
5
  class Service extends events_1.EventEmitter {
6
- constructor(config) {
6
+ get path() {
7
+ return `/${this.oneBot.platform}/${this.oneBot.uin}/${this.version}`;
8
+ }
9
+ constructor(adapter, config) {
7
10
  super();
11
+ this.adapter = adapter;
8
12
  this.config = config;
9
13
  this.filterFn = Service.createFilterFunction(config.filters || {});
10
14
  }
package/lib/types.d.ts CHANGED
@@ -2,8 +2,8 @@ export type LogLevel = "trace" | "debug" | "info" | "warn" | "error" | "fatal" |
2
2
  export type Dispose = () => any;
3
3
  export type MayBeArray<T extends any> = T | T[];
4
4
  /**
5
- * 异步锁---
6
- */
5
+ * 异步锁---
6
+ */
7
7
  export declare class AsyncLock {
8
8
  private _lock;
9
9
  private _waitList;
package/lib/types.js CHANGED
@@ -2,8 +2,8 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.AsyncLock = void 0;
4
4
  /**
5
- * 异步锁---
6
- */
5
+ * 异步锁---
6
+ */
7
7
  class AsyncLock {
8
8
  constructor() {
9
9
  this._lock = false;
@@ -11,7 +11,7 @@ class AsyncLock {
11
11
  }
12
12
  async lock() {
13
13
  if (this._lock) {
14
- await new Promise((resolve) => {
14
+ await new Promise(resolve => {
15
15
  this._waitList.push(resolve);
16
16
  });
17
17
  }
package/lib/utils.d.ts CHANGED
@@ -4,6 +4,9 @@ export declare function transformObj(obj: any, callback: any): any;
4
4
  export declare function deepClone<T extends any>(obj: T): T;
5
5
  export declare function pick<T extends object, K extends keyof T>(source: T, keys?: Iterable<K>, forced?: boolean): Pick<T, K>;
6
6
  export declare function omit<T, K extends keyof T>(source: T, keys?: Iterable<K>): Omit<T, K>;
7
+ export declare function randomId(seed: string): number;
8
+ export declare function randomId(seed: string, length: number): number;
9
+ export declare function randomId(seed: string, min: number, max: number): number;
7
10
  /**
8
11
  * 将驼峰命名替换为下划线分割命名
9
12
  * @param name
@@ -11,13 +14,13 @@ export declare function omit<T, K extends keyof T>(source: T, keys?: Iterable<K>
11
14
  * @todo 是否应该改名 ToUnderLine()?
12
15
  */
13
16
  export declare function toLine<T extends string>(name: T): string;
14
- export interface Class {
15
- new (...args: any[]): any;
17
+ export interface Class<T = any> {
18
+ new (...args: any[]): T;
16
19
  }
17
- export declare function Mixin(base: Class, ...classes: Class[]): Class;
20
+ export declare function Mixin(base: Class, ...classes: Class[]): Class<any>;
18
21
  export declare function toHump(action: string): string;
19
22
  export declare function remove<T>(list: T[], item: T): void;
20
23
  export declare function toBool(v: any): boolean;
21
24
  export declare function uuid(): string;
22
- export declare function protectedFields<T>(source: T, ...keys: (keyof T)[]): T;
25
+ export declare function protectedFields<T>(source: T, ...keys: ((keyof T) | string)[]): T;
23
26
  export declare function getProperties(obj: any): any;
package/lib/utils.js CHANGED
@@ -22,9 +22,13 @@ var __importStar = (this && this.__importStar) || function (mod) {
22
22
  __setModuleDefault(result, mod);
23
23
  return result;
24
24
  };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
25
28
  Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.getProperties = exports.protectedFields = exports.uuid = exports.toBool = exports.remove = exports.toHump = exports.Mixin = exports.toLine = exports.omit = exports.pick = exports.deepClone = exports.transformObj = exports.deepMerge = exports.version = void 0;
29
+ exports.getProperties = exports.protectedFields = exports.uuid = exports.toBool = exports.remove = exports.toHump = exports.Mixin = exports.toLine = exports.randomId = exports.omit = exports.pick = exports.deepClone = exports.transformObj = exports.deepMerge = exports.version = void 0;
27
30
  const crypto = __importStar(require("crypto"));
31
+ const seed_random_1 = __importDefault(require("seed-random"));
28
32
  const packageJson = require('../package.json');
29
33
  exports.version = packageJson.version;
30
34
  // 合并对象/数组
@@ -114,6 +118,19 @@ function omit(source, keys) {
114
118
  return result;
115
119
  }
116
120
  exports.omit = omit;
121
+ function randomId(seed, ...args) {
122
+ let [min = 0, max = 1] = args;
123
+ let formatter = (n) => n;
124
+ if (args.length === 1) {
125
+ const len = Math.min(Number.MAX_SAFE_INTEGER.toString().length, args[0]);
126
+ min = 10 ** (len - 1);
127
+ max = Math.min(Number.MAX_SAFE_INTEGER, 10 ** len - 1);
128
+ formatter = (n) => Math.floor(n);
129
+ }
130
+ const rand = (0, seed_random_1.default)(seed);
131
+ return formatter(rand() * (max - min) + min);
132
+ }
133
+ exports.randomId = randomId;
117
134
  /**
118
135
  * 将驼峰命名替换为下划线分割命名
119
136
  * @param name