koishi-plugin-phimg2 1.0.6 → 1.0.8

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
@@ -1,5 +1,5 @@
1
1
  import { Context, Schema } from 'koishi';
2
- export declare const name = "phimg";
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 = "phimg";
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
- 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(`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,38 @@ 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
+ 输入文本: 进行标签搜索
152
+
141
153
  可选项:
142
154
  --tags 获取当前群聊内置标签列表
143
155
  --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 [选项]
156
+ --pp [num] 每页数量 (默认50)
157
+ --p [num] 页码 (默认1)
158
+ --sf [field] 排序字段 (默认score)
159
+ --sd [desc|asc] 排序方向 (默认desc)
160
+ --i [index] 选择结果索引 (默认随机)`;
161
+ const configHelp = `用法: /搜图-c [选项]
153
162
 
154
163
  可选项:
155
- --add [tags] 添加标签,多个标签用逗号分隔
156
- --rm [tags] 删除标签,多个标签用逗号分隔
157
- --on 开启当前群聊的搜图功能
158
- --off 关闭当前群聊的搜图功能
164
+ --add [tags] 添加标签
165
+ --rm [tags] 删除标签
166
+ --on 开启搜图
167
+ --off 关闭搜图
159
168
  --onglobal 启用全局标签
160
169
  --offglobal 关闭全局标签`;
161
170
  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
171
  if (!session?.guildId) return "搜图仅限群聊使用。";
163
- let params = paramsArray.join(" ").trim();
164
- if (!params && !session.quote && !options.tags && !options.status) {
165
- return searchHelp;
166
- }
172
+ const rawParams = paramsArray.join(" ");
173
+ const hasImageInContent = !!(import_koishi.h.select(session.content, "image")[0] || import_koishi.h.select(session.content, "img")[0]);
174
+ if (!rawParams && !session.quote && !hasImageInContent && !options.tags && !options.status) return searchHelp;
167
175
  const groupId = session.guildId;
168
176
  const groupConfig = await getGroupConfig(groupId);
169
177
  if (options.status) {
@@ -178,8 +186,8 @@ function apply(ctx, config) {
178
186
  if (!groupConfig.enabled) {
179
187
  return '搜图未在本群开启,管理员请用 "搜图-c --on" 启动';
180
188
  }
181
- params = translateText(params || "");
182
- const cleanParams = params.replace(/<[^>]+>/g, "").trim();
189
+ const paramsText = translateText(rawParams || "");
190
+ const cleanParams = paramsText.replace(/<[^>]+>/g, "").trim();
183
191
  let imageUrl;
184
192
  if (session.quote) {
185
193
  const img = import_koishi.h.select(session.quote.content, "image")[0] || import_koishi.h.select(session.quote.content, "img")[0];
@@ -207,26 +215,23 @@ function apply(ctx, config) {
207
215
  };
208
216
  const data = await makeRequest("reverse", queryParams);
209
217
  const images = data.images;
210
- if (images.length > 10) {
211
- return `搜索到过多图片 (${images.length} 张),请尝试减小距离参数。`;
212
- }
218
+ if (images.length > 10) return `搜索到过多图片 (${images.length} 张),请尝试减小距离参数。`;
213
219
  if (images.length === 0) return "未找到匹配的图片";
214
220
  const result = [(0, import_koishi.h)("at", { id: session.userId }), import_koishi.h.text(`
215
221
  distance: ${distance}
216
222
  `)];
217
223
  for (const img of images) {
218
224
  result.push(getMediaElement(img));
219
- result.push(import_koishi.h.text(`
220
- id: ${img.id} | score: ${img.score}
225
+ result.push(import_koishi.h.text(` nid: ${img.id} | score: ${img.score}
221
226
  `));
222
227
  }
223
228
  return result;
224
229
  } else {
225
- const userTags = cleanParams ? cleanParams.split(",").map((t) => t.trim()).filter((t) => t) : [];
230
+ const userTags = cleanParams ? cleanParams.split(/[,,]/).map((t) => t.trim()).filter((t) => t) : [];
226
231
  const globalTags = groupConfig.useGlobalTags ? config.defaultTags : [];
227
232
  const groupTags = groupConfig.customTags;
228
233
  const allTags = Array.from(/* @__PURE__ */ new Set([...groupTags, ...globalTags, ...userTags]));
229
- if (allTags.length === 0 && !cleanParams) return "请输入搜索标签。";
234
+ if (allTags.length === 0) return "请输入搜索标签。";
230
235
  const queryParams = {
231
236
  q: allTags.join(", "),
232
237
  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.6",
4
+ "version": "1.0.8",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "files": [