koishi-plugin-phimg2 1.0.6 → 1.0.7
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.d.ts +2 -1
- package/lib/index.js +37 -38
- package/package.json +1 -1
package/lib/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Context, Schema } from 'koishi';
|
|
2
|
-
export declare const name = "
|
|
2
|
+
export declare const name = "phimg2";
|
|
3
3
|
export declare const inject: string[];
|
|
4
4
|
declare module 'koishi' {
|
|
5
5
|
interface Tables {
|
|
@@ -13,6 +13,7 @@ export interface Config {
|
|
|
13
13
|
enabledByDefault: boolean;
|
|
14
14
|
useGlobalTagsByDefault: boolean;
|
|
15
15
|
filterId: number;
|
|
16
|
+
showErrorLog: boolean;
|
|
16
17
|
}
|
|
17
18
|
export declare const Config: Schema<Config>;
|
|
18
19
|
interface GroupConfig {
|
package/lib/index.js
CHANGED
|
@@ -27,7 +27,7 @@ __export(src_exports, {
|
|
|
27
27
|
});
|
|
28
28
|
module.exports = __toCommonJS(src_exports);
|
|
29
29
|
var import_koishi = require("koishi");
|
|
30
|
-
var name = "
|
|
30
|
+
var name = "phimg2";
|
|
31
31
|
var inject = ["database", "http"];
|
|
32
32
|
var translationTable = {
|
|
33
33
|
";": ";",
|
|
@@ -55,7 +55,8 @@ var Config = import_koishi.Schema.object({
|
|
|
55
55
|
defaultTags: import_koishi.Schema.array(String).description("全局默认标签").default(["safe"]),
|
|
56
56
|
enabledByDefault: import_koishi.Schema.boolean().description("新群聊默认启用搜图功能").default(true),
|
|
57
57
|
useGlobalTagsByDefault: import_koishi.Schema.boolean().description("新群聊默认启用全局标签").default(true),
|
|
58
|
-
filterId: import_koishi.Schema.number().description("搜索使用的 Filter ID (例如 100073)").default(100073)
|
|
58
|
+
filterId: import_koishi.Schema.number().description("搜索使用的 Filter ID (例如 100073)").default(100073),
|
|
59
|
+
showErrorLog: import_koishi.Schema.boolean().description("是否在控制台输出搜图失败的日志").default(false)
|
|
59
60
|
});
|
|
60
61
|
function apply(ctx, config) {
|
|
61
62
|
ctx.model.extend("phimg_config", {
|
|
@@ -107,7 +108,13 @@ function apply(ctx, config) {
|
|
|
107
108
|
headers: { "User-Agent": "Phimg for Koishi" }
|
|
108
109
|
});
|
|
109
110
|
} else {
|
|
110
|
-
|
|
111
|
+
const formBody = new URLSearchParams();
|
|
112
|
+
for (const key in params) {
|
|
113
|
+
if (params[key] !== void 0 && params[key] !== null) {
|
|
114
|
+
formBody.append(key, String(params[key]));
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
responseData = await ctx.http.post(endpoint, formBody, {
|
|
111
118
|
params: queryParams,
|
|
112
119
|
headers: {
|
|
113
120
|
"User-Agent": "Phimg for Koishi",
|
|
@@ -120,9 +127,11 @@ function apply(ctx, config) {
|
|
|
120
127
|
}
|
|
121
128
|
return responseData;
|
|
122
129
|
} catch (error) {
|
|
130
|
+
if (config.showErrorLog) {
|
|
131
|
+
ctx.logger("phimg").warn(`API Error: ${error.message}`);
|
|
132
|
+
}
|
|
123
133
|
if (error.response?.status === 404) throw new Error("未找到匹配的图片");
|
|
124
|
-
|
|
125
|
-
throw new Error(`API 请求失败: ${error.message}`);
|
|
134
|
+
throw new Error(error.message || "API 请求失败");
|
|
126
135
|
}
|
|
127
136
|
}, "makeRequest");
|
|
128
137
|
const VIDEO_TYPES = ["webm", "mp4"];
|
|
@@ -131,39 +140,36 @@ function apply(ctx, config) {
|
|
|
131
140
|
const file = selected.representations.full;
|
|
132
141
|
const url = file.endsWith(".webm") ? selected.representations.medium : selected.representations.large;
|
|
133
142
|
const fileType = file.split(".").pop()?.toLowerCase() || "";
|
|
134
|
-
if (VIDEO_TYPES.includes(fileType))
|
|
135
|
-
return import_koishi.h.video(url);
|
|
136
|
-
}
|
|
143
|
+
if (VIDEO_TYPES.includes(fileType)) return import_koishi.h.video(url);
|
|
137
144
|
return import_koishi.h.image(url);
|
|
138
145
|
}, "getMediaElement");
|
|
139
146
|
const searchHelp = `用法: /搜图 [tags|distance]
|
|
140
147
|
|
|
148
|
+
用法说明:
|
|
149
|
+
引用图片: 进行以图搜图 (默认距离 0.25)
|
|
150
|
+
输入文本: 进行标签搜索
|
|
151
|
+
|
|
141
152
|
可选项:
|
|
142
153
|
--tags 获取当前群聊内置标签列表
|
|
143
154
|
--status 获取当前群聊的搜图功能状态
|
|
144
|
-
--pp [
|
|
145
|
-
--p [
|
|
146
|
-
--sf [
|
|
147
|
-
--sd [
|
|
148
|
-
--i [index]
|
|
149
|
-
|
|
150
|
-
提示:
|
|
151
|
-
图搜图使用方式为引用图片,默认匹配距离为0.25`;
|
|
152
|
-
const configHelp = `用法: 搜图-c [选项]
|
|
155
|
+
--pp [num] 每页数量 (默认50)
|
|
156
|
+
--p [num] 页码 (默认1)
|
|
157
|
+
--sf [field] 排序字段 (默认score)
|
|
158
|
+
--sd [desc|asc] 排序方向 (默认desc)
|
|
159
|
+
--i [index] 选择结果索引 (默认随机)`;
|
|
160
|
+
const configHelp = `用法: /搜图-c [选项]
|
|
153
161
|
|
|
154
162
|
可选项:
|
|
155
|
-
--add [tags]
|
|
156
|
-
--rm [tags]
|
|
157
|
-
--on
|
|
158
|
-
--off
|
|
163
|
+
--add [tags] 添加标签
|
|
164
|
+
--rm [tags] 删除标签
|
|
165
|
+
--on 开启搜图
|
|
166
|
+
--off 关闭搜图
|
|
159
167
|
--onglobal 启用全局标签
|
|
160
168
|
--offglobal 关闭全局标签`;
|
|
161
169
|
ctx.command("搜图 [...params]", "从图站搜索图片").option("tags", "--tags").option("status", "--status").option("pp", "--pp <per_page:number>", { fallback: 50 }).option("p", "--p <page:number>", { fallback: 1 }).option("sf", "--sf <sf:string>", { fallback: "score" }).option("sd", "--sd <sd:string>", { fallback: "desc" }).option("i", "--i <index:number>", { fallback: -1 }).action(async ({ session, options }, ...paramsArray) => {
|
|
162
170
|
if (!session?.guildId) return "搜图仅限群聊使用。";
|
|
163
|
-
|
|
164
|
-
if (!
|
|
165
|
-
return searchHelp;
|
|
166
|
-
}
|
|
171
|
+
const rawParams = paramsArray.join(" ");
|
|
172
|
+
if (!rawParams && !session.quote && !options.tags && !options.status) return searchHelp;
|
|
167
173
|
const groupId = session.guildId;
|
|
168
174
|
const groupConfig = await getGroupConfig(groupId);
|
|
169
175
|
if (options.status) {
|
|
@@ -178,17 +184,13 @@ function apply(ctx, config) {
|
|
|
178
184
|
if (!groupConfig.enabled) {
|
|
179
185
|
return '搜图未在本群开启,管理员请用 "搜图-c --on" 启动';
|
|
180
186
|
}
|
|
181
|
-
|
|
182
|
-
const cleanParams =
|
|
187
|
+
const paramsText = translateText(rawParams || "");
|
|
188
|
+
const cleanParams = paramsText.replace(/<[^>]+>/g, "").trim();
|
|
183
189
|
let imageUrl;
|
|
184
190
|
if (session.quote) {
|
|
185
191
|
const img = import_koishi.h.select(session.quote.content, "image")[0] || import_koishi.h.select(session.quote.content, "img")[0];
|
|
186
192
|
if (img) imageUrl = img.attrs.url || img.attrs.src;
|
|
187
193
|
}
|
|
188
|
-
if (!imageUrl) {
|
|
189
|
-
const img = import_koishi.h.select(session.content, "image")[0] || import_koishi.h.select(session.content, "img")[0];
|
|
190
|
-
if (img) imageUrl = img.attrs.url || img.attrs.src;
|
|
191
|
-
}
|
|
192
194
|
try {
|
|
193
195
|
if (imageUrl) {
|
|
194
196
|
let distance = 0.25;
|
|
@@ -207,26 +209,23 @@ function apply(ctx, config) {
|
|
|
207
209
|
};
|
|
208
210
|
const data = await makeRequest("reverse", queryParams);
|
|
209
211
|
const images = data.images;
|
|
210
|
-
if (images.length > 10) {
|
|
211
|
-
return `搜索到过多图片 (${images.length} 张),请尝试减小距离参数。`;
|
|
212
|
-
}
|
|
212
|
+
if (images.length > 10) return `搜索到过多图片 (${images.length} 张),请尝试减小距离参数。`;
|
|
213
213
|
if (images.length === 0) return "未找到匹配的图片";
|
|
214
214
|
const result = [(0, import_koishi.h)("at", { id: session.userId }), import_koishi.h.text(`
|
|
215
215
|
distance: ${distance}
|
|
216
216
|
`)];
|
|
217
217
|
for (const img of images) {
|
|
218
218
|
result.push(getMediaElement(img));
|
|
219
|
-
result.push(import_koishi.h.text(`
|
|
220
|
-
id: ${img.id} | score: ${img.score}
|
|
219
|
+
result.push(import_koishi.h.text(` nid: ${img.id} | score: ${img.score}
|
|
221
220
|
`));
|
|
222
221
|
}
|
|
223
222
|
return result;
|
|
224
223
|
} else {
|
|
225
|
-
const userTags = cleanParams ? cleanParams.split(
|
|
224
|
+
const userTags = cleanParams ? cleanParams.split(/[,,]/).map((t) => t.trim()).filter((t) => t) : [];
|
|
226
225
|
const globalTags = groupConfig.useGlobalTags ? config.defaultTags : [];
|
|
227
226
|
const groupTags = groupConfig.customTags;
|
|
228
227
|
const allTags = Array.from(/* @__PURE__ */ new Set([...groupTags, ...globalTags, ...userTags]));
|
|
229
|
-
if (allTags.length === 0
|
|
228
|
+
if (allTags.length === 0) return "请输入搜索标签。";
|
|
230
229
|
const queryParams = {
|
|
231
230
|
q: allTags.join(", "),
|
|
232
231
|
key: config.apiKey,
|