koishi-plugin-bilibili-notify 0.1.3 → 1.0.0-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/authority.js CHANGED
@@ -3,9 +3,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  class Authority {
4
4
  constructor(ctx) {
5
5
  // 授予权限
6
- ctx.permissions.provide('qqguild.admin', async (name, session) => {
7
- return session.event.user.id === '12814193631283946447';
8
- });
6
+ /* ctx.permissions.provide('telegram:admin', async (name, session) => {
7
+ console.log(session);
8
+ return session.telegram?.sender?.role === 'admin'
9
+ }) */
9
10
  }
10
11
  }
11
12
  exports.default = Authority;
package/lib/biliAPI.d.ts CHANGED
@@ -22,7 +22,7 @@ declare class BiliAPI extends Service {
22
22
  getLiveRoomInfo(roomId: string): Promise<any>;
23
23
  getMasterInfo(mid: string): Promise<any>;
24
24
  createNewClient(): void;
25
- getCookies(): Promise<string>;
25
+ getCookies(): string;
26
26
  loadCookiesFromDatabase(): Promise<void>;
27
27
  checkIfTokenNeedRefresh(refreshToken: string, csrf: string, times?: number): Promise<void>;
28
28
  }
package/lib/biliAPI.js CHANGED
@@ -121,7 +121,7 @@ class BiliAPI extends koishi_1.Service {
121
121
  this.jar = new tough_cookie_1.CookieJar();
122
122
  this.client = (0, axios_cookiejar_support_1.wrapper)(axios_1.default.create({ jar: this.jar, headers: { 'Content-Type': 'application/json' } }));
123
123
  }
124
- async getCookies() {
124
+ getCookies() {
125
125
  let cookies;
126
126
  this.jar.store.getAllCookies((err, c) => {
127
127
  if (err)
@@ -194,7 +194,8 @@ class BiliAPI extends koishi_1.Service {
194
194
  const encrypted = new Uint8Array(await crypto.subtle.encrypt({ name: "RSA-OAEP" }, publicKey, data));
195
195
  return encrypted.reduce((str, c) => str + c.toString(16).padStart(2, "0"), "");
196
196
  }
197
- const correspondPath = await getCorrespondPath(Date.now());
197
+ const correspondPath = await getCorrespondPath(data.timestamp);
198
+ // 获取refresh_csrf
198
199
  const { data: refreshCsrfHtml } = await this.client.get(`https://www.bilibili.com/correspond/1/${correspondPath}`);
199
200
  // 创建一个虚拟的DOM元素
200
201
  const { document } = new jsdom_1.JSDOM(refreshCsrfHtml).window;
@@ -212,8 +213,14 @@ class BiliAPI extends koishi_1.Service {
212
213
  switch (refreshData.code) {
213
214
  // 账号未登录
214
215
  case -101: return this.createNewClient();
215
- case -111: throw new Error('csrf 校验失败');
216
- case 86095: throw new Error('refresh_csrf 错误或 refresh_token 与 cookie 不匹配');
216
+ case -111: {
217
+ await this.ctx.database.remove('loginBili', [1]);
218
+ throw new Error('csrf 校验失败');
219
+ }
220
+ case 86095: {
221
+ await this.ctx.database.remove('loginBili', [1]);
222
+ throw new Error('refresh_csrf 错误或 refresh_token 与 cookie 不匹配');
223
+ }
217
224
  }
218
225
  // 更新refresh_token
219
226
  await this.ctx.database.upsert('loginBili', [{
@@ -18,10 +18,11 @@ declare class ComRegister {
18
18
  qqguildBot: Bot<Context>;
19
19
  constructor(ctx: Context, config: ComRegister.Config);
20
20
  dynamicDetect(ctx: Context, bot: Bot<Context>, guildId: string, uid: string): () => Promise<void>;
21
- liveDetect(ctx: Context, bot: Bot<Context>, guildId: string, roomId: string): () => Promise<void>;
21
+ liveDetect(ctx: Context, bot: Bot<Context>, guildId: string, roomId: string): () => Promise<false | void>;
22
22
  checkIfNeedSub(comNeed: boolean, subType: string, session: Session, data?: any): Promise<boolean>;
23
23
  getSubFromDatabase(ctx: Context): Promise<void>;
24
24
  unsubSingle(ctx: Context, id: string, type: number): string;
25
+ checkIfIsLogin(ctx: Context): Promise<boolean>;
25
26
  }
26
27
  declare namespace ComRegister {
27
28
  interface Config {
@@ -30,7 +30,7 @@ class ComRegister {
30
30
  // 拿到QQ频道机器人
31
31
  this.qqguildBot = ctx.bots[ctx.bots.findIndex(bot => bot.platform === 'qqguild')];
32
32
  this.getSubFromDatabase(ctx);
33
- ctx.command('test')
33
+ ctx.command('test', { hidden: true, permissions: ['authority:5'] })
34
34
  .subcommand('.cookies')
35
35
  .usage('测试指令,用于测试从数据库读取cookies')
36
36
  .action(async () => {
@@ -69,7 +69,7 @@ class ComRegister {
69
69
  console.log(num && `Hello World`);
70
70
  });
71
71
  ctx.command('test')
72
- .subcommand('.gimg <uid:string> <index:number>', '生成图片', { permissions: ['qqguild.admin'] })
72
+ .subcommand('.gimg <uid:string> <index:number>')
73
73
  .usage('测试图片生成')
74
74
  .example('test.gimg')
75
75
  .action(async ({ session }, uid, index) => {
@@ -78,13 +78,6 @@ class ComRegister {
78
78
  const [pic] = await ctx.gimg.generateDynamicImg(data.items[index]);
79
79
  return pic;
80
80
  });
81
- ctx.command('test')
82
- .subcommand('.subm')
83
- .usage('查看订阅对象状态')
84
- .example('test subm')
85
- .action(() => {
86
- console.log(this.subManager);
87
- });
88
81
  ctx.command('test')
89
82
  .subcommand('.group')
90
83
  .usage('查看session groupId')
@@ -99,7 +92,7 @@ class ComRegister {
99
92
  .action(({ session }) => {
100
93
  console.log(session);
101
94
  });
102
- ctx.command('bili', 'bili-notify插件相关指令')
95
+ ctx.command('bili', 'bili-notify插件相关指令', { permissions: ['authority:3'] })
103
96
  .subcommand('.login', '登录B站之后才可以进行之后的操作')
104
97
  .usage('使用二维码登录,登录B站之后才可以进行之后的操作')
105
98
  .example('bili login')
@@ -161,13 +154,20 @@ class ComRegister {
161
154
  bili_cookies: encryptedCookies,
162
155
  bili_refresh_token: encryptedRefreshToken
163
156
  }]);
157
+ // 销毁定时器
164
158
  dispose();
165
- return await session.send('登录成功!');
159
+ // 发送成功登录推送
160
+ await session.send('登录成功!');
161
+ // 订阅之前的订阅
162
+ await this.getSubFromDatabase(ctx);
163
+ // 调用bili show指令
164
+ await session.execute('bili show');
165
+ return;
166
166
  }
167
167
  }, 1000);
168
168
  });
169
169
  ctx.command('bili')
170
- .subcommand('.unsub <uid:string> 取消订阅')
170
+ .subcommand('.unsub <uid:string>', '取消订阅UP主动态、直播或全部')
171
171
  .usage('取消订阅,加-l为取消直播订阅,加-d为取消动态订阅,什么都不加则为全部取消')
172
172
  .option('live', '-l')
173
173
  .option('dynamic', '-d')
@@ -212,7 +212,7 @@ class ComRegister {
212
212
  !exist && session.send('未订阅该用户,无需取消订阅');
213
213
  });
214
214
  ctx.command('bili')
215
- .subcommand('.show')
215
+ .subcommand('.show', '展示订阅对象')
216
216
  .usage('展示订阅对象')
217
217
  .example('bili show')
218
218
  .action(async ({ session }) => {
@@ -224,13 +224,18 @@ class ComRegister {
224
224
  table && session.send(table);
225
225
  });
226
226
  ctx.command('bili')
227
- .subcommand('.sub <mid:string> [guildId:string]')
227
+ .subcommand('.sub <mid:string> [guildId:string]', '订阅用户动态和直播通知')
228
228
  .option('live', '-l')
229
229
  .option('dynamic', '-d')
230
- .usage('订阅用户动态和直播通知,若需要订阅直播请加上-l,需要订阅动态则加上-d。若没有加任何参数,之后会向你单独询问,<>中为必选参数,[]中为可选参数,目标群号若不填,则默认为当前群聊')
230
+ .usage('订阅用户动态和直播通知,若需要订阅直播请加上-l,需要订阅动态则加上-d。若没有加任何参数,之后会向你单独询问,尖括号中为必选参数,中括号为可选参数,目标群号若不填,则默认为当前群聊')
231
231
  .example('bili sub 用户uid 目标QQ群号(暂不支持) -l -d')
232
232
  .action(async ({ session, options }, mid, guildId) => {
233
233
  this.logger.info('调用bili.sub指令');
234
+ // 检查是否登录
235
+ if (!(await this.checkIfIsLogin(ctx))) {
236
+ // 未登录直接返回
237
+ return '请使用指令bili login登录后再进行订阅操作';
238
+ }
234
239
  // 如果订阅人数超过三个则直接返回
235
240
  if (this.num >= 3)
236
241
  return '目前最多只能订阅三个人';
@@ -354,7 +359,7 @@ class ComRegister {
354
359
  }
355
360
  });
356
361
  ctx.command('bili')
357
- .subcommand('.dynamic <uid:string> <guildId:string>')
362
+ .subcommand('.dynamic <uid:string> <guildId:string>', '订阅用户动态推送', { hidden: true })
358
363
  .option('bot', '-b <type:string>')
359
364
  .usage('订阅用户动态推送')
360
365
  .example('bili dynamic 1')
@@ -391,7 +396,7 @@ class ComRegister {
391
396
  this.subManager[index].dynamicDispose = dispose;
392
397
  });
393
398
  ctx.command('bili')
394
- .subcommand('.live <roomId:string> <guildId:string>')
399
+ .subcommand('.live <roomId:string> <guildId:string>', '订阅主播开播通知', { hidden: true })
395
400
  .option('bot', '-b <type:string>')
396
401
  .usage('订阅主播开播通知')
397
402
  .example('bili live 732')
@@ -426,7 +431,7 @@ class ComRegister {
426
431
  this.subManager[index].liveDispose = dispose;
427
432
  });
428
433
  ctx.command('bili')
429
- .subcommand('.status <roomId:string>')
434
+ .subcommand('.status <roomId:string>', '查询主播当前直播状态', { hidden: true })
430
435
  .usage('查询主播当前直播状态')
431
436
  .example('bili status 732')
432
437
  .action(async ({ session }, roomId) => {
@@ -539,7 +544,7 @@ class ComRegister {
539
544
  try {
540
545
  // 如果flag为false则说明前面的代码还未执行完,则直接返回
541
546
  if (!flag)
542
- return;
547
+ return flag = false;
543
548
  // 发送请求检测直播状态
544
549
  let content;
545
550
  try {
@@ -679,6 +684,9 @@ class ComRegister {
679
684
  }
680
685
  }
681
686
  async getSubFromDatabase(ctx) {
687
+ if (!(await this.checkIfIsLogin(ctx))) { // 如果未登录,则直接返回
688
+ return;
689
+ }
682
690
  // 从数据库中获取数据
683
691
  const subData = await ctx.database.get('bilibili', { id: { $gt: 0 } });
684
692
  // 设定订阅数量
@@ -784,6 +792,15 @@ class ComRegister {
784
792
  }
785
793
  }
786
794
  }
795
+ async checkIfIsLogin(ctx) {
796
+ if ((await ctx.database.get('loginBili', 1)).length !== 0) { // 数据库中有数据
797
+ // 检查cookie中是否有值
798
+ if (ctx.biliAPI.getCookies() !== '[]') { // 有值说明已登录
799
+ return true;
800
+ }
801
+ }
802
+ return false;
803
+ }
787
804
  }
788
805
  (function (ComRegister) {
789
806
  ComRegister.Config = koishi_1.Schema.object({
package/lib/index.js CHANGED
@@ -31,7 +31,7 @@ const koishi_1 = require("koishi");
31
31
  // import crypto
32
32
  const crypto_1 = __importDefault(require("crypto"));
33
33
  // import plugins
34
- const authority_1 = __importDefault(require("./authority"));
34
+ // import Authority from './authority'
35
35
  const comRegister_1 = __importDefault(require("./comRegister"));
36
36
  const Database = __importStar(require("./database"));
37
37
  // import Service
@@ -69,7 +69,7 @@ function apply(ctx, config) {
69
69
  ctx.plugin(generateImg_1.default, { cardColorStart: config.cardColorStart, cardColorEnd: config.cardColorEnd });
70
70
  ctx.plugin(biliAPI_1.default);
71
71
  // load plugin
72
- ctx.plugin(authority_1.default);
72
+ // ctx.plugin(Authority)
73
73
  ctx.plugin(comRegister_1.default, { pushTime: config.pushTime });
74
74
  // 当用户输入“恶魔兔,启动!”时,执行 help 指令
75
75
  ctx.middleware((session, next) => {
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": "0.1.3",
4
+ "version": "1.0.0-alpha.0",
5
5
  "contributors": [
6
6
  "Akokko <admin@akokko.com>"
7
7
  ],
@@ -12,6 +12,10 @@
12
12
  "dist"
13
13
  ],
14
14
  "license": "MIT",
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "git+https://github.com/Akokk0/koishi-plugin-bilibili-notify"
18
+ },
15
19
  "scripts": {},
16
20
  "keywords": [
17
21
  "chatbot",