koishi-plugin-equery
Version:
68 lines (67 loc) • 3.29 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Config = exports.name = void 0;
exports.apply = apply;
const koishi_1 = require("koishi");
const query_1 = require("./query");
const response_1 = require("./utils/response");
const usage_1 = require("./usage");
exports.name = "equery";
exports.Config = koishi_1.Schema.object({
apiKey: koishi_1.Schema.string()
.description("验证令牌(Synjones-Auth值)")
.default("")
.required(false),
});
function apply(ctx, config) {
ctx.command("equery <building> <room>", "查询山东大学青岛校区宿舍剩余电量")
.usage(usage_1.usage)
.example("equery S1 A101 查询 S1 A101 的电量")
.action(async (_, building, room) => {
if (!config.apiKey || config.apiKey.trim() === "") {
return (0, response_1.formatResponse)((0, response_1.failure)("配置错误", "未设置必要的API密钥,请在插件配置中设置Synjones-Auth验证令牌"));
}
if (!building || !room) {
return (0, response_1.formatResponse)((0, response_1.failure)("参数不足", "请提供建筑和房间号,例如: equery S1 A101"));
}
(building = building.toUpperCase()), (room = room.toUpperCase());
try {
const queryResult = await (0, query_1.query)(building, room, config.apiKey);
if (!queryResult || typeof queryResult !== "object") {
return (0, response_1.formatResponse)((0, response_1.failure)("查询失败", "返回结果格式异常"));
}
if (!queryResult.success) {
return (0, response_1.formatResponse)((0, response_1.failure)("查询失败", queryResult.error ||
"请检查宿舍楼和房间号是否正确(或者排查令牌问题)"));
}
console.log("查询结果:", JSON.stringify(queryResult));
let electricityData = "数据获取失败";
let buildingName = building;
let roomInfo = room;
let areaInfo = "青岛校区";
if (queryResult.data) {
if (typeof queryResult.data === "object") {
electricityData =
queryResult.data.electricity || "未知";
buildingName =
queryResult.data.buildingName || building;
roomInfo = queryResult.data.room || room;
areaInfo = queryResult.data.areaName || "青岛校区";
}
else {
electricityData = String(queryResult.data);
}
}
else if (queryResult.message) {
electricityData = String(queryResult.message);
}
const responseMessage = `${areaInfo} ${buildingName} ${roomInfo}` +
`\n剩余电量: ${electricityData}`;
return (0, response_1.formatResponse)((0, response_1.success)(`${buildingName} ${roomInfo}`, responseMessage));
}
catch (error) {
console.error("电量查询失败:", error);
return (0, response_1.formatResponse)((0, response_1.failure)("电量查询失败", error.message));
}
});
}