@azimiao/koishi-plugin-cafe-bot-exp 0.0.12 → 0.0.16

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.
@@ -7,6 +7,7 @@ export interface CafeBotDrawConfig {
7
7
  forceUpdateDataWhenLoad: boolean;
8
8
  ImageServer: string;
9
9
  ImageServerAuth: string;
10
+ ImageNoCache: boolean;
10
11
  MinCount: number;
11
12
  MaxCount: number;
12
13
  Star1Name: string;
package/lib/index.js CHANGED
@@ -37,6 +37,9 @@ __export(src_exports, {
37
37
  });
38
38
  module.exports = __toCommonJS(src_exports);
39
39
 
40
+ // package.json
41
+ var version = "0.0.16";
42
+
40
43
  // src/draw/index.ts
41
44
  var draw_exports = {};
42
45
  __export(draw_exports, {
@@ -260,6 +263,7 @@ var CafeBotDrawConfig = import_koishi.Schema.object({
260
263
  forceUpdateDataWhenLoad: import_koishi.Schema.boolean().description("插件加载时强制刷新数据").default(false),
261
264
  ImageServer: import_koishi.Schema.string().description("图像生成服务器").default("http://127.0.0.1:3000/screenshot"),
262
265
  ImageServerAuth: import_koishi.Schema.string().description("图像生成服务器密码").default(""),
266
+ ImageNoCache: import_koishi.Schema.boolean().description("每次都重新生成图片(一般仅用于Debug)").default(false),
263
267
  MinCount: import_koishi.Schema.number().description("最小抽取次数").default(0),
264
268
  MaxCount: import_koishi.Schema.number().description("最大抽取次数").default(8),
265
269
  Star1Name: import_koishi.Schema.string().description("1星名称").default("N"),
@@ -307,11 +311,12 @@ var downloadTool_default = downloadFileIfNotExist;
307
311
 
308
312
  // src/draw/index.ts
309
313
  var name = "cafe-bot-exp.draw";
310
- var inject = ["http"];
314
+ var inject = ["http", "logger"];
311
315
  var Config = CafeBotDrawConfig;
312
316
  var animals = ["瑟蕾奴", "可鲁贝洛斯", "捷欧", "基库", "蔡特", "柯贝", "古利亚诺斯", "小星", "影良", "fio", "xeros"];
313
317
  var starEnum = [5, 4, 3, 2, 1];
314
318
  var cafebotCardData = [];
319
+ var logger = null;
315
320
  function shuffleWithCustomRandom(array, rand) {
316
321
  for (let i = array.length - 1; i > 0; i--) {
317
322
  const a = rand();
@@ -395,16 +400,17 @@ async function getCards(seed, ctx, config) {
395
400
  }
396
401
  __name(getCards, "getCards");
397
402
  async function apply(ctx, config) {
403
+ logger = ctx.logger(name);
398
404
  await downloadCardDataIfNotExist(ctx, config, config.forceUpdateDataWhenLoad);
399
405
  ctx.command("轨迹抽卡", `抽取你的每日轨迹人物卡吧~`);
400
406
  ctx.command("轨迹抽卡/给我抽", "进行每日抽卡").action(async (argv, _) => {
401
407
  let seed = DailySeededName(argv.session.userId);
402
- console.log(`getcard for ${seed}`);
408
+ logger?.info(`getcard for ${seed}`);
403
409
  let randomer = new PseudoRandom_default(seed);
404
410
  let result = await getCards(seed, ctx, config);
405
411
  if (result.length <= 0) {
406
412
  let msg2 = "你并不是本群限定非酋, 只不过抽到的卡牌被" + animals[randomer.nextInt(0, animals.length - 1)] + "叼走了~\n";
407
- console.log(`getcard for ${seed} is empty,send it`);
413
+ logger?.info(`getcard for ${seed} is empty,send it`);
408
414
  await argv.session?.send(`${At(argv)}${msg2}`);
409
415
  return;
410
416
  }
@@ -428,36 +434,37 @@ async function apply(ctx, config) {
428
434
  }
429
435
  });
430
436
  await argv.session?.send(msg);
431
- console.log(`getcard for ${seed} send ok`);
437
+ logger?.info(`getcard for ${seed} send ok`);
432
438
  return;
433
439
  });
434
440
  const requestWebCache = {};
435
441
  ctx.command("轨迹抽卡/给我抽图", "进行抽卡并显示卡牌图片").action(async (argv, _) => {
436
442
  let seed = DailySeededName(argv.session.userId);
437
- console.log(`getcard img for ${seed}`);
443
+ logger?.info(`getcard img for ${seed}`);
438
444
  let randomer = new PseudoRandom_default(seed);
439
445
  let result = await getCards(seed, ctx, config);
440
446
  if (result.length <= 0) {
441
447
  let msg = "你并不是本群限定非酋, 只不过抽到的卡牌被" + animals[randomer.nextInt(0, animals.length - 1)] + "叼走了~\n";
442
448
  await argv.session?.send(`${At(argv)} ${msg}`);
443
- console.log(`getcard img for ${seed} is empty`);
449
+ logger?.info(`getcard img for ${seed} is empty`);
444
450
  return;
445
451
  }
446
452
  if (requestWebCache[seed]) {
447
453
  await argv.session?.send(`${At(argv)} 请等待上次抽取完成哦~`);
448
- console.log(`getcard img for ${seed}, another request is processing, send please wait`);
454
+ logger?.info(`getcard img for ${seed}, another request is processing, send please wait`);
449
455
  return;
450
456
  }
451
457
  requestWebCache[seed] = "waiting";
452
458
  await argv.session?.sendQueued(`${At(argv)} 抽取中,请稍候...`);
453
- console.log(`getcard img for ${seed} is processing, send please wait`);
459
+ logger?.info(`getcard img for ${seed} is processing, send please wait`);
454
460
  var a = html_default(result);
455
461
  await ctx.http.post(
456
462
  config.ImageServer,
457
463
  {
458
464
  html: a,
459
465
  auth: config.ImageServerAuth,
460
- filename: seed
466
+ filename: seed,
467
+ nocache: config.ImageNoCache
461
468
  },
462
469
  {
463
470
  responseType: "json"
@@ -466,13 +473,13 @@ async function apply(ctx, config) {
466
473
  delete requestWebCache[seed];
467
474
  await argv.session?.sendQueued(`${At(argv)}<img src="${res.data}"/>`);
468
475
  await argv.session?.cancelQueued();
469
- console.log(`getcard img for ${seed} send ok~`);
476
+ logger?.info(`getcard img for ${seed} send ok~`);
470
477
  }).catch(async (e) => {
471
- console.log(e);
478
+ logger?.error(`getcard img for ${seed} is failed, send create failed`);
479
+ logger?.error(e);
472
480
  delete requestWebCache[seed];
473
- await argv.session?.sendQueued(`${At(argv)}图片生成失败`);
481
+ await argv.session?.sendQueued(`${At(argv)}超级计算机『卡佩尔』发生核心故障😵(A...D...)`);
474
482
  await argv.session?.cancelQueued();
475
- console.log(`getcard img for ${seed} is failed, send create failed`);
476
483
  });
477
484
  return;
478
485
  });
@@ -592,7 +599,7 @@ var CafeTimeTools_default = CafeTimeTools;
592
599
 
593
600
  // src/quiz/index.ts
594
601
  var name3 = "cafe-bot-exp.quiz";
595
- var inject2 = ["http", "cache"];
602
+ var inject2 = ["http", "cache", "logger"];
596
603
  var Config3 = CafeBotQuizConfig;
597
604
  var validOptions = {
598
605
  "A": 0,
@@ -608,6 +615,7 @@ var validOptions = {
608
615
  "3": 2,
609
616
  "4": 3
610
617
  };
618
+ var logger2 = null;
611
619
  var quizDataIds = [];
612
620
  var quizDataSet = {};
613
621
  async function downloadQuitDataIfNotExist(ctx, config, forceUpdate) {
@@ -627,6 +635,8 @@ async function downloadQuitDataIfNotExist(ctx, config, forceUpdate) {
627
635
  quizDataSet[element.question.id] = element;
628
636
  });
629
637
  } catch (error) {
638
+ logger2?.error("get quiz data failed");
639
+ logger2?.error(error);
630
640
  throw new Error("quiz read error");
631
641
  }
632
642
  return;
@@ -643,28 +653,52 @@ async function answerHandler(ctx, config, argv, answer) {
643
653
  if (answer in validOptions) {
644
654
  selectNumber = validOptions[answer];
645
655
  } else {
646
- await argv.session?.send(`${At(argv)}选项无效,请重新输入~`);
656
+ await argv.session?.send(`${At(argv)}输入选项无效哦,请重新输入~`);
647
657
  return;
648
658
  }
649
659
  await ctx.cache.delete("question", argv.session.userId);
650
660
  if (quizDataIds == null || quizDataIds.length <= 0) {
651
- await argv.session?.send(`${At(argv)}超级计算机『卡佩尔』发生核心故障😵`);
661
+ await argv.session?.send(`${At(argv)}超级计算机『卡佩尔』发生核心故障😵(导力网络异常波动)`);
652
662
  return;
653
663
  }
664
+ const userQuiz = await ctx.database.get("cafeQuiz", { userId: argv.session.userId });
665
+ let userQuizA = null;
666
+ if (userQuiz.length === 0) {
667
+ userQuizA = await ctx.database.create("cafeQuiz", {
668
+ userId: argv.session.userId,
669
+ right: 0,
670
+ wrong: 0
671
+ });
672
+ } else {
673
+ userQuizA = userQuiz[0];
674
+ }
654
675
  var qItem = quizDataSet[lastQuestion.question];
655
- console.log(`${argv.session.userId} try answer quiz(id:${lastQuestion.question}), input answer:${selectNumber}`);
676
+ logger2?.info(`${argv.session.userId} try answer quiz(id:${lastQuestion.question}): right answer:${lastQuestion.answer}, input answer:${selectNumber}`);
656
677
  if (qItem && lastQuestion.answer == selectNumber) {
657
- await argv.session?.send(`${At(argv)}回答正确😊${qItem.explain.length > 0 ? "," + qItem.explain : ""}`);
678
+ await argv.session?.send(`${At(argv)}回答正确😊${qItem.explain.length > 0 ? "" + qItem.explain : ""}`);
679
+ await ctx.database.set("cafeQuiz", userQuizA.id, {
680
+ right: userQuizA.right + 1
681
+ });
658
682
  return;
659
683
  } else {
660
- await argv.session?.send(`${At(argv)}回答错误😟${qItem.explain2.length > 0 ? "," + qItem.explain2 : ""}`);
684
+ await argv.session?.send(`${At(argv)}回答错误😟${qItem.explain2.length > 0 ? "" + qItem.explain2 : ""}`);
685
+ await ctx.database.set("cafeQuiz", userQuizA.id, {
686
+ wrong: userQuizA.wrong + 1
687
+ });
661
688
  return;
662
689
  }
663
690
  }
664
691
  __name(answerHandler, "answerHandler");
665
692
  async function apply3(ctx, config) {
693
+ logger2 = ctx.logger(name3);
694
+ ctx.model.extend("cafeQuiz", {
695
+ id: "unsigned",
696
+ userId: "string",
697
+ right: "integer",
698
+ wrong: "integer"
699
+ }, { primary: "id", autoInc: true });
666
700
  await downloadQuitDataIfNotExist(ctx, config, config.forceUpdateWhenLoad);
667
- ctx.command("轨迹答题/出题").action(async (argv, _) => {
701
+ ctx.command("轨迹答题/出题", "随机抽一道题目").action(async (argv, _) => {
668
702
  await downloadQuitDataIfNotExist(ctx, config, false);
669
703
  var lastQuestion = await ctx.cache.get("question", argv.session.userId);
670
704
  if (lastQuestion) {
@@ -672,8 +706,9 @@ async function apply3(ctx, config) {
672
706
  return;
673
707
  }
674
708
  var dailyNameKey = DailySeededName(argv.session.userId);
709
+ argv.session.user;
675
710
  if (quizDataIds == null || quizDataIds.length <= 0) {
676
- await argv.session?.send(`${At(argv)}超级计算机『卡佩尔』发生核心故障😵`);
711
+ await argv.session?.send(`${At(argv)}超级计算机『卡佩尔』发生核心故障😵(导力网络异常波动)`);
677
712
  return;
678
713
  }
679
714
  var todayCount = await ctx.cache.get("todayCache", dailyNameKey);
@@ -686,12 +721,13 @@ async function apply3(ctx, config) {
686
721
  }
687
722
  todayCount++;
688
723
  var cacheTimeoutTime = CafeTimeTools_default.getRemainingSecondsToBeijingMidnight();
724
+ logger2?.debug(`try set cache ${dailyNameKey}, ${todayCount}`);
689
725
  await ctx.cache.set("todayCache", dailyNameKey, todayCount, cacheTimeoutTime * 1e3);
690
726
  var randomId = import_koishi6.Random.pick(quizDataIds);
691
727
  var qItem = quizDataSet[randomId];
692
728
  var qOptions = import_koishi6.Random.shuffle(qItem.options);
693
729
  var answerIndex = -1;
694
- console.log(`${argv.session.userId} get a new quiz(id:${randomId})`);
730
+ logger2?.info(`${argv.session.userId} get a new quiz(id:${randomId})`);
695
731
  for (var i = 0; i < qOptions.length; i++) {
696
732
  if (qOptions[i].oid === qItem.a) {
697
733
  answerIndex = i;
@@ -708,7 +744,7 @@ D. ${qOptions[3].s}`;
708
744
  return;
709
745
  });
710
746
  ctx.command("轨迹答题").usage("通过趣味答题看看你是不是合格的桂皮吧~");
711
- ctx.command("轨迹答题/回答 <answer:text>", `使用"回答+空格+选项"回答问题哦,如"回答 A"`).action(async (argv, answer) => {
747
+ ctx.command("轨迹答题/回答 <answer:text>", `使用"回答+空格+选项"回答问题,另外你也可以直接@我说出选项(ABCD)哦`).action(async (argv, answer) => {
712
748
  await answerHandler(ctx, config, argv, answer);
713
749
  return;
714
750
  });
@@ -728,9 +764,19 @@ D. ${qOptions[3].s}`;
728
764
  await answerHandler(ctx, config, argv, "D");
729
765
  return;
730
766
  });
731
- ctx.command("轨迹答题/答题分数").action(async (argv, _) => {
732
- await argv.session?.send(`${At(argv)}正在开发中哦~`);
733
- return;
767
+ ctx.command("轨迹答题/答题分", "查看答题总分数").action(async (argv, _) => {
768
+ const userQuiz = await ctx.database.get("cafeQuiz", { userId: argv.session.userId });
769
+ if (userQuiz.length === 0) {
770
+ await argv.session?.send(`${At(argv)}你还没有答过题哦,快来答题吧~`);
771
+ return;
772
+ } else {
773
+ let userQuizA = userQuiz[0];
774
+ let total = userQuizA.right + userQuizA.wrong;
775
+ await argv.session?.send(`${At(argv)}答题总数: ${total}
776
+ 正确回答: ${userQuizA.right}
777
+ 正确率: ${Math.round(userQuizA.right * 1 / total * 100)}`);
778
+ return;
779
+ }
734
780
  });
735
781
  }
736
782
  __name(apply3, "apply");
@@ -745,13 +791,18 @@ var Config4 = import_koishi7.Schema.intersect([
745
791
 
746
792
  // src/index.ts
747
793
  var name4 = "cafe-bot-exp";
748
- var inject3 = ["http", "cache"];
794
+ var inject3 = ["http", "cache", "logger", "database"];
749
795
  async function apply4(ctx, config) {
750
796
  ctx.plugin(draw_exports, config);
751
797
  ctx.plugin(quiz_exports, config);
752
798
  ctx.plugin(cat_exports, config);
753
799
  ctx.command("关于", "about").action(async (argv, _) => {
754
- argv.session?.send(`我是轨迹Cafe群聊机器人概念版~`);
800
+ argv.session?.send(
801
+ `我是运行在超级计算机『卡佩尔』上的轨迹CafeBot概念版v${version}🎉~
802
+ - 我提供轨迹抽卡🎴和轨迹问答🙋‍功能
803
+ - 另外我还有一些隐藏功能待你探索哦🎁
804
+ 多多与我聊天吧~`
805
+ );
755
806
  });
756
807
  }
757
808
  __name(apply4, "apply");
@@ -18,5 +18,16 @@ declare module '@koishijs/cache' {
18
18
  todayCache: number;
19
19
  }
20
20
  }
21
+ declare module 'koishi' {
22
+ interface Tables {
23
+ 'cafeQuiz': CafeQuiz;
24
+ }
25
+ }
26
+ export interface CafeQuiz {
27
+ id: number;
28
+ userId: string;
29
+ right: number;
30
+ wrong: number;
31
+ }
21
32
  export declare function apply(ctx: Context, config: Config): Promise<void>;
22
33
  export {};
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@azimiao/koishi-plugin-cafe-bot-exp",
3
3
  "description": "cafe bot experiment",
4
- "version": "0.0.12",
4
+ "version": "0.0.16",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "repository": {