koishi-plugin-checkgal 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/api.d.ts CHANGED
@@ -7,4 +7,5 @@ export declare class TouchGalAPI {
7
7
  constructor(ctx: Context);
8
8
  searchGame(keyword: string, config: Config): Promise<GameInfo[]>;
9
9
  getDownloads(patchId: number): Promise<DownloadResource[]>;
10
+ downloadAndConvertImage(url: string): Promise<Buffer | null>;
10
11
  }
package/lib/api.js CHANGED
@@ -1,6 +1,10 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
6
  exports.TouchGalAPI = void 0;
7
+ const sharp_1 = __importDefault(require("sharp"));
4
8
  class TouchGalAPI {
5
9
  constructor(ctx) {
6
10
  this.http = ctx.http;
@@ -58,5 +62,22 @@ class TouchGalAPI {
58
62
  return [];
59
63
  }
60
64
  }
65
+ async downloadAndConvertImage(url) {
66
+ if (!url)
67
+ return null;
68
+ try {
69
+ const response = await this.http.get(url, {
70
+ responseType: 'arraybuffer',
71
+ });
72
+ // 类型守卫,确保 response 是 ArrayBuffer
73
+ if (!(response instanceof ArrayBuffer))
74
+ return null;
75
+ return (0, sharp_1.default)(Buffer.from(response)).jpeg().toBuffer();
76
+ }
77
+ catch (error) {
78
+ this.logger.error(`Failed to download or convert image from ${url}:`, error);
79
+ return null;
80
+ }
81
+ }
61
82
  }
62
83
  exports.TouchGalAPI = TouchGalAPI;
package/lib/commands.d.ts CHANGED
@@ -8,4 +8,5 @@ declare module 'koishi' {
8
8
  gameCache: GameCache;
9
9
  }
10
10
  }
11
+ export declare const inject: string[];
11
12
  export declare function apply(ctx: Context, config: Config): void;
package/lib/commands.js CHANGED
@@ -1,7 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.inject = void 0;
3
4
  exports.apply = apply;
4
5
  const koishi_1 = require("koishi");
6
+ exports.inject = ['touchgal', 'gameCache'];
5
7
  function apply(ctx, config) {
6
8
  ctx.command('查询gal <keyword:text>', '查询Galgame信息')
7
9
  .action(async ({ session }, keyword) => {
@@ -16,9 +18,15 @@ function apply(ctx, config) {
16
18
  }
17
19
  // 缓存结果
18
20
  results.forEach(game => ctx.gameCache.set(game.id, game));
19
- const forwardMessages = results.map(game => {
21
+ // 并发下载并转换所有图片
22
+ const imageBuffers = await Promise.all(results.map(game => ctx.touchgal.downloadAndConvertImage(game.banner)));
23
+ const forwardMessages = results.map((game, index) => {
24
+ const imageBuffer = imageBuffers[index];
25
+ const imageElement = imageBuffer
26
+ ? (0, koishi_1.h)('image', { url: `base64://${imageBuffer.toString('base64')}` })
27
+ : (0, koishi_1.h)('text', { content: '封面图加载失败' });
20
28
  const content = [
21
- (0, koishi_1.h)('image', { url: game.banner }),
29
+ imageElement,
22
30
  `ID: ${game.id}`,
23
31
  `名称: ${game.name}`,
24
32
  `平台: ${game.platform.join(', ')}`,
@@ -55,8 +63,12 @@ function apply(ctx, config) {
55
63
  return `未找到ID为 ${id} 的下载资源。`;
56
64
  }
57
65
  const gameTitle = gameInfo ? `游戏: ${gameInfo.name} (ID: ${id})` : `游戏ID: ${id}`;
66
+ const imageBuffer = gameInfo ? await ctx.touchgal.downloadAndConvertImage(gameInfo.banner) : null;
67
+ const imageElement = imageBuffer
68
+ ? (0, koishi_1.h)('image', { url: `base64://${imageBuffer.toString('base64')}` })
69
+ : (0, koishi_1.h)('text', { content: gameInfo ? '封面图加载失败' : '' });
58
70
  const header = [
59
- gameInfo ? (0, koishi_1.h)('image', { url: gameInfo.banner }) : '',
71
+ imageElement,
60
72
  gameTitle,
61
73
  `共找到 ${downloads.length} 个下载资源:`,
62
74
  ].filter(Boolean).join('\n');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "koishi-plugin-checkgal",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "个人测试请勿使用",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
@@ -24,5 +24,8 @@
24
24
  "devDependencies": {
25
25
  "koishi": "^4.18.9",
26
26
  "typescript": "^5.9.3"
27
+ },
28
+ "dependencies": {
29
+ "sharp": "^0.34.4"
27
30
  }
28
31
  }