koishi-plugin-vv
Version:
57 lines (56 loc) • 2.11 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Config = exports.name = void 0;
exports.apply = apply;
const koishi_1 = require("koishi");
const axios_1 = __importDefault(require("axios"));
exports.name = "vv";
exports.Config = koishi_1.Schema.object({
apiBaseUrl: koishi_1.Schema.string()
.default("https://api.zvv.quest")
.description("API地址"),
defaultImageCount: koishi_1.Schema.number()
.default(5)
.min(1)
.max(20)
.description("默认图片数量"),
timeout: koishi_1.Schema.number().default(10000).description("请求超时时间(毫秒)"),
});
function apply(ctx, config) {
ctx.command("vv <text> [n:number]", "搜索 VV 梗图")
.example("vv 我喜欢你 5")
.action(async ({ session }, text, n) => {
if (!text || text.trim() === "") {
return "请输入要搜索的文本";
}
// 如果未指定n,则使用配置中的默认值
n = n || config.defaultImageCount;
try {
const response = await axios_1.default.get(`${config.apiBaseUrl}/search`, {
params: {
q: text,
n: n,
},
timeout: config.timeout,
});
if (response.data.code === 200 &&
response.data.data?.length > 0) {
const images = response.data.data;
// 创建一个图像消息数组
const elements = images.map((url) => koishi_1.h.image(url));
// 返回复合消息
return (0, koishi_1.h)("message", elements);
}
else {
return `没有找到与"${text}"相关的梗图`;
}
}
catch (error) {
console.error("搜索VV梗图出错:", error);
return `搜索出错: ${error.message || "未知错误"}`;
}
});
}