UNPKG

koishi-plugin-gyyg-randompic

Version:

随机输出自选路径下的图片,图片格式支持jpg,jpeg,png,gif

84 lines (82 loc) 3.48 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 name2 in all) __defProp(target, name2, { get: all[name2], 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, name: () => name }); module.exports = __toCommonJS(src_exports); var import_koishi = require("koishi"); var import_path = __toESM(require("path")); var import_url = require("url"); var import_promises = require("node:fs/promises"); var name = "pic"; var Config = import_koishi.Schema.object({ addr: import_koishi.Schema.string().required().description("图片目录绝对路径(如 D:\\images 或 /home/user/pics)") }); function apply(ctx) { ctx.command("来张经典图").action(async ({ session }) => { const dirPath = import_path.default.resolve(ctx.config.addr); try { await (0, import_promises.access)(dirPath, import_promises.constants.R_OK); const files = await (0, import_promises.readdir)(dirPath); const validExtensions = /* @__PURE__ */ new Set([".jpg", ".jpeg", ".png", ".gif"]); const imageFiles = files.filter( (file) => validExtensions.has(import_path.default.extname(file).toLowerCase()) ); if (imageFiles.length === 0) { return [ "⚠️ 未发现可用图片", `支持格式:${Array.from(validExtensions).join(", ")}`, `当前路径:${dirPath}` ].join("\n"); } const randomFile = imageFiles[Math.floor(Math.random() * imageFiles.length)]; return import_koishi.h.image((0, import_url.pathToFileURL)(import_path.default.join(dirPath, randomFile)).href); } catch (error) { if (error.code === "ENOENT") { return `❌ 路径不存在:${dirPath}`; } if (error.code === "EACCES") { return `🔐 无权限访问目录:${dirPath}`; } ctx.logger("pic").error("图片加载失败:", error); return "服务暂时不可用,请联系管理员"; } }); } __name(apply, "apply"); // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { Config, apply, name });