koishi-plugin-checkgal 1.1.1 → 1.2.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/api.d.ts +2 -1
- package/lib/api.js +48 -2
- package/lib/commands.js +3 -5
- package/package.json +1 -1
package/lib/api.d.ts
CHANGED
|
@@ -4,8 +4,9 @@ import { Config } from './config';
|
|
|
4
4
|
export declare class TouchGalAPI {
|
|
5
5
|
private http;
|
|
6
6
|
private logger;
|
|
7
|
+
private tempDir;
|
|
7
8
|
constructor(ctx: Context);
|
|
8
9
|
searchGame(keyword: string, config: Config): Promise<GameInfo[]>;
|
|
9
10
|
getDownloads(patchId: number): Promise<DownloadResource[]>;
|
|
10
|
-
downloadAndConvertImage(url: string): Promise<
|
|
11
|
+
downloadAndConvertImage(url: string): Promise<string | null>;
|
|
11
12
|
}
|
package/lib/api.js
CHANGED
|
@@ -1,14 +1,55 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
2
35
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
36
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
37
|
};
|
|
5
38
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
39
|
exports.TouchGalAPI = void 0;
|
|
7
40
|
const sharp_1 = __importDefault(require("sharp"));
|
|
41
|
+
const path = __importStar(require("path"));
|
|
42
|
+
const crypto = __importStar(require("crypto"));
|
|
43
|
+
const fs_1 = require("fs");
|
|
8
44
|
class TouchGalAPI {
|
|
9
45
|
constructor(ctx) {
|
|
10
46
|
this.http = ctx.http;
|
|
11
47
|
this.logger = ctx.logger('checkgal-api');
|
|
48
|
+
// 直接使用 koishi/temp 目录
|
|
49
|
+
this.tempDir = path.join(ctx.app.baseDir, 'temp');
|
|
50
|
+
ctx.on('ready', async () => {
|
|
51
|
+
await fs_1.promises.mkdir(this.tempDir, { recursive: true });
|
|
52
|
+
});
|
|
12
53
|
}
|
|
13
54
|
async searchGame(keyword, config) {
|
|
14
55
|
const url = 'https://www.touchgal.us/api/search';
|
|
@@ -69,10 +110,15 @@ class TouchGalAPI {
|
|
|
69
110
|
const response = await this.http.get(url, {
|
|
70
111
|
responseType: 'arraybuffer',
|
|
71
112
|
});
|
|
72
|
-
// 类型守卫,确保 response 是 ArrayBuffer
|
|
73
113
|
if (!(response instanceof ArrayBuffer))
|
|
74
114
|
return null;
|
|
75
|
-
|
|
115
|
+
const imageBuffer = await (0, sharp_1.default)(Buffer.from(response)).jpeg().toBuffer();
|
|
116
|
+
const hash = crypto.createHash('md5').update(url).digest('hex');
|
|
117
|
+
const fileName = `${hash}.jpg`;
|
|
118
|
+
const filePath = path.join(this.tempDir, fileName);
|
|
119
|
+
await fs_1.promises.writeFile(filePath, imageBuffer);
|
|
120
|
+
// 返回相对于 koishi 根目录的相对路径
|
|
121
|
+
return `temp/${fileName}`;
|
|
76
122
|
}
|
|
77
123
|
catch (error) {
|
|
78
124
|
this.logger.error(`Failed to download or convert image from ${url}:`, error);
|
package/lib/commands.js
CHANGED
|
@@ -23,7 +23,7 @@ function apply(ctx, config) {
|
|
|
23
23
|
const forwardMessages = results.map((game, index) => {
|
|
24
24
|
const imagePath = imagePaths[index];
|
|
25
25
|
const imageElement = imagePath
|
|
26
|
-
? (0, koishi_1.h)('image', { url:
|
|
26
|
+
? (0, koishi_1.h)('image', { url: imagePath })
|
|
27
27
|
: (0, koishi_1.h)('text', { content: '封面图加载失败' });
|
|
28
28
|
const content = [
|
|
29
29
|
imageElement,
|
|
@@ -32,7 +32,6 @@ function apply(ctx, config) {
|
|
|
32
32
|
`平台: ${game.platform.join(', ')}`,
|
|
33
33
|
`语言: ${game.language.join(', ')}`,
|
|
34
34
|
].join('\n');
|
|
35
|
-
// 使用固定的机器人名称和 ID,避免 bot.username 可能不存在的问题
|
|
36
35
|
return (0, koishi_1.h)('message', { userId: session.bot.selfId, nickname: 'CheckGal Bot' }, content);
|
|
37
36
|
});
|
|
38
37
|
await session.send((0, koishi_1.h)('message', { forward: true }, forwardMessages));
|
|
@@ -44,7 +43,7 @@ function apply(ctx, config) {
|
|
|
44
43
|
if (!id)
|
|
45
44
|
return '请输入游戏ID。';
|
|
46
45
|
let gameInfo = ctx.gameCache.get(id);
|
|
47
|
-
//
|
|
46
|
+
// 如果缓存中没有,尝试重新获取
|
|
48
47
|
if (!gameInfo) {
|
|
49
48
|
await session.send('缓存中未找到该游戏信息,正在尝试重新搜索...');
|
|
50
49
|
const results = await ctx.touchgal.searchGame(String(id), config);
|
|
@@ -54,7 +53,6 @@ function apply(ctx, config) {
|
|
|
54
53
|
ctx.gameCache.set(id, gameInfo);
|
|
55
54
|
}
|
|
56
55
|
else {
|
|
57
|
-
// 如果通过ID也搜不到,就直接获取下载链接
|
|
58
56
|
await session.send(`无法获取游戏“${id}”的详细信息,但仍会尝试获取下载链接...`);
|
|
59
57
|
}
|
|
60
58
|
}
|
|
@@ -65,7 +63,7 @@ function apply(ctx, config) {
|
|
|
65
63
|
const gameTitle = gameInfo ? `游戏: ${gameInfo.name} (ID: ${id})` : `游戏ID: ${id}`;
|
|
66
64
|
const imagePath = gameInfo ? await ctx.touchgal.downloadAndConvertImage(gameInfo.banner) : null;
|
|
67
65
|
const imageElement = imagePath
|
|
68
|
-
? (0, koishi_1.h)('image', { url:
|
|
66
|
+
? (0, koishi_1.h)('image', { url: imagePath })
|
|
69
67
|
: (0, koishi_1.h)('text', { content: gameInfo ? '封面图加载失败' : '' });
|
|
70
68
|
const header = [
|
|
71
69
|
imageElement,
|