koishi-plugin-cocoyyy-console 1.0.4 → 1.0.6

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/index.js CHANGED
@@ -344,6 +344,16 @@ var tagFuncMenuList = [
344
344
  description: "标签取消别名",
345
345
  command: "unbind [标签] [别名]"
346
346
  },
347
+ {
348
+ name: "addstart",
349
+ description: "开始给标签批量添加图片",
350
+ command: "addstart [标签]"
351
+ },
352
+ {
353
+ name: "addend",
354
+ description: "结束给标签批量添加图片",
355
+ command: "addend"
356
+ },
347
357
  {
348
358
  name: "taglist",
349
359
  description: "获取标签列表",
@@ -469,6 +479,46 @@ async function getImgCountByTag(tag_id) {
469
479
  return count;
470
480
  }
471
481
  __name(getImgCountByTag, "getImgCountByTag");
482
+ async function startBatchAddImg(ctx, tag_name, session, savePath2) {
483
+ try {
484
+ const guild_id = session.guildId;
485
+ let tag = await getTag(tag_name, guild_id);
486
+ if (tag == null) {
487
+ throw new Error(`not found tag '${tag_name}'!`);
488
+ }
489
+ if (tag.get("status") !== 0) {
490
+ logger.info(`[startBatchAddImg Info]:tag '${tag_name}' is deleted`);
491
+ throw new Error(`tag '${tag_name}' is deleted!`);
492
+ }
493
+ if (savePath2 == null) {
494
+ throw new Error("saving path config wrong!");
495
+ }
496
+ if (uid_tag_map.has(session.userId)) {
497
+ throw new Error("already started");
498
+ }
499
+ let key = session.guildId + "_" + session.userId;
500
+ uid_tag_map.set(key, tag_name);
501
+ return { result: true, error: null };
502
+ } catch (e) {
503
+ logger.error("[startBatchAddImg Error]: " + e?.message || String(e));
504
+ return { result: false, error: e?.message || String(e) };
505
+ }
506
+ }
507
+ __name(startBatchAddImg, "startBatchAddImg");
508
+ async function endBatchAddImg(ctx, session) {
509
+ try {
510
+ let key = session.guildId + "_" + session.userId;
511
+ if (!uid_tag_map.has(key)) {
512
+ throw new Error("not start recording");
513
+ }
514
+ uid_tag_map.delete(key);
515
+ return { result: true, error: null };
516
+ } catch (e) {
517
+ logger.error("[startBatchAddImg Error]: " + e?.message || String(e));
518
+ return { result: false, error: e?.message || String(e) };
519
+ }
520
+ }
521
+ __name(endBatchAddImg, "endBatchAddImg");
472
522
 
473
523
  // src/services/tag_func/tag_service.ts
474
524
  async function findTagByName(tag_name, guild_id) {
@@ -618,6 +668,7 @@ __name(getTagList, "getTagList");
618
668
 
619
669
  // src/services/tag_func/tag_commands.ts
620
670
  var import_url = require("url");
671
+ var uid_tag_map = /* @__PURE__ */ new Map();
621
672
  function registerTagCommands(ctx, connected, savePath2) {
622
673
  ctx.command("maketag <参数>", "创建一个标签").check(({ session }) => {
623
674
  if (!connected) return;
@@ -678,16 +729,48 @@ function registerTagCommands(ctx, connected, savePath2) {
678
729
  return `取消绑定失败:${exec.error}`;
679
730
  return `取消绑定成功`;
680
731
  });
681
- ctx.command("taglist", "获取标签列表").check(({ session }) => {
732
+ ctx.command("addstart <参数>", "开始给标签批量添加图片").check(({ session }) => {
682
733
  if (!connected) return;
683
734
  if (!is_at_bot(session)) return;
684
735
  }).action(async ({ session }, ...args) => {
736
+ const tag = args?.[0];
737
+ if (!tag) return "请提供正确格式,如:@bot addstart [标签]";
738
+ let exec = await startBatchAddImg(ctx, tag, session, savePath2);
739
+ if (!exec.result)
740
+ return exec.error;
741
+ return "[开始记录图片]";
742
+ });
743
+ ctx.command("addend", "结束给标签批量添加图片").check(({ session }) => {
744
+ if (!connected) return;
745
+ if (!is_at_bot(session)) return;
746
+ }).action(async ({ session }) => {
747
+ let exec = await endBatchAddImg(ctx, session);
748
+ if (!exec.result)
749
+ return exec.error;
750
+ return "[停止记录图片]";
751
+ });
752
+ ctx.command("taglist", "获取标签列表").check(({ session }) => {
753
+ if (!connected) return;
754
+ if (!is_at_bot(session)) return;
755
+ }).action(async ({ session }) => {
685
756
  let exec = await getTagList(session.guildId);
686
757
  if (exec == null)
687
758
  return `查询失败`;
688
759
  return `查询成功!
689
760
  ${exec.map((item) => "标签:" + item.tag + " 黑历史数:" + item.count + "\n别名:" + (item.alias ? item.alias : "无")).join("\n\n")}`;
690
761
  });
762
+ ctx.middleware(async (session, next) => {
763
+ let key = session.guildId + "_" + session.userId;
764
+ if (!uid_tag_map.has(key)) return await next();
765
+ const tag = uid_tag_map.get(key);
766
+ const img = session.content;
767
+ const srcMatch = img.match(/src="([^"]+)"/);
768
+ if (!srcMatch) return "请给出图片,如需停止记录图片,请输入@bot addend [标签]";
769
+ let exec = await saveImg(ctx, tag, session.guildId, img, savePath2);
770
+ if (!exec.result)
771
+ return `保存失败:${exec.error}`;
772
+ return "保存成功";
773
+ });
691
774
  }
692
775
  __name(registerTagCommands, "registerTagCommands");
693
776
 
@@ -1,8 +1,16 @@
1
- import { Context } from "koishi";
1
+ import { Context, Session } from "koishi";
2
2
  declare function saveImg(ctx: Context, tag_name: string, guild_id: string, imgurl: string, savePath: string): Promise<{
3
3
  result: boolean;
4
4
  error: string | null;
5
5
  }>;
6
6
  declare function randomImgByTag(tag_name: string, guild_id: string): Promise<string | null>;
7
7
  declare function getImgCountByTag(tag_id: number): Promise<number>;
8
- export { saveImg, randomImgByTag, getImgCountByTag };
8
+ declare function startBatchAddImg(ctx: Context, tag_name: string, session: Session, savePath: string): Promise<{
9
+ result: boolean;
10
+ error: string | null;
11
+ }>;
12
+ declare function endBatchAddImg(ctx: Context, session: Session): Promise<{
13
+ result: boolean;
14
+ error: string | null;
15
+ }>;
16
+ export { saveImg, randomImgByTag, getImgCountByTag, startBatchAddImg, endBatchAddImg };
@@ -1,3 +1,4 @@
1
1
  import { Context } from 'koishi';
2
+ declare let uid_tag_map: Map<string, string>;
2
3
  declare function registerTagCommands(ctx: Context, connected: boolean, savePath: string | null): void;
3
- export { registerTagCommands };
4
+ export { uid_tag_map, registerTagCommands };
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-cocoyyy-console",
3
3
  "description": "for self using, function console",
4
- "version": "1.0.4",
4
+ "version": "1.0.6",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "contributors": [