UNPKG

koishi-plugin-pointmintmarket-pic

Version:

使用积分兑换图片?用这个插件就对了。支持多个api接入,通过pointmarket实现积分兑换。

120 lines (118 loc) 5.39 kB
var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var src_exports = {}; __export(src_exports, { Config: () => Config, apply: () => apply, inject: () => inject }); module.exports = __toCommonJS(src_exports); var import_koishi = require("koishi"); var jsonpath = __toESM(require("jsonpath")); var Config = import_koishi.Schema.object({ timeout: import_koishi.Schema.number().default(5e3).min(1e3).description("http请求超时时间,单位为毫秒,此值不影响获取图片的时间"), apiList: import_koishi.Schema.array( import_koishi.Schema.intersect([ import_koishi.Schema.object({ id: import_koishi.Schema.string().description("商品ID"), name: import_koishi.Schema.string().description("此api对应的商品名称"), description: import_koishi.Schema.string().role("textarea", { rows: [2, 2] }).description("商品详细描述"), price: import_koishi.Schema.number().default(10).min(1).description("商品定价"), tags: import_koishi.Schema.array(import_koishi.Schema.string()).description("搜索关键词标签"), stock: import_koishi.Schema.number().description("初始库存设置") }).description("API配置 - 基础设置"), import_koishi.Schema.object({ url: import_koishi.Schema.string().description("完整的API请求地址 示例: `https://api.example.com/data`"), method: import_koishi.Schema.union(["GET", "POST"]).default("GET").description("HTTP 请求方法"), response: import_koishi.Schema.string().description("配置解析的JSON路径,可通过[🔗*JSONPath Online Evaluator*](https://jsonpath.com/)测试,API会直接返回图片的请将此值配置保持空值") }).description("API配置 - API设置") ]) ).description("API配置"), debug: import_koishi.Schema.boolean().default(false).description("是否启用调试日志") }); var inject = ["market", "http"]; function apply(ctx, config) { const logger = new import_koishi.Logger(ctx.name); ctx.on("ready", async () => { ctx.market.unregisterItems(ctx.name); await registerExampleItems(); logger.info("示例商品已注册"); }); ctx.on("dispose", async () => { ctx.market.unregisterItems(ctx.name); logger.info("示例商品已注销"); }); async function registerExampleItems() { try { for (const api of config.apiList) { const item = { id: api.id, name: api.name, description: api.description, price: api.price, tags: api.tags, stock: api.stock, // 购买回调函数 onPurchase: /* @__PURE__ */ __name(async (session) => { try { if (!api.response) { session.send(`<img src="${api.url}"/>`); return { code: 200, msg: "兑换成功", data: { itemType: "api" } }; } let data; if (api.method === "GET") { data = await ctx.http.get(api.url, { timeout: config.timeout }); } else { data = await ctx.http.post(api.url, { timeout: config.timeout }); } const result = jsonpath.query(data, api.response); await session.send(`<img src="${result[0]}"/>`); return { code: 200, msg: "兑换成功", data: { itemType: "api" } }; } catch (error) { logger.error("兑换示例商品时出错:", error); return { code: 500, msg: "兑换失败", data: { itemType: "api" } }; } }, "onPurchase") }; ctx.market.registerItem(ctx.name, item); } } catch (error) { logger.error("注册示例商品时出错:", error); } } __name(registerExampleItems, "registerExampleItems"); } __name(apply, "apply"); // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { Config, apply, inject });