koishi-plugin-sleepsign-in
Version:
Sleep check-in plugin
139 lines (137 loc) • 5.7 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 __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/index.ts
var src_exports = {};
__export(src_exports, {
Config: () => Config,
apply: () => apply,
inject: () => inject,
name: () => name
});
module.exports = __toCommonJS(src_exports);
var import_koishi = require("koishi");
var name = "sleepsign-in";
var inject = ["database"];
var Config = import_koishi.Schema.intersect([
import_koishi.Schema.object({
enableSignTime: import_koishi.Schema.boolean().default(false).description("是否自定义签到时间,默认签到时间为晚上22点到明早6点")
}),
import_koishi.Schema.union([
import_koishi.Schema.object({
enableSignTime: import_koishi.Schema.const(true).required(),
signStartTime: import_koishi.Schema.number().min(1).max(24).default(22).description("签到开始时间"),
signEndTime: import_koishi.Schema.number().min(1).max(24).default(6).description("签到结束时间")
}),
import_koishi.Schema.object({})
]).description("签到时间配置"),
import_koishi.Schema.object({
goodNightMsg: import_koishi.Schema.string().default("晚安").description("晚安签到的触发消息"),
missedSignMsg: import_koishi.Schema.string().default("现在不是睡觉时间哦!").description("未到晚安时间的提醒消息"),
repeatSignMsg: import_koishi.Schema.string().default("你已经晚安过了哦!").description("重复晚安的提醒消息"),
succeedMsg: import_koishi.Schema.string().default("晚安!你是第-rank个晚安的群友哦!").description("晚安成功的提醒消息,-rank为排名,-time为晚安时间")
}).description("晚安消息配置")
]);
function apply(ctx, config) {
const logger = ctx.logger(ctx.name);
if (!ctx.database.tables.user_sign_in) {
ctx.database.extend("user_sign_in", {
id: {
type: "unsigned",
initial: 1
},
user_id: "string",
count: "integer",
sign_time: "string"
}, {
primary: "id",
autoInc: true
});
}
ctx.middleware(async (session, next) => {
let date = /* @__PURE__ */ new Date();
if (session.content === config.goodNightMsg) {
if (!isDuringSignTime(date, config))
return (0, import_koishi.h)("at", { id: session.userId }) + " " + config.missedSignMsg;
try {
let dateStr = date.toLocaleString();
const rows = await ctx.database.get("user_sign_in", {
user_id: session.userId
});
if (!(rows.length > 0)) {
await ctx.database.create("user_sign_in", {
user_id: session.userId,
count: 1,
sign_time: dateStr
});
} else {
if (!isDuringSignTime(new Date(rows[0].sign_time), config))
return (0, import_koishi.h)("at", { id: session.userId }) + " " + config.repeatSignMsg;
await ctx.database.set("user_sign_in", {
user_id: session.userId
}, {
count: rows[0].count + 1,
sign_time: dateStr
});
}
const succeedMsg = config.succeedMsg.replace("-rank", (await ctx.database.get("user_sign_in", { sign_time: dateStr })).length.toString()).replace("-time", date.toLocaleTimeString([], { hour: "2-digit", minute: "2-digit" }));
return succeedMsg;
} catch (err) {
logger.error("晚安失败,原因:" + err);
return (0, import_koishi.h)("at", { id: session.userId }) + " 晚安失败,请稍后再试";
}
} else {
return next();
}
});
ctx.command("查询晚安次数").action(async ({ session }) => {
try {
const rows = await ctx.database.get("user_sign_in", {
user_id: session.userId
});
if (rows.length > 0) {
await session.send((0, import_koishi.h)("at", { id: session.userId }) + " 你已经晚安" + rows[0].count + "次了");
} else {
await session.send((0, import_koishi.h)("at", { id: session.userId }) + " 你还没有晚安过呢");
}
} catch (err) {
logger.error("查询失败,原因:" + err);
await session.send((0, import_koishi.h)("at", { id: session.userId }) + " 查询失败,请稍后再试");
}
});
}
__name(apply, "apply");
function isDuringSignTime(date, config) {
let startTime = new Date(date.getFullYear(), date.getMonth() + 1, date.getDate(), config.signStartTime, 0, 0);
let endTime = new Date(date.getFullYear(), date.getMonth() + 1, date.getDate(), config.signEndTime, 0, 0);
const t = startTime > endTime;
if (t)
[startTime, endTime] = [endTime, startTime];
if (date >= startTime && date <= endTime)
return t ? false : true;
else
return t ? true : false;
}
__name(isDuringSignTime, "isDuringSignTime");
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
Config,
apply,
inject,
name
});