koishi-plugin-bilibili-notify 1.0.6-alpha.0 → 1.0.8

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.
@@ -1,9 +1,11 @@
1
1
  import { Bot, Context, Logger, Schema, Session } from "koishi";
2
+ import { Notifier } from "@koishijs/plugin-notifier";
2
3
  declare class ComRegister {
3
4
  static inject: string[];
4
5
  logger: Logger;
5
6
  config: ComRegister.Config;
6
7
  num: number;
8
+ subNotifier: Notifier;
7
9
  subManager: {
8
10
  id: number;
9
11
  uid: string;
@@ -19,8 +21,10 @@ declare class ComRegister {
19
21
  constructor(ctx: Context, config: ComRegister.Config);
20
22
  dynamicDetect(ctx: Context, bot: Bot<Context>, guildId: string, uid: string): () => Promise<void | string[]>;
21
23
  liveDetect(ctx: Context, bot: Bot<Context>, guildId: string, roomId: string): () => Promise<void>;
24
+ subShow(): string;
22
25
  checkIfNeedSub(comNeed: boolean, subType: string, session: Session, data?: any): Promise<boolean>;
23
- getSubFromDatabase(ctx: Context): Promise<void>;
26
+ updateSubNotifier(ctx: Context): void;
27
+ getSubFromDatabase(ctx: Context): Promise<string[]>;
24
28
  unsubSingle(ctx: Context, id: string, type: number): string;
25
29
  checkIfIsLogin(ctx: Context): Promise<boolean>;
26
30
  }
@@ -17,6 +17,7 @@ class ComRegister {
17
17
  logger;
18
18
  config;
19
19
  num = 0;
20
+ subNotifier;
20
21
  subManager = [];
21
22
  // QQ群机器人
22
23
  qqBot;
@@ -175,13 +176,13 @@ class ComRegister {
175
176
  }]);
176
177
  // 销毁定时器
177
178
  dispose();
179
+ // 订阅之前的订阅
180
+ await this.getSubFromDatabase(ctx);
178
181
  // 清除控制台通知
179
182
  ctx.biliAPI.disposeNotifier();
180
183
  // 发送成功登录推送
181
184
  await session.send('登录成功!');
182
- // 订阅之前的订阅
183
- await this.getSubFromDatabase(ctx);
184
- // 调用bili show指令
185
+ // bili show
185
186
  await session.execute('bili show');
186
187
  return;
187
188
  }
@@ -198,6 +199,9 @@ class ComRegister {
198
199
  // 若用户UID为空则直接返回
199
200
  if (!uid)
200
201
  return '用户UID不能为空';
202
+ // -d -l两个选项不能同时存在
203
+ if (options.dynamic && options.live)
204
+ return '需要取消订阅该UP主请直接使用指令bili unsub 用户UID';
201
205
  // 定义是否存在
202
206
  let exist;
203
207
  await Promise.all(this.subManager.map(async (sub, i) => {
@@ -220,11 +224,13 @@ class ComRegister {
220
224
  // 从数据库中删除订阅
221
225
  await ctx.database.remove('bilibili', { uid: this.subManager[i].uid });
222
226
  // 将该订阅对象从订阅管理对象中移除
223
- this.subManager = this.subManager.splice(i, i);
227
+ this.subManager.splice(i, 1);
224
228
  // id--
225
229
  this.num--;
226
230
  // 发送成功通知
227
231
  session.send('已取消订阅该用户');
232
+ // 更新控制台提示
233
+ this.updateSubNotifier(ctx);
228
234
  // 将存在flag设置为true
229
235
  exist = true;
230
236
  }
@@ -236,13 +242,9 @@ class ComRegister {
236
242
  .subcommand('.show', '展示订阅对象')
237
243
  .usage('展示订阅对象')
238
244
  .example('bili show')
239
- .action(async ({ session }) => {
240
- let table = ``;
241
- this.subManager.forEach(sub => {
242
- table += `UID:${sub.uid} ${sub.dynamic ? '已订阅动态' : ''} ${sub.live ? '已订阅直播' : ''}` + '\n';
243
- });
244
- !table && session.send('没有订阅任何UP');
245
- table && session.send(table);
245
+ .action(() => {
246
+ const subTable = this.subShow();
247
+ return subTable;
246
248
  });
247
249
  ctx.command('bili')
248
250
  .subcommand('.sub <mid:string> [guildId:string]', '订阅用户动态和直播通知')
@@ -382,6 +384,15 @@ class ComRegister {
382
384
  // 发送订阅消息通知
383
385
  await bot.sendMessage(sub.targetId, `订阅${userData.info.uname}动态通知`);
384
386
  }
387
+ // 新增订阅展示到控制台
388
+ // 获取subTable
389
+ const subTable = this.subShow();
390
+ // 判断之前是否存在Notifier
391
+ this.subNotifier && this.subNotifier.dispose();
392
+ this.subNotifier = ctx.notifier.create({
393
+ type: 'primary',
394
+ content: subTable
395
+ });
385
396
  });
386
397
  ctx.command('bili')
387
398
  .subcommand('.dynamic <uid:string> <guildId:string>', '订阅用户动态推送', { hidden: true })
@@ -503,7 +514,7 @@ class ComRegister {
503
514
  let firstSubscription = true;
504
515
  let timePoint;
505
516
  // Test code
506
- let timer = 0;
517
+ // let timer = 0
507
518
  return async () => {
508
519
  // Test code
509
520
  /* this.logger.info('timer:' + timer++)
@@ -748,6 +759,14 @@ class ComRegister {
748
759
  }
749
760
  };
750
761
  }
762
+ subShow() {
763
+ // 在控制台中显示订阅对象
764
+ let table = ``;
765
+ this.subManager.forEach(sub => {
766
+ table += `UID:${sub.uid} ${sub.dynamic ? '已订阅动态' : ''} ${sub.live ? '已订阅直播' : ''}` + '\n';
767
+ });
768
+ return table ? table : '没有订阅任何UP';
769
+ }
751
770
  async checkIfNeedSub(comNeed, subType, session, data) {
752
771
  if (comNeed) {
753
772
  if (subType === '直播' && !data.live_room) {
@@ -783,16 +802,30 @@ class ComRegister {
783
802
  }
784
803
  }
785
804
  }
805
+ updateSubNotifier(ctx) {
806
+ // 更新控制台提示
807
+ this.subNotifier && this.subNotifier.dispose();
808
+ // 获取subTable
809
+ const subTable = this.subShow();
810
+ // 设置更新后的提示
811
+ this.subNotifier = ctx.notifier.create({
812
+ type: 'primary',
813
+ content: subTable
814
+ });
815
+ }
786
816
  async getSubFromDatabase(ctx) {
787
- if (!(await this.checkIfIsLogin(ctx))) { // 如果未登录,则直接返回
817
+ // 如果未登录,则直接返回
818
+ if (!(await this.checkIfIsLogin(ctx)))
819
+ return;
820
+ // 已存在订阅管理对象,不再进行订阅操作
821
+ if (this.subManager.length !== 0)
788
822
  return;
789
- }
790
823
  // 从数据库中获取数据
791
824
  const subData = await ctx.database.get('bilibili', { id: { $gt: 0 } });
792
825
  // 设定订阅数量
793
826
  this.num = subData.length;
794
827
  // 如果订阅数量超过三个则被非法修改数据库
795
- // 向管理员发送重新订阅通知
828
+ // 在控制台提示重新订阅
796
829
  if (this.num > 3) {
797
830
  ctx.notifier.create({
798
831
  type: 'danger',
@@ -803,7 +836,14 @@ class ComRegister {
803
836
  // 定义Bot
804
837
  let bot;
805
838
  // 循环遍历
806
- subData.forEach(async (sub) => {
839
+ for (const sub of subData) {
840
+ // 判断是否存在没有任何订阅的数据
841
+ if (!sub.dynamic && !sub.live) { // 存在未订阅任何项目的数据
842
+ // 删除该条数据
843
+ ctx.database.remove('bilibili', { id: sub.id });
844
+ // 跳过下面的步骤
845
+ continue;
846
+ }
807
847
  // 拿到对应bot
808
848
  switch (sub.platform) {
809
849
  case 'qq': bot = this.qqBot;
@@ -880,67 +920,72 @@ class ComRegister {
880
920
  }
881
921
  // 保存新订阅对象
882
922
  this.subManager.push(subManagerItem);
883
- // 发送订阅成功通知
884
- });
923
+ }
924
+ // 在控制台中显示订阅对象
925
+ this.updateSubNotifier(ctx);
885
926
  }
886
927
  unsubSingle(ctx, id /* UID或RoomId */, type /* 0取消Live订阅,1取消Dynamic订阅 */) {
887
928
  let index;
888
- switch (type) {
889
- case 0: { // 取消Live订阅
890
- index = this.subManager.findIndex(sub => sub.roomId === id);
891
- if (index === -1)
892
- return '未订阅该用户,无需取消订阅';
893
- // 取消订阅
894
- this.subManager[index].live && this.subManager[index].liveDispose();
895
- // 如果没有对这个UP的任何订阅,则移除
896
- if (!this.subManager[index].dynamic) {
897
- // 获取要删除行的id
898
- const id = this.subManager[index].id;
899
- // 从管理对象中移除
900
- this.subManager = this.subManager.splice(index, index);
901
- // 从数据库中删除
902
- ctx.database.remove('bilibili', [id]);
903
- // num--
904
- this.num--;
905
- return '已取消订阅该用户';
929
+ const checkIfNoSubExist = (index) => {
930
+ if (!this.subManager[index].dynamic && !this.subManager[index].live) {
931
+ // 获取要删除行的id
932
+ const id = this.subManager[index].id;
933
+ // 从管理对象中移除
934
+ this.subManager.splice(index, 1);
935
+ // 从数据库中删除
936
+ ctx.database.remove('bilibili', [id]);
937
+ // num--
938
+ this.num--;
939
+ return '已取消订阅该用户';
940
+ }
941
+ return null;
942
+ };
943
+ try {
944
+ switch (type) {
945
+ case 0: { // 取消Live订阅
946
+ index = this.subManager.findIndex(sub => sub.roomId === id);
947
+ if (index === -1)
948
+ return '未订阅该用户,无需取消订阅';
949
+ // 取消订阅
950
+ this.subManager[index].live && this.subManager[index].liveDispose();
951
+ this.subManager[index].liveDispose = null;
952
+ this.subManager[index].live = false;
953
+ // 如果没有对这个UP的任何订阅,则移除
954
+ const info = checkIfNoSubExist(index);
955
+ if (info)
956
+ return info;
957
+ // 更新数据库
958
+ ctx.database.upsert('bilibili', [{
959
+ id: +`${this.subManager[index].id}`,
960
+ live: 0
961
+ }]);
962
+ return '已取消订阅Live';
906
963
  }
907
- this.subManager[index].liveDispose = null;
908
- this.subManager[index].live = false;
909
- // 更新数据库
910
- ctx.database.upsert('bilibili', [{
911
- id: +`${this.subManager[index].id}`,
912
- live: 0
913
- }]);
914
- return '已取消订阅Live';
915
- }
916
- case 1: { // 取消Dynamic订阅
917
- index = this.subManager.findIndex(sub => sub.uid === id);
918
- if (index === -1)
919
- return '未订阅该用户,无需取消订阅';
920
- // 取消订阅
921
- this.subManager[index].dynamic && this.subManager[index].dynamicDispose();
922
- // 如果没有对这个UP的任何订阅,则移除
923
- if (!this.subManager[index].live) {
924
- // 获取要删除行的id
925
- const id = this.subManager[index].id;
926
- // 从管理对象中移除
927
- this.subManager = this.subManager.splice(index, index);
928
- // 从数据库中删除
929
- ctx.database.remove('bilibili', [id]);
930
- // num--
931
- this.num--;
932
- return '已取消订阅该用户';
964
+ case 1: { // 取消Dynamic订阅
965
+ index = this.subManager.findIndex(sub => sub.uid === id);
966
+ if (index === -1)
967
+ return '未订阅该用户,无需取消订阅';
968
+ // 取消订阅
969
+ this.subManager[index].dynamic && this.subManager[index].dynamicDispose();
970
+ this.subManager[index].dynamicDispose = null;
971
+ this.subManager[index].dynamic = false;
972
+ // 如果没有对这个UP的任何订阅,则移除
973
+ const info = checkIfNoSubExist(index);
974
+ if (info)
975
+ return info;
976
+ // 更新数据库
977
+ ctx.database.upsert('bilibili', [{
978
+ id: +`${this.subManager[index].id}`,
979
+ dynamic: 0
980
+ }]);
981
+ return '已取消订阅Dynamic';
933
982
  }
934
- this.subManager[index].dynamicDispose = null;
935
- this.subManager[index].dynamic = false;
936
- // 更新数据库
937
- ctx.database.upsert('bilibili', [{
938
- id: +`${this.subManager[index].id}`,
939
- dynamic: 0
940
- }]);
941
- return '已取消订阅Dynamic';
942
983
  }
943
984
  }
985
+ finally {
986
+ // 执行完该方法后,保证执行一次updateSubNotifier()
987
+ this.updateSubNotifier(ctx);
988
+ }
944
989
  }
945
990
  async checkIfIsLogin(ctx) {
946
991
  if ((await ctx.database.get('loginBili', 1)).length !== 0) { // 数据库中有数据
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-bilibili-notify",
3
3
  "description": "Koishi bilibili notify plugin",
4
- "version": "1.0.6-alpha.0",
4
+ "version": "1.0.8",
5
5
  "contributors": [
6
6
  "Akokko <admin@akokko.com>"
7
7
  ],
package/readme.md CHANGED
@@ -81,7 +81,9 @@
81
81
  - ver 1.0.3 修复了一些bug,提供用户自己选择推送卡片字体样式的选项
82
82
  - ver 1.0.4 修复了重复推送的bug,提供用户选择推送卡片渲染方式的选项
83
83
  - ver 1.0.5 修复了用户非法篡改数据库内容可能导致程序异常运行的bug,修复了UP主开播动态推送空白卡片的bug
84
- - ver 1.0.6 修复了转发动态转发信息出现undefined的bug,优化了动态推送的逻辑
84
+ - ver 1.0.6 修复了转发动态转发信息出现undefined的bug,修复了再次登录订阅显示错误的bug,优化了动态推送的逻辑
85
+ - ver 1.0.7 修复了在已登录情况下,再次登录会导致重复订阅和提示用户未订阅任何UP主的提示(实际上已订阅)的bug,新增了订阅对象在控制台的显示,优化了bili show指令的逻辑
86
+ - ver 1.0.8 修复了取消订阅的bug
85
87
 
86
88
  ## 感谢
87
89