koishi-plugin-shutdown
Version:
shutdown(8) for Koishi.
134 lines (130 loc) • 7.97 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
var __commonJS = (cb, mod) => function __require() {
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
};
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/locales/zh-CN.yml
var require_zh_CN = __commonJS({
"src/locales/zh-CN.yml"(exports2, module2) {
module2.exports = { commands: { shutdown: { description: "关闭或重启 Koishi", usage: "此指令可用于关闭或重启 Koishi。\n第一个参数是一个时间字符串 (通常是“now”)。\n时间字符串用于指定执行关机的时间。可以是“hh:mm”格式的小时/分钟,也可以直接通过“+m”中表示从现在开始指定的分钟数 m。\n“now”是“+0”的别名,即触发立即关闭。如果未指定时间参数,则默认“+1”。\n可选地,这之后可能会在关闭之前向所有登录用户发送一条墙消息。", options: { reboot: "软重启", rebootHard: "硬重启", wall: "发送提示信息", clear: "清除所有计划中的关闭", show: "列出计划中的关闭操作" }, messages: { "no-pending": "没有计划中的关闭", "list-header": "计划中的关闭:", "list-item": { reboot: "重启计划于:{0}", poweroff: "关闭计划于:{0}" }, "invalid-time": "无法解析时间:{0}", clear: "已清除所有计划中的关闭。", reboot: "计划于 {0} 重启,使用“shutdown -c”以取消。", poweroff: "计划于 {0} 关闭,使用“shutdown -c”以取消。", restarted: "机器人已重新启动。" }, "wall-messages": { clear: "关闭已经取消。", reboot: "系统将在 {0} 重启。", poweroff: "系统将在 {0} 关闭。" } } } };
}
});
// src/locales/en-US.yml
var require_en_US = __commonJS({
"src/locales/en-US.yml"(exports2, module2) {
module2.exports = { commands: { shutdown: { description: "Power off or reboot Koishi", usage: 'shutdown may be used to power off or reboot Koishi.\nThe first argument may be a time string (which is usually "now").\nThe time string may either be in the format "hh:mm" for hour/minutes specifying the time to execute the shutdown at, specified in 24h clock format. Alternatively it may be in the syntax "+m" referring to the specified number of minutes m from now.\n"now" is an alias for "+0", i.e. for triggering an immediate shutdown. If no time argument is specified, "+1" is implied.\nOptionally, this may be followed by a wall message to be sent to all logged-in users before going down.', options: { reboot: "Soft reboot.", rebootHard: "Hard reboot.", wall: "Send wall message before power off or reboot.", clear: "Clear all pending shutdowns.", show: "List all pending shutdown actions." }, messages: { "no-pending": "There's no pending shutdown.", "list-header": "Pending shutdowns:", "list-item": { reboot: "Reboot scheduled at: {0}", poweroff: "Power off scheduled at: {0}" }, "invalid-time": "Failed to parse time specification: {0}", clear: "Cleared all pending shutdowns.", reboot: 'Reboot scheduled for {0}, use "shutdown -c" to cancel.', poweroff: 'Power off scheduled for {0}, use "shutdown -c" to cancel.', restarted: "Koishi has been restarted." }, "wall-messages": { clear: "The system shutdown has been cancelled.", reboot: "The system is going down for reboot at {0}.", poweroff: "The system is going down for power off at {0}." } } } };
}
});
// src/index.tsx
var src_exports = {};
__export(src_exports, {
Config: () => Config,
apply: () => apply,
name: () => name
});
module.exports = __toCommonJS(src_exports);
var import_koishi = require("koishi");
var import_jsx_runtime = require("@satorijs/element/jsx-runtime");
var name = "shutdown";
var Config = import_koishi.Schema.object({});
function apply(ctx) {
const pendings = [];
ctx.i18n.define("zh-CN", require_zh_CN());
ctx.i18n.define("en-US", require_en_US());
ctx.command("shutdown [time:string] [wall:text]", { authority: 4 }).alias("exit").option("reboot", "-r", { fallback: false }).option("rebootHard", "-R", { fallback: false }).option("wall", "-w", { fallback: false }).option("clear", "-c", { fallback: false }).option("show", "-s", { fallback: false }).action(({ options, session }, time, wall) => {
if (options.show) {
if (!pendings.length) return session.text(".no-pending");
const result = [session.text(".list-header")];
for (const pending of pendings) {
const action2 = pending.code ? "reboot" : "poweroff";
result.push(session.text(`.list-item.${action2}`, [pending.date.toString()]));
}
return result.join("\n");
}
if (options.clear) {
if (!pendings.length) return session.text(".no-pending");
for (const pending of pendings.splice(0)) {
clearTimeout(pending.timer);
}
if (wall || options.wall) {
ctx.broadcast(wall || /* @__PURE__ */ (0, import_jsx_runtime.jsx)("i18n", { path: "commands.shutdown.wall-messages.clear" }));
}
return session.text(".clear");
}
let parsedTime = parseTime(time || "+1");
if (time === "+0" || time === "now") parsedTime = 0;
else if (parsedTime === false) return session.text(".invalid-time", [time]);
const { isDirect, channelId, guildId, sid } = session;
const code = options.reboot ? 51 : options.rebootHard ? 52 : 0;
const date = new Date((/* @__PURE__ */ new Date()).getTime() + parsedTime);
const action = code ? "reboot" : "poweroff";
const timer = setTimeout(() => {
if (!ctx.loader) process.exit(code);
const content = session.text(".restarted");
ctx.loader.envData.message = { isDirect, channelId, guildId, sid, content };
ctx.loader.fullReload(code);
}, parsedTime);
pendings.push({ timer, code, date });
if (wall || options.wall) {
const path = `commands.shutdown.wall-messages.${action}`;
ctx.broadcast(wall || /* @__PURE__ */ (0, import_jsx_runtime.jsx)("i18n", { path, children: date.toString() }));
}
return session.text("." + action, [date.toString()]);
});
}
__name(apply, "apply");
function parseTime(time) {
if (!time) return false;
const hhmm = parseHhmm(time);
if (hhmm !== false) return hhmm;
return parseMinutes(time);
}
__name(parseTime, "parseTime");
function parseHhmm(time) {
const splits = time.split(":");
if (splits.length !== 2) return false;
const [h, m] = splits.map((x) => +x);
if (h * 0 !== 0 || m * 0 !== 0) return false;
if (h < 0 || m < 0) return false;
if (h > 23 || m > 59) return false;
const date = /* @__PURE__ */ new Date();
date.setHours(h);
date.setMinutes(m);
date.setSeconds(0);
date.setMilliseconds(0);
let dateNum = date.getTime();
const nowNum = (/* @__PURE__ */ new Date()).getTime();
if (dateNum < nowNum) dateNum += 864e5;
return dateNum - nowNum;
}
__name(parseHhmm, "parseHhmm");
function parseMinutes(time) {
if (!time.startsWith("+")) return false;
const num = Number(time);
if (!num || num < 0) return false;
return num * 6e4;
}
__name(parseMinutes, "parseMinutes");
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
Config,
apply,
name
});
//# sourceMappingURL=index.js.map