koishi-plugin-phimg2 1.0.5 → 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 CHANGED
@@ -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
@@ -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
- responseData = await ctx.http.post(endpoint, params, {
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
- ctx.logger("phimg").warn(error);
125
- throw new Error(`${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 [per_page] 每页结果数量,默认为50
145
- --p [page] 页码,默认为1
146
- --sf [sf] 排序字段,默认为score
147
- --sd [sd] 排序方向,默认为desc
148
- --i [index] 选择结果索引,默认为-1(即随机)
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
- let params = paramsArray.join(" ").trim();
164
- if (!params && !session.quote && !options.tags && !options.status) {
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,20 +184,18 @@ function apply(ctx, config) {
178
184
  if (!groupConfig.enabled) {
179
185
  return '搜图未在本群开启,管理员请用 "搜图-c --on" 启动';
180
186
  }
181
- params = translateText(params || "");
187
+ const paramsText = translateText(rawParams || "");
188
+ const cleanParams = paramsText.replace(/<[^>]+>/g, "").trim();
182
189
  let imageUrl;
183
- const quote = session.quote;
184
- if (quote) {
185
- const imgElement = import_koishi.h.select(quote.content, "image")[0] || import_koishi.h.select(quote.content, "img")[0];
186
- if (imgElement) {
187
- imageUrl = imgElement.attrs.url || imgElement.attrs.src;
188
- }
190
+ if (session.quote) {
191
+ const img = import_koishi.h.select(session.quote.content, "image")[0] || import_koishi.h.select(session.quote.content, "img")[0];
192
+ if (img) imageUrl = img.attrs.url || img.attrs.src;
189
193
  }
190
194
  try {
191
195
  if (imageUrl) {
192
196
  let distance = 0.25;
193
- if (params) {
194
- const parsed = Number(params);
197
+ if (cleanParams) {
198
+ const parsed = Number(cleanParams);
195
199
  if (!isNaN(parsed)) {
196
200
  distance = parsed;
197
201
  } else {
@@ -205,26 +209,23 @@ function apply(ctx, config) {
205
209
  };
206
210
  const data = await makeRequest("reverse", queryParams);
207
211
  const images = data.images;
208
- if (images.length > 10) {
209
- return `搜索到过多图片 (${images.length} 张),请尝试减小距离参数。`;
210
- }
212
+ if (images.length > 10) return `搜索到过多图片 (${images.length} 张),请尝试减小距离参数。`;
211
213
  if (images.length === 0) return "未找到匹配的图片";
212
214
  const result = [(0, import_koishi.h)("at", { id: session.userId }), import_koishi.h.text(`
213
215
  distance: ${distance}
214
216
  `)];
215
217
  for (const img of images) {
216
218
  result.push(getMediaElement(img));
217
- result.push(import_koishi.h.text(`
218
- id: ${img.id} | score: ${img.score}
219
+ result.push(import_koishi.h.text(` nid: ${img.id} | score: ${img.score}
219
220
  `));
220
221
  }
221
222
  return result;
222
223
  } else {
223
- const userTags = params ? params.split(",").map((t) => t.trim()).filter((t) => t) : [];
224
+ const userTags = cleanParams ? cleanParams.split(/[,,]/).map((t) => t.trim()).filter((t) => t) : [];
224
225
  const globalTags = groupConfig.useGlobalTags ? config.defaultTags : [];
225
226
  const groupTags = groupConfig.customTags;
226
227
  const allTags = Array.from(/* @__PURE__ */ new Set([...groupTags, ...globalTags, ...userTags]));
227
- if (allTags.length === 0 && !params) return "请输入搜索标签。";
228
+ if (allTags.length === 0) return "请输入搜索标签。";
228
229
  const queryParams = {
229
230
  q: allTags.join(", "),
230
231
  key: config.apiKey,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-phimg2",
3
3
  "description": "Koishi 插件,让机器人通过Philomena API在使用Philomena搭建的图站上使用标签(tags)搜图或图搜图",
4
- "version": "1.0.5",
4
+ "version": "1.0.7",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "files": [