@alemonjs/qq-bot 0.0.1 → 0.0.3

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/README.md CHANGED
@@ -30,6 +30,8 @@ qq-group-bot:
30
30
  port: 17157
31
31
  # 如果在服务器也启动了机器人,可以进行互相调用
32
32
  ws: 'ws://...'
33
+ # 频道沙盒
34
+ sandbox: false
33
35
  ```
34
36
 
35
37
  ## Community
package/lib/api.js CHANGED
@@ -23,36 +23,54 @@ class QQBotAPI {
23
23
  * @param url
24
24
  * @returns
25
25
  */
26
- getAuthentication(appId, clientSecret) {
26
+ getAuthentication(app_id, clientSecret) {
27
27
  return axios.post(`${this.BOTS_API_RUL}/app/getAppAccessToken`, {
28
- appId: appId,
28
+ appId: app_id,
29
29
  clientSecret: clientSecret
30
30
  });
31
31
  }
32
32
  /**
33
- * 创建axios实例
33
+ * group
34
34
  * @param config
35
35
  * @returns
36
36
  */
37
- async GroupService(options) {
38
- const appId = config.get('appId');
39
- const token = config.get('token');
37
+ async groupService(options) {
38
+ const app_id = config.get('app_id');
39
+ const token = config.get('access_token');
40
40
  const service = await axios.create({
41
41
  baseURL: this.API_URL,
42
42
  timeout: 20000,
43
43
  headers: {
44
- 'X-Union-Appid': appId,
44
+ 'X-Union-Appid': app_id,
45
45
  'Authorization': `QQBot ${token}`
46
46
  }
47
47
  });
48
48
  return service(options);
49
49
  }
50
+ /**
51
+ * guild
52
+ * @param opstion
53
+ * @returns
54
+ */
55
+ async guildServer(opstion) {
56
+ const app_id = config.get('app_id');
57
+ const token = config.get('token');
58
+ const sandbox = config.get('sandbox');
59
+ const service = await axios.create({
60
+ baseURL: sandbox ? this.API_URL_SANDBOX : this.API_URL,
61
+ timeout: 20000,
62
+ headers: {
63
+ Authorization: `Bot ${app_id}.${token}`
64
+ }
65
+ });
66
+ return service(opstion);
67
+ }
50
68
  /**
51
69
  * 得到鉴权
52
70
  * @returns
53
71
  */
54
72
  async gateway() {
55
- return this.GroupService({
73
+ return this.groupService({
56
74
  url: '/gateway'
57
75
  }).then(res => res?.data);
58
76
  }
@@ -66,7 +84,7 @@ class QQBotAPI {
66
84
  */
67
85
  async usersOpenMessages(openid, data, msg_id) {
68
86
  console.log('msg_id', msg_id);
69
- return this.GroupService({
87
+ return this.groupService({
70
88
  url: `/v2/users/${openid}/messages`,
71
89
  method: 'post',
72
90
  data: data
@@ -98,7 +116,7 @@ class QQBotAPI {
98
116
  * @returns
99
117
  */
100
118
  async groupOpenMessages(group_openid, data) {
101
- return this.GroupService({
119
+ return this.groupService({
102
120
  url: `/v2/groups/${group_openid}/messages`,
103
121
  method: 'post',
104
122
  data: data
@@ -114,7 +132,7 @@ class QQBotAPI {
114
132
  * 图片:png/jpg,视频:mp4,语音:silk
115
133
  */
116
134
  async postRichMediaByUsers(openid, data) {
117
- return this.GroupService({
135
+ return this.groupService({
118
136
  url: `/v2/users/${openid}/files`,
119
137
  method: 'post',
120
138
  data: data
@@ -130,7 +148,7 @@ class QQBotAPI {
130
148
  * 图片:png/jpg,视频:mp4,语音:silk
131
149
  */
132
150
  async userFiles(openid, data) {
133
- return this.GroupService({
151
+ return this.groupService({
134
152
  url: `/v2/users/${openid}/files`,
135
153
  method: 'post',
136
154
  data: data
@@ -146,7 +164,7 @@ class QQBotAPI {
146
164
  * 图片:png/jpg,视频:mp4,语音:silk
147
165
  */
148
166
  async postRichMediaByGroup(openid, data) {
149
- return this.GroupService({
167
+ return this.groupService({
150
168
  url: `/v2/groups/${openid}/files`,
151
169
  method: 'post',
152
170
  data: {
@@ -162,7 +180,7 @@ class QQBotAPI {
162
180
  * @returns
163
181
  */
164
182
  async groupsFiles(openid, data) {
165
- return this.GroupService({
183
+ return this.groupService({
166
184
  url: `/v2/groups/${openid}/files`,
167
185
  method: 'post',
168
186
  data: {
@@ -283,7 +301,7 @@ class QQBotAPI {
283
301
  * @returns
284
302
  */
285
303
  userMessageDelete(openid, message_id) {
286
- return this.GroupService({
304
+ return this.groupService({
287
305
  url: `/v2/users/${openid}/messages/${message_id}`,
288
306
  method: 'delete'
289
307
  }).then(res => res?.data);
@@ -295,29 +313,11 @@ class QQBotAPI {
295
313
  * @returns
296
314
  */
297
315
  grouMessageDelte(group_openid, message_id) {
298
- return this.GroupService({
316
+ return this.groupService({
299
317
  url: `/v2/groups/${group_openid}/messages/${message_id}`,
300
318
  method: 'delete'
301
319
  }).then(res => res?.data);
302
320
  }
303
- /**
304
- * 基础请求
305
- * @param opstion
306
- * @returns
307
- */
308
- async request(opstion) {
309
- const appId = config.get('appId');
310
- const token = config.get('token');
311
- const sandbox = config.get('sandbox');
312
- const service = await axios.create({
313
- baseURL: sandbox ? this.API_URL_SANDBOX : this.API_URL,
314
- timeout: 20000,
315
- headers: {
316
- Authorization: `Bot ${appId}.${token}`
317
- }
318
- });
319
- return service(opstion);
320
- }
321
321
  /**
322
322
  * 创建form
323
323
  * @param image
@@ -353,7 +353,7 @@ class QQBotAPI {
353
353
  async postImage(channel_id, message) {
354
354
  const formdata = await this.createFrom(message.image, message.msg_id, message.content, message.name);
355
355
  const dary = formdata != false ? formdata.getBoundary() : '';
356
- return this.request({
356
+ return this.guildServer({
357
357
  method: 'post',
358
358
  url: `/channels/${channel_id}/messages`,
359
359
  headers: {
@@ -371,7 +371,7 @@ class QQBotAPI {
371
371
  async postDirectImage(guild_id, message) {
372
372
  const formdata = await this.createFrom(message.image, message.msg_id, message.content, message.name);
373
373
  const dary = formdata != false ? formdata.getBoundary() : '';
374
- return this.request({
374
+ return this.guildServer({
375
375
  method: 'post',
376
376
  url: `/dms/${guild_id}/messages`,
377
377
  headers: {
@@ -391,7 +391,7 @@ class QQBotAPI {
391
391
  * @returns
392
392
  */
393
393
  async usersMe() {
394
- return this.request({
394
+ return this.guildServer({
395
395
  method: 'get',
396
396
  url: `/users/@me`
397
397
  }).then(res => res?.data);
@@ -402,7 +402,7 @@ class QQBotAPI {
402
402
  * @returns
403
403
  */
404
404
  async usersMeGuilds(params) {
405
- return this.request({
405
+ return this.guildServer({
406
406
  method: 'get',
407
407
  url: `/users/@me/guilds`,
408
408
  params
@@ -419,7 +419,7 @@ class QQBotAPI {
419
419
  * @returns
420
420
  */
421
421
  async guilds(guild_id) {
422
- return this.request({
422
+ return this.guildServer({
423
423
  method: 'get',
424
424
  url: `/guilds/${guild_id}`
425
425
  }).then(res => res?.data);
@@ -435,7 +435,7 @@ class QQBotAPI {
435
435
  * @returns
436
436
  */
437
437
  async guildsChannels(guild_id) {
438
- return this.request({
438
+ return this.guildServer({
439
439
  method: 'get',
440
440
  url: `/guilds/${guild_id}/channels`
441
441
  }).then(res => res?.data);
@@ -446,7 +446,7 @@ class QQBotAPI {
446
446
  * @returns
447
447
  */
448
448
  async channels(channel_id) {
449
- return this.request({
449
+ return this.guildServer({
450
450
  method: 'get',
451
451
  url: `/channels/${channel_id}`
452
452
  }).then(res => res?.data);
@@ -457,7 +457,7 @@ class QQBotAPI {
457
457
  * @returns
458
458
  */
459
459
  async guildsChannelsCreate(guild_id, data) {
460
- return this.request({
460
+ return this.guildServer({
461
461
  method: 'post',
462
462
  url: `/guilds/${guild_id}/channels`,
463
463
  data
@@ -469,7 +469,7 @@ class QQBotAPI {
469
469
  * @returns
470
470
  */
471
471
  async guildsChannelsUpdate(channel_id, data) {
472
- return this.request({
472
+ return this.guildServer({
473
473
  method: 'PATCH',
474
474
  url: `/channels/${channel_id}`,
475
475
  data
@@ -482,7 +482,7 @@ class QQBotAPI {
482
482
  * @returns
483
483
  */
484
484
  async guildsChannelsdelete(channel_id, data) {
485
- return this.request({
485
+ return this.guildServer({
486
486
  method: 'DELETE',
487
487
  url: `/channels/${channel_id}`,
488
488
  data
@@ -494,7 +494,7 @@ class QQBotAPI {
494
494
  * @returns
495
495
  */
496
496
  async channelsChannelOnlineNums(channel_id) {
497
- return this.request({
497
+ return this.guildServer({
498
498
  method: 'GET',
499
499
  url: `/channels/${channel_id}/online_nums`
500
500
  }).then(res => res?.data);
@@ -510,7 +510,7 @@ class QQBotAPI {
510
510
  * @returns
511
511
  */
512
512
  async guildsMembers(guild_id, params) {
513
- return this.request({
513
+ return this.guildServer({
514
514
  method: 'GET',
515
515
  url: `/guilds/${guild_id}/members`,
516
516
  params
@@ -524,7 +524,7 @@ class QQBotAPI {
524
524
  * @returns
525
525
  */
526
526
  async guildsRolesMembers(guild_id, role_id, params) {
527
- return this.request({
527
+ return this.guildServer({
528
528
  method: 'GET',
529
529
  url: `/guilds/${guild_id}/roles/${role_id}/members`,
530
530
  params
@@ -537,7 +537,7 @@ class QQBotAPI {
537
537
  * @returns
538
538
  */
539
539
  async guildsMembersMessage(guild_id, user_id) {
540
- return this.request({
540
+ return this.guildServer({
541
541
  method: 'GET',
542
542
  url: `/guilds/${guild_id}/members/${user_id}`
543
543
  }).then(res => res?.data);
@@ -549,7 +549,7 @@ class QQBotAPI {
549
549
  * @returns
550
550
  */
551
551
  async guildsMembersDelete(guild_id, user_id) {
552
- return this.request({
552
+ return this.guildServer({
553
553
  method: 'DELETE',
554
554
  url: `/guilds/${guild_id}/members/${user_id}`
555
555
  }).then(res => res?.data);
@@ -561,7 +561,7 @@ class QQBotAPI {
561
561
  * @returns
562
562
  */
563
563
  async channelsMessages(channel_id, message_id) {
564
- return this.request({
564
+ return this.guildServer({
565
565
  method: 'GET',
566
566
  url: `/channels/${channel_id}/messages/${message_id}`
567
567
  }).then(res => res?.data);
@@ -574,7 +574,7 @@ class QQBotAPI {
574
574
  * @returns
575
575
  */
576
576
  async channelsMessagesPost(channel_id, data) {
577
- return this.request({
577
+ return this.guildServer({
578
578
  method: 'POST',
579
579
  url: `/channels/${channel_id}/messages`,
580
580
  data
@@ -588,7 +588,7 @@ class QQBotAPI {
588
588
  * @returns
589
589
  */
590
590
  async channelsMessagesDelete(channel_id, message_id, hidetip = true) {
591
- return this.request({
591
+ return this.guildServer({
592
592
  method: 'DELETE',
593
593
  url: `/channels/${channel_id}/messages/${message_id}?hidetip=${hidetip}`
594
594
  }).then(res => res?.data);
@@ -604,7 +604,7 @@ class QQBotAPI {
604
604
  * @returns
605
605
  */
606
606
  async guildsRoles(guild_id) {
607
- return this.request({
607
+ return this.guildServer({
608
608
  method: 'GET',
609
609
  url: `/guilds/${guild_id}/roles`
610
610
  }).then(res => res?.data);
@@ -619,7 +619,7 @@ class QQBotAPI {
619
619
  * @returns
620
620
  */
621
621
  async guildsRolesPost(guild_id, data) {
622
- return this.request({
622
+ return this.guildServer({
623
623
  method: 'POST',
624
624
  url: `/guilds/${guild_id}/roles`,
625
625
  data
@@ -635,7 +635,7 @@ class QQBotAPI {
635
635
  * @returns
636
636
  */
637
637
  async guildsRolesPatch(guild_id, role_id, data) {
638
- return this.request({
638
+ return this.guildServer({
639
639
  method: 'PATCH',
640
640
  url: `/guilds/${guild_id}/roles/${role_id}`,
641
641
  data
@@ -647,7 +647,7 @@ class QQBotAPI {
647
647
  * @param role_id 身份组id
648
648
  */
649
649
  async guildsRolesDelete(guild_id, role_id) {
650
- return this.request({
650
+ return this.guildServer({
651
651
  method: 'DELETE',
652
652
  url: `/guilds/${guild_id}/roles/${role_id}`
653
653
  }).then(res => res?.data);
@@ -661,7 +661,7 @@ class QQBotAPI {
661
661
  * @returns
662
662
  */
663
663
  async guildsRolesMembersPut(guild_id, channel_id, user_id, role_id) {
664
- return this.request({
664
+ return this.guildServer({
665
665
  method: 'PUT',
666
666
  url: `/guilds/${guild_id}/members/${user_id}/roles/${role_id}`,
667
667
  data: {
@@ -680,7 +680,7 @@ class QQBotAPI {
680
680
  * @returns
681
681
  */
682
682
  async guildsRolesMembersDelete(guild_id, channel_id, user_id, role_id) {
683
- return this.request({
683
+ return this.guildServer({
684
684
  method: 'DELETE',
685
685
  url: `/guilds/${guild_id}/members/${user_id}/roles/${role_id}`,
686
686
  data: {
@@ -701,7 +701,7 @@ class QQBotAPI {
701
701
  * @param user_id 用户id
702
702
  */
703
703
  async channelsPermissions(channel_id, user_id) {
704
- return this.request({
704
+ return this.guildServer({
705
705
  method: 'GET',
706
706
  url: `/channels/${channel_id}/members/${user_id}/permissions`
707
707
  }).then(res => res?.data);
@@ -713,7 +713,7 @@ class QQBotAPI {
713
713
  * @param 参数包括add和remove两个字段分别表示授予的权限以及删除的权限。要授予用户权限即把add对应位置 1,删除用户权限即把remove对应位置 1。当两个字段同一位都为 1,表现为删除权限。
714
714
  */
715
715
  async channelsPermissionsPut(channel_id, user_id, add, remove) {
716
- return this.request({
716
+ return this.guildServer({
717
717
  method: 'PUT',
718
718
  url: `/channels/${channel_id}/members/${user_id}/permissions`,
719
719
  data: {
@@ -738,7 +738,7 @@ class QQBotAPI {
738
738
  * @returns
739
739
  */
740
740
  async guildsMessageSetting(guild_id) {
741
- return this.request({
741
+ return this.guildServer({
742
742
  method: 'GET',
743
743
  url: `/guilds/${guild_id}/message/setting`
744
744
  }).then(res => res?.data);
@@ -755,7 +755,7 @@ class QQBotAPI {
755
755
  * @returns
756
756
  */
757
757
  async usersMeDms() {
758
- return this.request({
758
+ return this.guildServer({
759
759
  method: 'POST',
760
760
  url: `/users/@me/dms`
761
761
  }).then(res => res?.data);
@@ -766,7 +766,7 @@ class QQBotAPI {
766
766
  * @returns
767
767
  */
768
768
  async dmsMessage(guild_id, data) {
769
- return this.request({
769
+ return this.guildServer({
770
770
  method: 'POST',
771
771
  url: `/dms/${guild_id}/messages`,
772
772
  data
@@ -779,7 +779,7 @@ class QQBotAPI {
779
779
  * @returns
780
780
  */
781
781
  async dmsMessageDelete(guild_id, message_id, hidetip = true) {
782
- return this.request({
782
+ return this.guildServer({
783
783
  method: 'DELETE',
784
784
  url: `/dms/${guild_id}/messages/${message_id}?hidetip=${hidetip}`
785
785
  }).then(res => res?.data);
@@ -796,7 +796,7 @@ class QQBotAPI {
796
796
  * 将mute_end_timestamp或mute_seconds传值为字符串'0',则表示解除全体禁言
797
797
  */
798
798
  async guildsMuteAll(guild_id, data) {
799
- return this.request({
799
+ return this.guildServer({
800
800
  method: 'PATCH',
801
801
  url: `/guilds/${guild_id}/mute`,
802
802
  data
@@ -811,7 +811,7 @@ class QQBotAPI {
811
811
  * @returns
812
812
  */
813
813
  async guildsMemberMute(guild_id, user_id, data) {
814
- return this.request({
814
+ return this.guildServer({
815
815
  method: 'PATCH',
816
816
  url: `/guilds/${guild_id}/members/${user_id}/mute`,
817
817
  data
@@ -824,7 +824,7 @@ class QQBotAPI {
824
824
  * 将mute_end_timestamp或mute_seconds传值为字符串'0',则表示解除禁言
825
825
  */
826
826
  async guildsMute(guild_id, data) {
827
- return this.request({
827
+ return this.guildServer({
828
828
  method: 'PATCH',
829
829
  url: `/guilds/${guild_id}/mute`,
830
830
  data
@@ -847,7 +847,7 @@ class QQBotAPI {
847
847
  * @returns 返回Announces 对象 (https://bot.q.qq.com/wiki/develop/api-v2/server-inter/channel/content/announces/model.html#Announces)
848
848
  */
849
849
  async guildsAnnounces(guild_id, data) {
850
- return this.request({
850
+ return this.guildServer({
851
851
  method: 'POST',
852
852
  url: `/guilds/${guild_id}/announces`,
853
853
  data
@@ -860,7 +860,7 @@ class QQBotAPI {
860
860
  * @returns
861
861
  */
862
862
  async guildsAnnouncesDelete(guild_id, message_id) {
863
- return this.request({
863
+ return this.guildServer({
864
864
  method: 'DELETE',
865
865
  url: `/guilds/${guild_id}/announces/${message_id}`
866
866
  }).then(res => res?.data);
@@ -878,7 +878,7 @@ class QQBotAPI {
878
878
  * @returns message_ids 为当前请求后子频道内所有精华消息 message_id 数组
879
879
  */
880
880
  async channelsPinsPut(channel_id, message_id) {
881
- return this.request({
881
+ return this.guildServer({
882
882
  method: 'PUT',
883
883
  url: `/channels/${channel_id}/pins/${message_id}`
884
884
  }).then(res => res?.data);
@@ -891,7 +891,7 @@ class QQBotAPI {
891
891
  * @returns
892
892
  */
893
893
  async channelsPinsDelete(channel_id, message_id) {
894
- return this.request({
894
+ return this.guildServer({
895
895
  method: 'DELETE',
896
896
  url: `/channels/${channel_id}/pins/${message_id}`
897
897
  }).then(res => res?.data);
@@ -903,7 +903,7 @@ class QQBotAPI {
903
903
  * @returns message_ids 为当前请求后子频道内所有精华消息 message_id 数组
904
904
  */
905
905
  async channelsPins(channel_id) {
906
- return this.request({
906
+ return this.guildServer({
907
907
  method: 'GET',
908
908
  url: `/channels/${channel_id}/pins`
909
909
  }).then(res => res?.data);
@@ -919,7 +919,7 @@ class QQBotAPI {
919
919
  * @returns 返回 Schedule 对象数组(详见https://bot.q.qq.com/wiki/develop/api-v2/server-inter/channel/content/schedule/model.html#schedule)
920
920
  */
921
921
  async channelsSchedules(channel_id) {
922
- return this.request({
922
+ return this.guildServer({
923
923
  method: 'GET',
924
924
  url: `/channels/${channel_id}/schedules`
925
925
  }).then(res => res?.data);
@@ -931,7 +931,7 @@ class QQBotAPI {
931
931
  * @returns 返回 Schedule 对象(详见https://bot.q.qq.com/wiki/develop/api-v2/server-inter/channel/content/schedule/model.html#schedule)
932
932
  */
933
933
  async channelsSchedulesSchedule(channel_id, schedule_id) {
934
- return this.request({
934
+ return this.guildServer({
935
935
  method: 'GET',
936
936
  url: `/channels/${channel_id}/schedules/${schedule_id}`
937
937
  }).then(res => res?.data);
@@ -954,7 +954,7 @@ class QQBotAPI {
954
954
  * @returns 返回 Schedule 对象(详见https://bot.q.qq.com/wiki/develop/api-v2/server-inter/channel/content/schedule/model.html#schedule)
955
955
  */
956
956
  async channelsSchedulesPost(channel_id, data) {
957
- return this.request({
957
+ return this.guildServer({
958
958
  method: 'POST',
959
959
  url: `/channels/${channel_id}/schedules`,
960
960
  data
@@ -979,7 +979,7 @@ class QQBotAPI {
979
979
  * @returns 返回 Schedule 对象(详见https://bot.q.qq.com/wiki/develop/api-v2/server-inter/channel/content/schedule/model.html#schedule)
980
980
  */
981
981
  async channelsSchedulesSchedulePatch(channel_id, schedule_id, data) {
982
- return this.request({
982
+ return this.guildServer({
983
983
  method: 'PATCH',
984
984
  url: `/channels/${channel_id}/schedules/${schedule_id}`,
985
985
  data
@@ -992,7 +992,7 @@ class QQBotAPI {
992
992
  * @returns
993
993
  */
994
994
  async channelsSchedulesScheduleDelete(channel_id, schedule_id) {
995
- return this.request({
995
+ return this.guildServer({
996
996
  method: 'DELETE',
997
997
  url: `/channels/${channel_id}/schedules/${schedule_id}`
998
998
  }).then(res => res?.data);
@@ -1011,7 +1011,7 @@ class QQBotAPI {
1011
1011
  * @returns
1012
1012
  */
1013
1013
  async channelsMessagesReactionsPut(channel_id, message_id, type, id) {
1014
- return this.request({
1014
+ return this.guildServer({
1015
1015
  method: 'PUT',
1016
1016
  url: `/channels/${channel_id}/messages/${message_id}/reactions/${type}/${id}`
1017
1017
  }).then(res => res?.data);
@@ -1025,7 +1025,7 @@ class QQBotAPI {
1025
1025
  * @returns
1026
1026
  */
1027
1027
  async channelsMessagesReactionsDelete(channel_id, message_id, type, id) {
1028
- return this.request({
1028
+ return this.guildServer({
1029
1029
  method: 'DELETE',
1030
1030
  url: `/channels/${channel_id}/messages/${message_id}/reactions/${type}/${id}`
1031
1031
  }).then(res => res?.data);
@@ -1042,7 +1042,7 @@ class QQBotAPI {
1042
1042
  * @returns data:{ users:User[], cookie:string,is_end:true|false }
1043
1043
  */
1044
1044
  async channelsMessagesReactionsUsers(channel_id, message_id, type, id, data) {
1045
- return this.request({
1045
+ return this.guildServer({
1046
1046
  method: 'GET',
1047
1047
  url: `/channels/${channel_id}/messages/${message_id}/reactions/${type}/${id}`,
1048
1048
  data
@@ -1063,7 +1063,7 @@ class QQBotAPI {
1063
1063
  * @returns
1064
1064
  */
1065
1065
  async channelsAudioPost(channel_id, data) {
1066
- return this.request({
1066
+ return this.guildServer({
1067
1067
  method: 'POST',
1068
1068
  url: `/channels/${channel_id}/audio`,
1069
1069
  data
@@ -1075,7 +1075,7 @@ class QQBotAPI {
1075
1075
  * @returns {}
1076
1076
  */
1077
1077
  async channelsMicPut(channel_id) {
1078
- return this.request({
1078
+ return this.guildServer({
1079
1079
  method: 'PUT',
1080
1080
  url: `/channels/${channel_id}/mic`
1081
1081
  }).then(res => res?.data);
@@ -1086,7 +1086,7 @@ class QQBotAPI {
1086
1086
  * @returns {}
1087
1087
  */
1088
1088
  async channelsMicDelete(channel_id) {
1089
- return this.request({
1089
+ return this.guildServer({
1090
1090
  method: 'DELETE',
1091
1091
  url: `/channels/${channel_id}/mic`
1092
1092
  }).then(res => res?.data);
@@ -1107,7 +1107,7 @@ class QQBotAPI {
1107
1107
  * @returns is_finish 为 1 时,表示已拉取完 为 0 时,表示未拉取完
1108
1108
  */
1109
1109
  async channelsThreads(channel_id) {
1110
- return this.request({
1110
+ return this.guildServer({
1111
1111
  method: 'GET',
1112
1112
  url: `/channels/${channel_id}/threads`
1113
1113
  }).then(res => res?.data);
@@ -1120,7 +1120,7 @@ class QQBotAPI {
1120
1120
  * 其中content字段可参考 https://bot.q.qq.com/wiki/develop/api-v2/server-inter/channel/content/forum/model.html#RichText
1121
1121
  */
1122
1122
  async channelsThreadsThread(channel_id, thread_id) {
1123
- return this.request({
1123
+ return this.guildServer({
1124
1124
  method: 'GET',
1125
1125
  url: `/channels/${channel_id}/threads/${thread_id}`
1126
1126
  }).then(res => res?.data);
@@ -1134,7 +1134,7 @@ class QQBotAPI {
1134
1134
  * @returns 返回 {task_id:string,create_time:string} 其中 task_id 为帖子id,create_time 发帖时间戳
1135
1135
  */
1136
1136
  async channelsThreadsPut(channel_id, data) {
1137
- return this.request({
1137
+ return this.guildServer({
1138
1138
  method: 'PUT',
1139
1139
  url: `/channels/${channel_id}/threads`,
1140
1140
  data
@@ -1147,7 +1147,7 @@ class QQBotAPI {
1147
1147
  * @returns
1148
1148
  */
1149
1149
  async channelsThreadsDelete(channel_id, thread_id) {
1150
- return this.request({
1150
+ return this.guildServer({
1151
1151
  method: 'DELETE',
1152
1152
  url: `/channels/${channel_id}/threads/${thread_id}`
1153
1153
  }).then(res => res?.data);
@@ -1163,7 +1163,7 @@ class QQBotAPI {
1163
1163
  * @returns
1164
1164
  */
1165
1165
  async guildApiPermission(guild_id) {
1166
- return this.request({
1166
+ return this.guildServer({
1167
1167
  url: `/guilds/${guild_id}/api_permission`
1168
1168
  }).then(res => res?.data);
1169
1169
  }
package/lib/client.js CHANGED
@@ -19,11 +19,16 @@ class QQBotClient extends QQBotAPI {
19
19
  */
20
20
  constructor(opstion) {
21
21
  super();
22
- config.set('secret', opstion.secret);
23
- config.set('appId', opstion.appId);
24
- config.set('token', opstion.token);
25
- config.set('port', opstion.port);
26
- config.set('ws', opstion.ws);
22
+ if (opstion.secret)
23
+ config.set('secret', opstion.secret);
24
+ if (opstion.app_id)
25
+ config.set('app_id', opstion.app_id);
26
+ if (opstion.token)
27
+ config.set('token', opstion.token);
28
+ if (opstion.port)
29
+ config.set('port', opstion.port);
30
+ if (opstion.ws)
31
+ config.set('ws', opstion.ws);
27
32
  }
28
33
  /**
29
34
  * 注册事件处理程序
@@ -37,139 +42,176 @@ class QQBotClient extends QQBotAPI {
37
42
  this.#events[key].push(val);
38
43
  return this;
39
44
  }
45
+ /**
46
+ * 定时鉴权
47
+ * @param cfg
48
+ * @returns
49
+ */
50
+ async #setTimeoutBotConfig() {
51
+ const callBack = async () => {
52
+ const app_id = config.get('app_id');
53
+ if (!app_id)
54
+ return;
55
+ const secret = config.get('secret');
56
+ if (!secret)
57
+ return;
58
+ // 发送请求
59
+ const data = await this.getAuthentication(app_id, secret).then(res => res.data);
60
+ config.set('access_token', data.access_token);
61
+ console.info('refresh', data.expires_in, 's');
62
+ setTimeout(callBack, data.expires_in * 1000);
63
+ };
64
+ await callBack();
65
+ }
40
66
  /**
41
67
  *
42
68
  * @param cfg
43
69
  * @param conversation
44
70
  */
45
- async connect() {
46
- this.#app = new Koa();
47
- this.#app.use(bodyParser());
48
- const router = new Router();
49
- const port = config.get('port');
50
- const cfg = {
51
- secret: config.get('secret'),
52
- port: port ? Number(port) : 17157
53
- };
54
- const ntqqWebhook = new WebhookAPI({
55
- secret: 'YI2mWG0lWH2nYJ4qcOAwiUH4reRE1pdR'
56
- });
57
- this.#app.use(async (ctx, next) => {
58
- let rawData = '';
59
- ctx.req.on('data', chunk => (rawData += chunk));
60
- ctx.req.on('end', () => (ctx.request.rawBody = rawData));
61
- await next();
62
- });
63
- // 启动服务
64
- router.post('/webhook', async (ctx) => {
65
- const sign = ctx.req.headers['x-signature-ed25519'];
66
- const timestamp = ctx.req.headers['x-signature-timestamp'];
67
- const rawBody = ctx.request.rawBody;
68
- const isValid = ntqqWebhook.validSign(timestamp, rawBody, String(sign));
69
- if (!isValid) {
70
- ctx.status = 400;
71
- ctx.body = { msg: 'invalid signature' };
72
- return;
73
- }
74
- const body = ctx.request.body;
75
- if (body.op == 13) {
76
- ctx.status = 200;
77
- ctx.body = {
78
- // 返回明文 token
79
- plain_token: body.d.plain_token,
80
- // 生成签名
81
- signature: ntqqWebhook.getSign(body.d.event_ts, body.d.plain_token)
82
- };
83
- }
84
- else if (body.op == 0) {
85
- ctx.status = 204;
86
- console.log('body', body.d);
87
- // 根据事件类型,处理事件
88
- for (const event of this.#events[body.t] || []) {
89
- event(body.d);
71
+ connect() {
72
+ try {
73
+ this.#setTimeoutBotConfig();
74
+ this.#app = new Koa();
75
+ this.#app.use(bodyParser());
76
+ const router = new Router();
77
+ const port = config.get('port');
78
+ const secret = config.get('secret');
79
+ const cfg = {
80
+ secret: secret ?? '',
81
+ port: port ? Number(port) : 17157
82
+ };
83
+ const ntqqWebhook = new WebhookAPI({
84
+ secret: cfg.secret
85
+ });
86
+ this.#app.use(async (ctx, next) => {
87
+ let rawData = '';
88
+ ctx.req.on('data', chunk => (rawData += chunk));
89
+ ctx.req.on('end', () => (ctx.request.rawBody = rawData));
90
+ await next();
91
+ });
92
+ // 启动服务
93
+ router.post('/webhook', async (ctx) => {
94
+ const sign = ctx.req.headers['x-signature-ed25519'];
95
+ const timestamp = ctx.req.headers['x-signature-timestamp'];
96
+ const rawBody = ctx.request.rawBody;
97
+ const isValid = ntqqWebhook.validSign(timestamp, rawBody, String(sign));
98
+ if (!isValid) {
99
+ ctx.status = 400;
100
+ ctx.body = { msg: 'invalid signature' };
101
+ return;
90
102
  }
91
- // 也可以分法到客户端。 发送失败需要处理 或清理调
92
- for (const client of this.#client) {
93
- try {
94
- client.ws.send(JSON.stringify(body.d));
95
- }
96
- catch (error) {
97
- console.error('send error', error);
98
- }
103
+ const body = ctx.request.body;
104
+ if (body.op == 13) {
105
+ ctx.status = 200;
106
+ ctx.body = {
107
+ // 返回明文 token
108
+ plain_token: body.d.plain_token,
109
+ // 生成签名
110
+ signature: ntqqWebhook.getSign(body.d.event_ts, body.d.plain_token)
111
+ };
99
112
  }
100
- }
101
- });
102
- this.#app.use(router.routes());
103
- this.#app.use(router.allowedMethods());
104
- // 启动服务
105
- const server = this.#app.listen(cfg.port, () => {
106
- console.log('Server running at http://localhost:' + cfg.port + '/webhook');
107
- });
108
- // 创建 WebSocketServer 并监听同一个端口
109
- const wss = new WebSocketServer({ server: server });
110
- // 处理客户端连接
111
- wss.on('connection', ws => {
112
- const clientId = v4();
113
- ws['clientId'] = clientId;
114
- console.log(clientId, 'connection');
115
- this.#client.push({ id: clientId, ws });
116
- // 处理消息事件
117
- ws.on('message', (message) => {
118
- // 拿到消息
119
- try {
120
- const body = JSON.parse(message.toString());
113
+ else if (body.op == 0) {
114
+ ctx.status = 204;
115
+ // 根据事件类型,处理事件
121
116
  for (const event of this.#events[body.t] || []) {
122
- event(body);
117
+ event(body.d);
118
+ }
119
+ // 也可以分法到客户端。 发送失败需要处理 或清理调
120
+ for (const client of this.#client) {
121
+ try {
122
+ client.ws.send(JSON.stringify(body.d));
123
+ }
124
+ catch (e) {
125
+ this.#error(e);
126
+ }
123
127
  }
124
128
  }
125
- catch (e) {
126
- console.error('parse error', e);
127
- }
128
- });
129
- // 处理关闭事件
130
- ws.on('close', () => {
131
- console.log(`Client ${clientId} disconnected`);
132
- this.#client = this.#client.filter(client => client.id !== clientId);
133
129
  });
134
- });
135
- const reconnect = () => {
136
- const ws = config.get('ws');
137
- if (!ws)
138
- return;
139
- // 使用了ws服务器
140
- this.#ws = new WebSocket(ws);
141
- this.#ws.on('open', () => {
142
- this.#count = 0;
143
- console.log('ws connected');
130
+ this.#app.use(router.routes());
131
+ this.#app.use(router.allowedMethods());
132
+ // 启动服务
133
+ const server = this.#app.listen(cfg.port, () => {
134
+ console.log('Server running at http://localhost:' + cfg.port + '/webhook');
144
135
  });
145
- this.#ws.on('message', data => {
146
- try {
136
+ // 创建 WebSocketServer 并监听同一个端口
137
+ const wss = new WebSocketServer({ server: server });
138
+ // 处理客户端连接
139
+ wss.on('connection', ws => {
140
+ const clientId = v4();
141
+ ws['clientId'] = clientId;
142
+ console.log(clientId, 'connection');
143
+ this.#client.push({ id: clientId, ws });
144
+ // 处理消息事件
145
+ ws.on('message', (message) => {
147
146
  // 拿到消息
148
- const body = JSON.parse(data.toString());
149
- for (const event of this.#events[body.t] || []) {
150
- event(body);
147
+ try {
148
+ const body = JSON.parse(message.toString());
149
+ for (const event of this.#events[body.t] || []) {
150
+ event(body);
151
+ }
151
152
  }
152
- }
153
- catch (e) {
154
- console.error('parse error', e);
155
- }
153
+ catch (e) {
154
+ this.#error(e);
155
+ }
156
+ });
157
+ // 处理关闭事件
158
+ ws.on('close', () => {
159
+ console.log(`Client ${clientId} disconnected`);
160
+ this.#client = this.#client.filter(client => client.id !== clientId);
161
+ });
156
162
  });
157
- this.#ws.on('close', () => {
158
- console.log('ws closed');
159
- // 重连5次,超过5次不再重连
160
- if (this.#count > 5) {
163
+ const reconnect = () => {
164
+ const ws = config.get('ws');
165
+ if (!ws)
161
166
  return;
162
- }
163
- // 23s 后重连
164
- setTimeout(() => {
165
- reconnect();
166
- }, 23000);
167
- });
168
- this.#ws.on('error', () => {
169
- console.log('ws error ');
170
- });
171
- };
172
- reconnect();
167
+ // 使用了ws服务器
168
+ this.#ws = new WebSocket(ws);
169
+ this.#ws.on('open', () => {
170
+ this.#count = 0;
171
+ console.log('ws connected');
172
+ });
173
+ this.#ws.on('message', data => {
174
+ try {
175
+ // 拿到消息
176
+ const body = JSON.parse(data.toString());
177
+ for (const event of this.#events[body.t] || []) {
178
+ event(body);
179
+ }
180
+ }
181
+ catch (e) {
182
+ this.#error(e);
183
+ }
184
+ });
185
+ this.#ws.on('close', () => {
186
+ console.log('ws closed');
187
+ // 重连5次,超过5次不再重连
188
+ if (this.#count > 5)
189
+ return;
190
+ // 23s 后重连
191
+ setTimeout(() => {
192
+ reconnect();
193
+ }, 23000);
194
+ });
195
+ this.#ws.on('error', e => {
196
+ this.#error(e);
197
+ });
198
+ };
199
+ reconnect();
200
+ }
201
+ catch (e) {
202
+ this.#error(e);
203
+ }
204
+ }
205
+ /**
206
+ *
207
+ * @param error
208
+ */
209
+ #error(error) {
210
+ if (this.#events['ERROR']) {
211
+ for (const event of this.#events['ERROR'] || []) {
212
+ event(error);
213
+ }
214
+ }
173
215
  }
174
216
  }
175
217
 
package/lib/index.d.ts ADDED
@@ -0,0 +1,6 @@
1
+ import * as alemonjs from 'alemonjs';
2
+
3
+ declare const platform = "qq-bot";
4
+ declare const _default: () => alemonjs.ClientAPI;
5
+
6
+ export { _default as default, platform };
package/lib/index.js CHANGED
@@ -1,17 +1,15 @@
1
- import { defineBot, getConfig, useUserHashKey, OnProcessor } from 'alemonjs';
1
+ import { defineBot, getConfigValue, useUserHashKey, OnProcessor } from 'alemonjs';
2
2
  import { QQBotClient } from './client.js';
3
3
 
4
4
  const platform = 'qq-bot';
5
5
  var index = defineBot(() => {
6
- const cfg = getConfig();
7
- const config = cfg.value['qq-bot'];
8
- if (!config)
9
- return;
6
+ const value = getConfigValue();
7
+ const config = value['qq-bot'];
10
8
  const client = new QQBotClient({
11
- secret: config.secret,
12
- appId: config.appId,
13
- token: config.token,
14
- port: config.port,
9
+ secret: config?.secret,
10
+ app_id: config?.app_id,
11
+ token: config?.token,
12
+ port: config?.port,
15
13
  ws: config?.ws
16
14
  });
17
15
  // 连接
@@ -57,8 +55,7 @@ var index = defineBot(() => {
57
55
  MessageText: event.content?.trim(),
58
56
  OpenId: event.author.member_openid,
59
57
  CreateAt: Date.now(),
60
- //
61
- tag: 'GROUP_AT_MESSAGE_CREATE',
58
+ tag: 'group',
62
59
  value: null
63
60
  };
64
61
  // 当访问的时候获取
@@ -108,7 +105,7 @@ var index = defineBot(() => {
108
105
  CreateAt: Date.now(),
109
106
  OpenId: '',
110
107
  //
111
- tag: 'GROUP_AT_MESSAGE_CREATE',
108
+ tag: 'group',
112
109
  value: null
113
110
  };
114
111
  // 当访问的时候获取
@@ -165,7 +162,7 @@ var index = defineBot(() => {
165
162
  OpenId: event.guild_id,
166
163
  CreateAt: Date.now(),
167
164
  //
168
- tag: 'AT_MESSAGE_CREATE',
165
+ tag: 'guild',
169
166
  //
170
167
  value: null
171
168
  };
@@ -223,7 +220,7 @@ var index = defineBot(() => {
223
220
  OpenId: event.guild_id,
224
221
  CreateAt: Date.now(),
225
222
  //
226
- tag: 'AT_MESSAGE_CREATE',
223
+ tag: 'guild',
227
224
  //
228
225
  value: null
229
226
  };
@@ -307,7 +304,7 @@ var index = defineBot(() => {
307
304
  OpenId: event.guild_id,
308
305
  CreateAt: Date.now(),
309
306
  //
310
- tag: 'AT_MESSAGE_CREATE',
307
+ tag: 'guild',
311
308
  value: null
312
309
  };
313
310
  // 当访问的时候获取
@@ -319,74 +316,145 @@ var index = defineBot(() => {
319
316
  // 处理消息
320
317
  OnProcessor(e, 'message.create');
321
318
  });
319
+ client.on('ERROR', console.error);
322
320
  // FRIEND_ADD
323
321
  global.client = client;
322
+ //
324
323
  return {
325
324
  api: {
326
325
  use: {
327
326
  send: (event, val) => {
328
327
  if (val.length < 0)
329
328
  return Promise.all([]);
330
- const content = val
331
- .filter(item => item.type == 'Link' || item.type == 'Mention' || item.type == 'Text')
332
- .map(item => {
333
- if (item.type == 'Link') {
334
- return `[${item.options?.title ?? item.value}](${item.value})`;
335
- }
336
- else if (item.type == 'Mention') {
337
- if (item.value == 'everyone' ||
338
- item.value == 'all' ||
339
- item.value == '' ||
340
- typeof item.value != 'string') {
341
- return ``;
329
+ // 打 tag
330
+ const tag = event.tag;
331
+ if (tag == 'group') {
332
+ const content = val
333
+ .filter(item => item.type == 'Link' || item.type == 'Mention' || item.type == 'Text')
334
+ .map(item => {
335
+ if (item.type == 'Link') {
336
+ return `[${item.options?.title ?? item.value}](${item.value})`;
342
337
  }
343
- if (item.options?.belong == 'user') {
344
- return `<@${item.value}>`;
338
+ else if (item.type == 'Mention') {
339
+ if (item.value == 'everyone' ||
340
+ item.value == 'all' ||
341
+ item.value == '' ||
342
+ typeof item.value != 'string') {
343
+ return ``;
344
+ }
345
+ if (item.options?.belong == 'user') {
346
+ return `<@${item.value}>`;
347
+ }
348
+ return '';
349
+ // return `<qqbot-at-everyone />`
345
350
  }
346
- return '';
347
- // return `<qqbot-at-everyone />`
351
+ else if (item.type == 'Text') {
352
+ return item.value;
353
+ }
354
+ })
355
+ .join('');
356
+ if (content) {
357
+ return Promise.all([content].map(item => client.groupOpenMessages(event.GuildId, {
358
+ content: item,
359
+ msg_id: event.MessageId,
360
+ msg_type: 0,
361
+ msg_seq: client.getMessageSeq(event.MessageId)
362
+ })));
348
363
  }
349
- else if (item.type == 'Text') {
350
- return item.value;
364
+ const images = val.filter(item => item.type == 'Image').map(item => item.value);
365
+ if (images) {
366
+ return Promise.all(images.map(async (msg) => {
367
+ const file_info = await client
368
+ .postRichMediaByGroup(event.GuildId, {
369
+ file_type: 1,
370
+ file_data: msg.toString('base64')
371
+ })
372
+ .then(res => res?.file_info);
373
+ if (!file_info)
374
+ return Promise.resolve(null);
375
+ return client.groupOpenMessages(event.GuildId, {
376
+ content: '',
377
+ media: {
378
+ file_info
379
+ },
380
+ msg_id: event.MessageId,
381
+ msg_type: 7,
382
+ msg_seq: client.getMessageSeq(event.MessageId)
383
+ });
384
+ }));
351
385
  }
352
- })
353
- .join('');
354
- if (content) {
355
- return Promise.all([content].map(item => client.groupOpenMessages(event.GuildId, {
356
- content: item,
357
- msg_id: event.MessageId,
358
- msg_type: 0,
359
- msg_seq: client.getMessageSeq(event.MessageId)
360
- })));
361
386
  }
362
- const images = val.filter(item => item.type == 'Image').map(item => item.value);
363
- if (images) {
364
- return Promise.all(images.map(async (msg) => {
365
- const file_info = await client
366
- .postRichMediaByGroup(event.GuildId, {
367
- file_type: 1,
368
- file_data: msg.toString('base64')
369
- })
370
- .then(res => res?.file_info);
371
- if (!file_info)
372
- return Promise.resolve(null);
373
- return client.groupOpenMessages(event.GuildId, {
374
- content: '',
375
- media: {
376
- file_info
377
- },
387
+ else {
388
+ const content = val
389
+ .filter(item => item.type == 'Link' || item.type == 'Mention' || item.type == 'Text')
390
+ .map(item => {
391
+ if (item.type == 'Link') {
392
+ return `[${item.options?.title ?? item.value}](${item.value})`;
393
+ }
394
+ else if (item.type == 'Mention') {
395
+ if (item.value == 'everyone' ||
396
+ item.value == 'all' ||
397
+ item.value == '' ||
398
+ typeof item.value != 'string') {
399
+ return `@everyone`;
400
+ }
401
+ if (item.options?.belong == 'user') {
402
+ return `<@!${item.value}>`;
403
+ }
404
+ else if (item.options?.belong == 'channel') {
405
+ return `<#${item.value}>`;
406
+ }
407
+ return '';
408
+ }
409
+ else if (item.type == 'Text') {
410
+ return item.value;
411
+ }
412
+ })
413
+ .join('');
414
+ if (content) {
415
+ return Promise.all([content].map(item => client.channelsMessagesPost(event.ChannelId, {
416
+ content: item,
417
+ msg_id: event.MessageId
418
+ })));
419
+ }
420
+ const images = val.filter(item => item.type == 'Image').map(item => item.value);
421
+ if (images) {
422
+ return Promise.all(images.map(item => client.postImage(event.ChannelId, {
378
423
  msg_id: event.MessageId,
379
- msg_type: 7,
380
- msg_seq: client.getMessageSeq(event.MessageId)
381
- });
382
- }));
424
+ image: item
425
+ })));
426
+ }
383
427
  }
384
428
  return Promise.all([]);
385
429
  },
386
- mention: async () => {
430
+ mention: async (e) => {
431
+ const event = e.value;
432
+ const tag = e.tag;
387
433
  // const event = e.value
388
434
  const Metions = [];
389
- return Metions;
435
+ if (tag == 'group') {
436
+ return Metions;
437
+ }
438
+ if (event.mentions) {
439
+ const mentions = e.value['mentions'];
440
+ // 艾特消息处理
441
+ const MessageMention = mentions.map(item => {
442
+ return {
443
+ UserId: item.id,
444
+ IsMaster: false,
445
+ UserName: item.username,
446
+ IsBot: item.bot,
447
+ UserKey: useUserHashKey({
448
+ Platform: 'qq-guild-bot',
449
+ UserId: item.id
450
+ })
451
+ };
452
+ }) ?? [];
453
+ return MessageMention;
454
+ }
455
+ else {
456
+ return Metions;
457
+ }
390
458
  }
391
459
  }
392
460
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alemonjs/qq-bot",
3
- "version": "0.0.1",
3
+ "version": "0.0.3",
4
4
  "description": "qq-bot",
5
5
  "author": "lemonade",
6
6
  "license": "MIT",