onebots 0.4.23 → 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 (77) hide show
  1. package/README.md +25 -3
  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/qq/bot.d.ts +24 -0
  7. package/lib/adapters/qq/bot.js +20 -0
  8. package/lib/adapters/qq/constans.d.ts +69 -0
  9. package/lib/adapters/qq/constans.js +128 -0
  10. package/lib/adapters/qq/elements.d.ts +125 -0
  11. package/lib/adapters/qq/elements.js +8 -0
  12. package/lib/adapters/qq/entries/channel.d.ts +17 -0
  13. package/lib/adapters/qq/entries/channel.js +2 -0
  14. package/lib/adapters/qq/entries/friend.d.ts +8 -0
  15. package/lib/adapters/qq/entries/friend.js +2 -0
  16. package/lib/adapters/qq/entries/group.d.ts +6 -0
  17. package/lib/adapters/qq/entries/group.js +2 -0
  18. package/lib/adapters/qq/entries/groupMember.d.ts +7 -0
  19. package/lib/adapters/qq/entries/groupMember.js +2 -0
  20. package/lib/adapters/qq/entries/guild.d.ts +21 -0
  21. package/lib/adapters/qq/entries/guild.js +2 -0
  22. package/lib/adapters/qq/entries/guildMember.d.ts +9 -0
  23. package/lib/adapters/qq/entries/guildMember.js +2 -0
  24. package/lib/adapters/qq/entries/user.d.ts +9 -0
  25. package/lib/adapters/qq/entries/user.js +2 -0
  26. package/lib/adapters/qq/event.d.ts +28 -0
  27. package/lib/adapters/qq/event.js +24 -0
  28. package/lib/adapters/qq/index.d.ts +35 -0
  29. package/lib/adapters/qq/index.js +170 -0
  30. package/lib/adapters/qq/message.d.ts +83 -0
  31. package/lib/adapters/qq/message.js +272 -0
  32. package/lib/adapters/qq/qqBot.d.ts +122 -0
  33. package/lib/adapters/qq/qqBot.js +438 -0
  34. package/lib/adapters/qq/sessionManager.d.ts +33 -0
  35. package/lib/adapters/qq/sessionManager.js +257 -0
  36. package/lib/adapters/qq/types.d.ts +45 -0
  37. package/lib/adapters/qq/types.js +2 -0
  38. package/lib/adapters/qq/utils.d.ts +12 -0
  39. package/lib/adapters/qq/utils.js +86 -0
  40. package/lib/bin.js +17 -2
  41. package/lib/config.sample.yaml +11 -5
  42. package/lib/onebot.d.ts +32 -24
  43. package/lib/onebot.js +51 -188
  44. package/lib/server/app.d.ts +22 -12
  45. package/lib/server/app.js +97 -71
  46. package/lib/service/V11/action/common.d.ts +9 -11
  47. package/lib/service/V11/action/common.js +27 -64
  48. package/lib/service/V11/action/friend.d.ts +5 -13
  49. package/lib/service/V11/action/friend.js +6 -16
  50. package/lib/service/V11/action/group.d.ts +19 -22
  51. package/lib/service/V11/action/group.js +20 -30
  52. package/lib/service/V11/action/index.d.ts +1 -1
  53. package/lib/service/V11/db_entities.d.ts +1 -1
  54. package/lib/service/V11/db_entities.js +1 -1
  55. package/lib/service/V11/db_sqlite.d.ts +1 -1
  56. package/lib/service/V11/index.d.ts +47 -16
  57. package/lib/service/V11/index.js +142 -164
  58. package/lib/service/V12/action/common.d.ts +3 -67
  59. package/lib/service/V12/action/common.js +9 -63
  60. package/lib/service/V12/action/friend.d.ts +4 -10
  61. package/lib/service/V12/action/friend.js +4 -12
  62. package/lib/service/V12/action/group.d.ts +19 -21
  63. package/lib/service/V12/action/group.js +19 -27
  64. package/lib/service/V12/action/guild.d.ts +40 -13
  65. package/lib/service/V12/action/guild.js +99 -11
  66. package/lib/service/V12/action/index.d.ts +1 -1
  67. package/lib/service/V12/index.d.ts +37 -13
  68. package/lib/service/V12/index.js +44 -72
  69. package/lib/service.d.ts +7 -2
  70. package/lib/service.js +32 -30
  71. package/lib/utils.d.ts +7 -4
  72. package/lib/utils.js +33 -28
  73. package/package.json +5 -4
  74. package/lib/service/V12/utils.d.ts +0 -8
  75. package/lib/service/V12/utils.js +0 -47
  76. /package/lib/{service/V11 → adapters/icqq}/utils.d.ts +0 -0
  77. /package/lib/{service/V11 → adapters/icqq}/utils.js +0 -0
@@ -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({
@@ -137,6 +132,18 @@ class V12 extends service_1.Service {
137
132
  }));
138
133
  }, this.config.heartbeat * 1000);
139
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
+ });
140
147
  }
141
148
  startHttp(config) {
142
149
  this.oneBot.app.router.all(this.path, (ctx) => this.httpRequestHandler(ctx, config));
@@ -281,16 +288,10 @@ class V12 extends service_1.Service {
281
288
  this._createWsr(config.url, config);
282
289
  }
283
290
  async stop(force) {
284
- if (this.client.status === icqq_1.OnlineStatus.Online) {
285
- await this.client.terminate();
286
- }
287
291
  this.wss.close();
288
292
  for (const ws of this.wsr) {
289
293
  ws.close();
290
294
  }
291
- if (force) {
292
- (0, fs_1.rmSync)(this.client.dir, { force: true, recursive: true });
293
- }
294
295
  }
295
296
  format(event, ...args) {
296
297
  const data = (typeof args[0]) === 'object' ? args.shift() || {} : {};
@@ -335,7 +336,7 @@ class V12 extends service_1.Service {
335
336
  data.self = this.action.getSelfInfo.apply(this);
336
337
  if (!data.detail_type)
337
338
  data.detail_type = data.message_type || data.notice_type || data.request_type || data.system_type;
338
- 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;
339
340
  if (data.source)
340
341
  data.source = {
341
342
  ...data.source,
@@ -370,7 +371,7 @@ class V12 extends service_1.Service {
370
371
  this.emit('dispatch', payload);
371
372
  }
372
373
  async apply(req) {
373
- let { action, params, echo } = req;
374
+ let { action = "", params = {}, echo } = req;
374
375
  action = (0, utils_2.toLine)(action);
375
376
  let is_async = action.includes("_async");
376
377
  if (is_async)
@@ -402,16 +403,31 @@ class V12 extends service_1.Service {
402
403
  if (Reflect.has(params, k)) {
403
404
  if (onebot_1.BOOLS.includes(k))
404
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
+ }
405
421
  args.push(params[k]);
406
422
  }
407
423
  }
408
424
  let ret, result;
409
425
  try {
410
- console.log(method, args);
411
426
  ret = this.action[method].apply(this, args);
412
427
  }
413
428
  catch (e) {
414
- return JSON.stringify(V12.error(e.message));
429
+ this.logger.error(e);
430
+ return "API 调用异常";
415
431
  }
416
432
  if (ret instanceof Promise) {
417
433
  if (is_async) {
@@ -426,6 +442,8 @@ class V12 extends service_1.Service {
426
442
  }
427
443
  if (result.data instanceof Map)
428
444
  result.data = [...result.data.values()];
445
+ if (result.data?.message)
446
+ result.data.message = this.adapter.toSegment('V12', result.data.message);
429
447
  if (echo) {
430
448
  result.echo = echo;
431
449
  }
@@ -528,16 +546,16 @@ class V12 extends service_1.Service {
528
546
  }
529
547
  if (event.detail_type === "group") {
530
548
  if (res.delete)
531
- this.client.deleteMsg(event.message_id);
549
+ this.adapter.call(this.oneBot.uin, 'V12', 'deleteMsg', [event.message_id]);
532
550
  if (res.kick && !event.anonymous)
533
- 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]);
534
552
  if (res.ban)
535
- 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]);
536
554
  }
537
555
  }
538
556
  if (event.type === "request" && "approve" in res) {
539
557
  const action = event.detail_type === "friend" ? "setFriendAddRequest" : "setGroupAddRequest";
540
- 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]);
541
559
  }
542
560
  }
543
561
  /**
@@ -636,52 +654,6 @@ class V12 extends service_1.Service {
636
654
  exports.V12 = V12;
637
655
  (function (V12) {
638
656
  const fileTypes = ['image', "file", 'record', 'video', 'flash'];
639
- function fromSegment(msgList) {
640
- msgList = [].concat(msgList);
641
- return msgList.map((msg) => {
642
- if (typeof msg !== 'object')
643
- msg = String(msg);
644
- if (typeof msg === 'string') {
645
- return { type: 'text', text: msg };
646
- }
647
- const { type, data = {}, ...other } = msg;
648
- Object.assign(data, other);
649
- if (type === 'music' && !data['platform']) {
650
- data['platform'] = data['type'];
651
- delete data['type'];
652
- }
653
- if (type === 'mention')
654
- data['qq'] = Number(data['user_id']);
655
- if (fileTypes.includes(type) && !data['file']) {
656
- data['file'] = data['file_id'];
657
- delete data['file_id'];
658
- }
659
- return {
660
- type: type.replace('mention', 'at').replace('at_all', 'at'),
661
- ...other,
662
- ...data
663
- };
664
- });
665
- }
666
- V12.fromSegment = fromSegment;
667
- function toSegment(msgList) {
668
- msgList = [].concat(msgList);
669
- return msgList.map((msg) => {
670
- if (typeof msg === 'string')
671
- return { type: 'text', data: { text: msg } };
672
- let { type, ...other } = msg;
673
- if (fileTypes.includes(type))
674
- other['file_id'] = other['file'];
675
- return {
676
- type: type === 'at' ? other['qq'] ? 'mention' : "mention_all" : type,
677
- data: {
678
- ...other,
679
- user_id: other['qq']
680
- }
681
- };
682
- });
683
- }
684
- V12.toSegment = toSegment;
685
657
  V12.defaultConfig = {
686
658
  heartbeat: 3,
687
659
  access_token: '',
@@ -716,7 +688,7 @@ exports.V12 = V12;
716
688
  V12.error = error;
717
689
  function formatPayload(uin, type, data) {
718
690
  return {
719
- self_id: uin + '',
691
+ self_id: uin,
720
692
  time: Math.floor(Date.now() / 1000),
721
693
  detail_type: type,
722
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
  }
@@ -14,61 +18,59 @@ exports.Service = Service;
14
18
  function createFilterFunction(filters) {
15
19
  const isLogicKey = (key) => {
16
20
  return [
17
- "$and",
18
- "$or",
19
- "$not",
20
- "$nor",
21
- "$regexp",
22
- "$like",
23
- "$gt",
24
- "$gte",
25
- "$lt",
26
- "$lte",
27
- "$between",
21
+ '$and',
22
+ '$or',
23
+ '$not',
24
+ '$nor',
25
+ '$regexp',
26
+ '$like',
27
+ '$gt',
28
+ '$gte',
29
+ '$lt',
30
+ '$lte',
31
+ '$between',
28
32
  ].includes(key);
29
33
  };
30
34
  const filterFn = (event, key, value) => {
31
35
  // 如果 key 为 $and、$or、$not、$nor 则递归调用
32
- if (key === "$and" || key === "$or" || key === "$not" || key === "$nor") {
33
- if (!value || typeof value !== "object")
36
+ if (key === "$and" || key === "$or" || key === "$not" || key === '$nor') {
37
+ if (!value || typeof value !== 'object')
34
38
  throw new Error("invalid filter");
35
39
  switch (key) {
36
40
  case "$and":
37
- return Array.isArray(value)
38
- ? value.every(item => filterFn(event, key, item))
39
- : Object.entries(value).every(([key, value]) => filterFn(event, key, value));
41
+ return Array.isArray(value) ? value.every((item) => filterFn(event, key, item)) :
42
+ Object.entries(value).every(([key, value]) => filterFn(event, key, value));
40
43
  case "$or":
41
- return Array.isArray(value)
42
- ? value.some(item => filterFn(event, key, item))
43
- : Object.entries(value).some(([key, value]) => filterFn(event, key, value));
44
+ return Array.isArray(value) ? value.some((item) => filterFn(event, key, item)) :
45
+ Object.entries(value).some(([key, value]) => filterFn(event, key, value));
44
46
  case "$nor":
45
- return !filterFn(event, "$or", value);
47
+ return !filterFn(event, '$or', value);
46
48
  case "$not":
47
- return !filterFn(event, "$and", value);
49
+ return !filterFn(event, '$and', value);
48
50
  }
49
51
  }
50
52
  if (typeof value === "boolean" && typeof event[key] !== "boolean") {
51
53
  return value;
52
54
  }
53
55
  if (typeof value !== "object") {
54
- if (key === "$regex" && typeof value === "string")
56
+ if (key === '$regex' && typeof value === 'string')
55
57
  return new RegExp(value).test(String(event));
56
- if (key === "$like" && typeof value === "string")
58
+ if (key === '$like' && typeof value === 'string')
57
59
  return String(event).includes(value);
58
- if (key === "$gt" && typeof value === "number")
60
+ if (key === '$gt' && typeof value === 'number')
59
61
  return Number(event) > value;
60
- if (key === "$gte" && typeof value === "number")
62
+ if (key === '$gte' && typeof value === 'number')
61
63
  return Number(event) >= value;
62
- if (key === "$lt" && typeof value === "number")
64
+ if (key === '$lt' && typeof value === 'number')
63
65
  return Number(event) < value;
64
- if (key === "$lte" && typeof value === "number")
66
+ if (key === '$lte' && typeof value === 'number')
65
67
  return Number(event) <= value;
66
68
  return value === event[key];
67
69
  }
68
- if (key === "$between" &&
70
+ if (key === '$between' &&
69
71
  Array.isArray(value) &&
70
72
  value.length === 2 &&
71
- value.every(item => typeof item === "number")) {
73
+ value.every((item) => typeof item === 'number')) {
72
74
  const [start, end] = value;
73
75
  return event >= start && event <= end;
74
76
  }
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;