koishi-plugin-cocoyyy-console 1.0.0 → 1.0.2

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
@@ -225,68 +225,118 @@ function getImgsModel() {
225
225
  }
226
226
  __name(getImgsModel, "getImgsModel");
227
227
 
228
- // src/utils/common.ts
229
- function is_at_bot(session) {
230
- const elements = Array.isArray(session?.elements) ? session.elements : [];
231
- const atElement = elements.find((element) => element?.type === "at");
232
- const atId = atElement?.attrs?.id;
233
- const selfId = session?.bot?.selfId ?? session?.selfId;
234
- if (atId == null || selfId == null) return false;
235
- return String(atId) === String(selfId);
236
- }
237
- __name(is_at_bot, "is_at_bot");
238
-
239
228
  // src/services/tag_service.ts
240
229
  var import_sequelize4 = require("sequelize");
241
- var import_fs = require("fs");
230
+ var import_fs2 = require("fs");
231
+ var import_path2 = require("path");
232
+
233
+ // src/services/img_service.ts
234
+ var import_koishi2 = require("koishi");
242
235
  var import_path = require("path");
243
- async function findTagByName(name2) {
236
+ var import_fs = require("fs");
237
+ async function saveImg(tag_name, img, savePath2) {
238
+ try {
239
+ const Img = getImgsModel();
240
+ logger.info("[saveImg Info]: " + img);
241
+ let tag = await getTag(tag_name);
242
+ if (tag == null) {
243
+ throw new Error(`not found tag '${tag_name}'!`);
244
+ }
245
+ if (tag.get("status") !== 0) {
246
+ logger.info(`[saveImg Info]:tag '${tag_name}' is deleted`);
247
+ throw new Error(`tag '${tag_name}' is deleted!`);
248
+ }
249
+ if (savePath2 == null) {
250
+ throw new Error("saving path config wrong!");
251
+ }
252
+ let dir = (0, import_path.join)(savePath2, String(tag.get("id")));
253
+ const now = new Date(Date.now() + 8 * 60 * 60 * 1e3);
254
+ const filetag_name = now.getFullYear().toString() + String(now.getMonth() + 1).padStart(2, "0") + String(now.getDate()).padStart(2, "0") + String(now.getHours()).padStart(2, "0") + String(now.getMinutes()).padStart(2, "0") + String(now.getSeconds()).padStart(2, "0") + ".txt";
255
+ let filePath = (0, import_path.join)(dir, filetag_name);
256
+ await import_fs.promises.writeFile(filePath, img);
257
+ const saveImg2 = await Img.create({ tag_id: tag.get("id"), img_url: filePath, createtime: now });
258
+ logger.info("[saveImg Info]: save image to '" + tag_name + "' success");
259
+ return { result: true, error: null };
260
+ } catch (e) {
261
+ logger.error("[saveImg Error]: " + e?.message || String(e));
262
+ return { result: false, error: e?.message || String(e) };
263
+ }
264
+ }
265
+ __name(saveImg, "saveImg");
266
+ async function randomImgByTag(tag_name) {
267
+ try {
268
+ const Img = getImgsModel();
269
+ const existed = await getTag(tag_name);
270
+ if (!existed) {
271
+ throw new Error(`not found tag '${tag_name}'!`);
272
+ }
273
+ if (existed.status == 0) {
274
+ throw new Error(`not found tag '${tag_name}'!`);
275
+ }
276
+ let ImgList = await Img.findAll({
277
+ where: {
278
+ status: 0,
279
+ tag_id: existed.get("id")
280
+ }
281
+ });
282
+ if (ImgList.length <= 0) {
283
+ throw new Error("Don't have image!");
284
+ }
285
+ const random = new import_koishi2.Random(() => Math.random());
286
+ let result = ImgList[random.int(0, ImgList.length)];
287
+ const filePath = result.get("img_url");
288
+ const fileContent = await import_fs.promises.readFile(filePath, { encoding: "utf-8" });
289
+ return fileContent;
290
+ } catch (e) {
291
+ logger.error("[randomImgByTag Error]: " + e?.message || String(e));
292
+ return null;
293
+ }
294
+ }
295
+ __name(randomImgByTag, "randomImgByTag");
296
+ async function getImgCountByTag(tag_id) {
297
+ const Img = getImgsModel();
298
+ let count = await Img.count({
299
+ where: { status: 0, tag_id }
300
+ });
301
+ return count;
302
+ }
303
+ __name(getImgCountByTag, "getImgCountByTag");
304
+
305
+ // src/services/tag_service.ts
306
+ async function findTagByName(tag_name) {
244
307
  const Tag = getTagsModel();
245
308
  return await Tag.findOne({
246
309
  where: {
247
310
  status: 0,
248
311
  [import_sequelize4.Op.or]: [
249
- { tags: name2 },
250
- { alias: { [import_sequelize4.Op.like]: `%${name2}%` } }
312
+ { tags: tag_name },
313
+ { alias: { [import_sequelize4.Op.like]: `%${tag_name}%` } }
251
314
  ]
252
315
  }
253
316
  });
254
317
  }
255
318
  __name(findTagByName, "findTagByName");
256
- async function findDeletedTagByName(name2) {
319
+ async function findDeletedTagByName(tag_name) {
257
320
  const Tag = getTagsModel();
258
321
  return await Tag.findOne({
259
322
  where: {
260
323
  status: 1,
261
324
  [import_sequelize4.Op.or]: [
262
- { tags: name2 },
263
- { alias: { [import_sequelize4.Op.like]: `%${name2}%` } }
325
+ { tags: tag_name },
326
+ { alias: { [import_sequelize4.Op.like]: `%${tag_name}%` } }
264
327
  ]
265
328
  }
266
329
  });
267
330
  }
268
331
  __name(findDeletedTagByName, "findDeletedTagByName");
269
- async function createorUpdateTag(name2, alias, savePath2) {
332
+ async function createorUpdateTag(tag_name, savePath2) {
270
333
  try {
271
334
  const Tag = getTagsModel();
272
- let existed = await findTagByName(name2);
335
+ let existed = await findTagByName(tag_name);
273
336
  if (existed) {
274
- if (alias == null) {
275
- throw new Error("already existed!");
276
- }
277
- let now_alias = existed.get("alias");
278
- if (now_alias != null) {
279
- const list = now_alias.split(";");
280
- if (!list.includes(alias)) list.push(alias);
281
- now_alias = list.join(";");
282
- } else {
283
- now_alias = alias;
284
- }
285
- existed.alias = now_alias;
286
- await existed.save();
287
- return { result: true, error: null };
337
+ throw new Error("already existed!");
288
338
  }
289
- existed = await findDeletedTagByName(name2);
339
+ existed = await findDeletedTagByName(tag_name);
290
340
  if (existed) {
291
341
  if (existed.get("status") != 0) {
292
342
  existed.set("status", 0);
@@ -299,9 +349,9 @@ async function createorUpdateTag(name2, alias, savePath2) {
299
349
  throw new Error("saving path config wrong!");
300
350
  }
301
351
  const now = new Date(Date.now() + 8 * 60 * 60 * 1e3);
302
- const tag = await Tag.create({ tags: name2, alias: alias ?? null, createtime: now });
303
- const dir = (0, import_path.join)(savePath2, String(tag.id));
304
- (0, import_fs.mkdirSync)(dir, { recursive: true });
352
+ const tag = await Tag.create({ tags: tag_name, alias: null, createtime: now });
353
+ const dir = (0, import_path2.join)(savePath2, String(tag.id));
354
+ (0, import_fs2.mkdirSync)(dir, { recursive: true });
305
355
  return { result: true, error: null };
306
356
  } catch (e) {
307
357
  logger.error("[createorUpdateTag Error]: " + e?.message || String(e));
@@ -309,9 +359,9 @@ async function createorUpdateTag(name2, alias, savePath2) {
309
359
  }
310
360
  }
311
361
  __name(createorUpdateTag, "createorUpdateTag");
312
- async function getTag(name2) {
362
+ async function getTag(tag_name) {
313
363
  try {
314
- let tag = await findTagByName(name2);
364
+ let tag = await findTagByName(tag_name);
315
365
  return tag;
316
366
  } catch (e) {
317
367
  logger.error("[getTag Error]: " + e?.message || String(e));
@@ -319,70 +369,81 @@ async function getTag(name2) {
319
369
  }
320
370
  }
321
371
  __name(getTag, "getTag");
322
-
323
- // src/services/img_service.ts
324
- var import_koishi2 = require("koishi");
325
- var import_path2 = require("path");
326
- var import_fs2 = require("fs");
327
- async function saveImg(name2, img, savePath2) {
372
+ async function bindAlias(tag_name, alias) {
328
373
  try {
329
- const Img = getImgsModel();
330
- logger.info("[saveImg Info]: " + img);
331
- let tag = await getTag(name2);
332
- if (tag == null) {
333
- throw new Error(`not found tag '${name2}'!`);
334
- }
335
- if (tag.get("status") !== 0) {
336
- logger.info(`[saveImg Info]:tag '${name2}' is deleted`);
337
- throw new Error(`tag '${name2}' is deleted!`);
374
+ return await checkAlias(tag_name, alias, false);
375
+ } catch (e) {
376
+ logger.error("[bindAlias Error]: " + e?.message || String(e));
377
+ return { result: false, error: e?.message || String(e) };
378
+ }
379
+ }
380
+ __name(bindAlias, "bindAlias");
381
+ async function unbindAlias(tag_name, alias) {
382
+ try {
383
+ return await checkAlias(tag_name, alias, true);
384
+ } catch (e) {
385
+ logger.error("[unbindAlias Error]: " + e?.message || String(e));
386
+ return { result: false, error: e?.message || String(e) };
387
+ }
388
+ }
389
+ __name(unbindAlias, "unbindAlias");
390
+ async function checkAlias(tag_name, alias, isDelete = false) {
391
+ try {
392
+ let existed = await findTagByName(tag_name);
393
+ if (existed == null) {
394
+ throw new Error(`not found tag '${tag_name}'`);
338
395
  }
339
- if (savePath2 == null) {
340
- throw new Error("saving path config wrong!");
396
+ let now_alias = existed.get("alias");
397
+ let new_list;
398
+ if (!isDelete) {
399
+ if (now_alias != null) {
400
+ const list = now_alias.split(";");
401
+ if (!list.includes(alias)) list.push(alias);
402
+ now_alias = list.join(";");
403
+ } else {
404
+ now_alias = alias;
405
+ }
406
+ } else {
407
+ if (now_alias != null) {
408
+ const list = now_alias.split(";");
409
+ if (list.includes(alias))
410
+ new_list = list.filter((tar) => tar !== alias);
411
+ now_alias = new_list.join(";");
412
+ } else {
413
+ throw new Error(`not found alia '${alias}'`);
414
+ }
341
415
  }
342
- let dir = (0, import_path2.join)(savePath2, String(tag.get("id")));
343
- const now = new Date(Date.now() + 8 * 60 * 60 * 1e3);
344
- const filename = now.getFullYear().toString() + String(now.getMonth() + 1).padStart(2, "0") + String(now.getDate()).padStart(2, "0") + String(now.getHours()).padStart(2, "0") + String(now.getMinutes()).padStart(2, "0") + String(now.getSeconds()).padStart(2, "0") + ".txt";
345
- let filePath = (0, import_path2.join)(dir, filename);
346
- await import_fs2.promises.writeFile(filePath, img);
347
- const saveImg2 = await Img.create({ tag_id: tag.get("id"), img_url: filePath, createtime: now });
348
- logger.info("[saveImg Info]: save image to '" + name2 + "' success");
416
+ existed.set("alias", now_alias);
417
+ await existed.save();
349
418
  return { result: true, error: null };
350
419
  } catch (e) {
351
- logger.error("[saveImg Error]: " + e?.message || String(e));
420
+ logger.error("[checkAlias Error]: " + e?.message || String(e));
352
421
  return { result: false, error: e?.message || String(e) };
353
422
  }
354
423
  }
355
- __name(saveImg, "saveImg");
356
- async function randomImgByTag(name2, savePath2) {
424
+ __name(checkAlias, "checkAlias");
425
+ async function getTagList() {
357
426
  try {
358
- const Img = getImgsModel();
359
- const existed = await getTag(name2);
360
- if (!existed) {
361
- throw new Error(`not found tag '${name2}'!`);
362
- }
363
- if (existed.status == 0) {
364
- throw new Error(`not found tag '${name2}'!`);
365
- }
366
- let ImgList = await Img.findAll({
367
- where: {
368
- status: 0,
369
- tag_id: existed.get("id")
370
- }
427
+ const Tag = getTagsModel();
428
+ let resultList = await Tag.findAll({
429
+ where: { status: 0 }
371
430
  });
372
- if (ImgList.length <= 0) {
373
- throw new Error("Don't have image!");
374
- }
375
- const random = new import_koishi2.Random(() => Math.random());
376
- let result = ImgList[random.int(0, ImgList.length)];
377
- const filePath = result.get("img_url");
378
- const fileContent = await import_fs2.promises.readFile(filePath, { encoding: "utf-8" });
379
- return fileContent;
431
+ let tagList = [];
432
+ resultList.forEach(async (element) => {
433
+ let count = await getImgCountByTag(element.get("id"));
434
+ tagList.push({
435
+ tag: element.get("tags"),
436
+ alias: element.get("alias"),
437
+ count
438
+ });
439
+ });
440
+ return tagList;
380
441
  } catch (e) {
381
- logger.error("[randomImgByTag Error]: " + e?.message || String(e));
442
+ logger.error("[getTagList Error]: " + e?.message || String(e));
382
443
  return null;
383
444
  }
384
445
  }
385
- __name(randomImgByTag, "randomImgByTag");
446
+ __name(getTagList, "getTagList");
386
447
 
387
448
  // src/index.ts
388
449
  var name = "cocoyyy-console";
@@ -405,6 +466,21 @@ var menuList = [
405
466
  name: "get",
406
467
  description: "给标签添加图片",
407
468
  command: "get [标签]"
469
+ },
470
+ {
471
+ name: "bind",
472
+ description: "标签绑定别名",
473
+ command: "bind [标签] [别名]"
474
+ },
475
+ {
476
+ name: "unbind",
477
+ description: "标签取消别名",
478
+ command: "unbind [标签] [别名]"
479
+ },
480
+ {
481
+ name: "taglist",
482
+ description: "获取标签列表",
483
+ command: "taglist"
408
484
  }
409
485
  ];
410
486
  var logger = new import_koishi3.Logger(name);
@@ -418,28 +494,19 @@ async function apply(ctx, config) {
418
494
  await getImgsModel().sync();
419
495
  }
420
496
  savePath = resolveTagBaseDir(config?.save);
421
- ctx.command("fullhelp", "帮助菜单").check(({ session }) => {
422
- if (!connected) return;
423
- if (!is_at_bot(session)) return;
424
- }).action(async ({ session }) => {
497
+ ctx.command("fullhelp", "帮助菜单").action(async ({ session }) => {
425
498
  return `帮助菜单[所有命令都需要@bot]:
426
499
  ${menuList.map((item) => item.command + ": " + item.description).join("\n ")}`;
427
500
  });
428
- ctx.command("maketag <参数>", "创建一个标签").check(({ session }) => {
429
- if (!connected) return;
430
- if (!is_at_bot(session)) return;
431
- }).action(async ({ session }, ...args) => {
501
+ ctx.command("maketag <参数>", "创建一个标签").action(async ({ session }, ...args) => {
432
502
  const tag = args?.[0];
433
503
  if (!tag) return "请提供正确格式,如:@bot maketag [标签]";
434
- let exec = await createorUpdateTag(tag, null, savePath);
504
+ let exec = await createorUpdateTag(tag, savePath);
435
505
  if (!exec.result)
436
506
  return `创建标签失败:${exec.error}`;
437
507
  return `已创建标签:${tag}`;
438
508
  });
439
- ctx.command("add <参数>", "给标签添加图片").check(({ session }) => {
440
- if (!connected) return;
441
- if (!is_at_bot(session)) return;
442
- }).action(async ({ session }, ...args) => {
509
+ ctx.command("add <参数>", "给标签添加图片").action(async ({ session }, ...args) => {
443
510
  const tag = args?.[0];
444
511
  const img = args?.[1];
445
512
  if (!tag || !img) return "请提供正确格式,如:@bot add [标签] [图片]";
@@ -448,17 +515,40 @@ async function apply(ctx, config) {
448
515
  return `保存图片失败:${exec.error}`;
449
516
  return "保存图片成功";
450
517
  });
451
- ctx.command("get <参数>", "获取标签对应图片").check(({ session }) => {
452
- if (!connected) return;
453
- if (!is_at_bot(session)) return;
454
- }).action(async ({ session }, ...args) => {
518
+ ctx.command("get <参数>", "获取标签对应图片").action(async ({ session }, ...args) => {
455
519
  const tag = args?.[0];
456
520
  if (!tag) return "请提供正确格式,如:@bot get [标签]";
457
- let exec = await randomImgByTag(tag, savePath);
521
+ let exec = await randomImgByTag(tag);
458
522
  if (!exec)
459
523
  return `获取图片失败`;
460
524
  return exec;
461
525
  });
526
+ ctx.command("bind <参数>", "标签绑定别名").action(async ({ session }, ...args) => {
527
+ const tag = args?.[0];
528
+ const alia = args?.[1];
529
+ if (!tag || !alia) return "请提供正确格式,如:@bot bind [标签] [别名]";
530
+ let exec = await bindAlias(tag, alia);
531
+ if (!exec.result)
532
+ return `绑定别名失败:${exec.error}`;
533
+ return `绑定别名成功`;
534
+ });
535
+ ctx.command("unbind <参数>", "标签取消别名").action(async ({ session }, ...args) => {
536
+ const tag = args?.[0];
537
+ const alia = args?.[1];
538
+ if (!tag || !alia) return "请提供正确格式,如:@bot unbind [标签] [别名]";
539
+ let exec = await unbindAlias(tag, alia);
540
+ if (!exec.result)
541
+ return `取消绑定失败:${exec.error}`;
542
+ return `取消绑定成功`;
543
+ });
544
+ ctx.command("taglist", "获取标签列表").action(async ({ session }, ...args) => {
545
+ let exec = await getTagList();
546
+ if (exec == null)
547
+ return `查询失败`;
548
+ return `查询成功!
549
+
550
+ ${exec.map((item) => "标签:" + item.tag + " 黑历史数:" + item.count + "\n别名:" + item.alias).join("\n")}`;
551
+ });
462
552
  }
463
553
  __name(apply, "apply");
464
554
  // Annotate the CommonJS export names for ESM import in node:
@@ -1,6 +1,7 @@
1
- declare function saveImg(name: string, img: string, savePath: string): Promise<{
1
+ declare function saveImg(tag_name: string, img: string, savePath: string): Promise<{
2
2
  result: boolean;
3
3
  error: string | null;
4
4
  }>;
5
- declare function randomImgByTag(name: string, savePath: string): Promise<string | null>;
6
- export { saveImg, randomImgByTag };
5
+ declare function randomImgByTag(tag_name: string): Promise<string | null>;
6
+ declare function getImgCountByTag(tag_id: number): Promise<number>;
7
+ export { saveImg, randomImgByTag, getImgCountByTag };
@@ -1,7 +1,20 @@
1
1
  import { TagsModel } from '../models/tags';
2
- declare function createorUpdateTag(name: string, alias?: string | null, savePath?: string | null): Promise<{
2
+ declare function createorUpdateTag(tag_name: string, savePath?: string | null): Promise<{
3
3
  result: boolean;
4
4
  error: string | null;
5
5
  }>;
6
- declare function getTag(name: string): Promise<TagsModel | null>;
7
- export { createorUpdateTag, getTag };
6
+ declare function getTag(tag_name: string): Promise<TagsModel | null>;
7
+ declare function bindAlias(tag_name: string, alias: string): Promise<{
8
+ result: boolean;
9
+ error: string | null;
10
+ }>;
11
+ declare function unbindAlias(tag_name: string, alias: string): Promise<{
12
+ result: boolean;
13
+ error: string | null;
14
+ }>;
15
+ declare function getTagList(): Promise<{
16
+ tag: string;
17
+ alias: string;
18
+ count: number;
19
+ }[]>;
20
+ export { createorUpdateTag, getTag, bindAlias, unbindAlias, getTagList };
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "koishi-plugin-cocoyyy-console",
3
3
  "description": "for self using, function console",
4
- "version": "1.0.0",
4
+ "version": "1.0.2",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "contributors": [
8
- "黑椰Coco"
8
+ "黑椰Coco <yyycoconut1130@gmail.com>"
9
9
  ],
10
10
  "files": [
11
11
  "lib",