koishi-plugin-countdown-clock 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,8 @@
1
+ import { Context } from "koishi";
2
+ import { CountDownData, Time } from "../types/countdown";
3
+ export declare function addCountDown(ctx: Context, uid: number, platform: string, channel: string, time: Time, text?: string): Promise<CountDownData>;
4
+ export declare function initCountDown(ctx: Context): Promise<void>;
5
+ export declare function getCountDown(ctx: Context, uid: number): CountDownData;
6
+ export declare function removeCountDown(ctx: Context, countDownOrId: number | CountDownData): Promise<false | import("minato").Driver.WriteResult>;
7
+ export declare function getAllCountDown(): CountDownData[];
8
+ export declare function getAndRemoveTimeUpCountDown(ctx: Context): Promise<CountDownData[]>;
@@ -0,0 +1,2 @@
1
+ import { Context } from "koishi";
2
+ export declare function init(ctx: Context): Promise<void>;
@@ -0,0 +1,2 @@
1
+ import { Context } from "koishi";
2
+ export declare function registerCancelCommand(ctx: Context): void;
@@ -0,0 +1,2 @@
1
+ import { Context } from "koishi";
2
+ export declare function registerCountDownCommand(ctx: Context): void;
package/lib/index.d.ts ADDED
@@ -0,0 +1,9 @@
1
+ import { Context, Schema } from 'koishi';
2
+ export declare const name = "countdown";
3
+ export interface Config {
4
+ }
5
+ export declare const Config: Schema<Config>;
6
+ export declare const inject: {
7
+ required: string[];
8
+ };
9
+ export declare function apply(ctx: Context): void;
package/lib/index.js ADDED
@@ -0,0 +1,224 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
6
+ var __commonJS = (cb, mod) => function __require() {
7
+ return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
8
+ };
9
+ var __export = (target, all) => {
10
+ for (var name2 in all)
11
+ __defProp(target, name2, { get: all[name2], enumerable: true });
12
+ };
13
+ var __copyProps = (to, from, except, desc) => {
14
+ if (from && typeof from === "object" || typeof from === "function") {
15
+ for (let key of __getOwnPropNames(from))
16
+ if (!__hasOwnProp.call(to, key) && key !== except)
17
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
18
+ }
19
+ return to;
20
+ };
21
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
22
+
23
+ // src/locales/zh-CN.yml
24
+ var require_zh_CN = __commonJS({
25
+ "src/locales/zh-CN.yml"(exports2, module2) {
26
+ module2.exports = { time: { s: "秒", m: "分钟", h: "小时", d: "天" }, countdown: { timeup: "<at id={uid}/> {text} 时间到", "default-text": "倒计时" }, commands: { countdown: { description: "开始一个倒计时", messages: { countdown: "<at id={uid}/> {text}剩余{time}", "no-countdown": "<at id={uid}/> 当前没有正在运行的倒计时", "time-invalid": "<at id={uid}/> 时间格式错误", "has-countdown": "<at id={uid}/> 当前有倒计时正在运行", success: "<at id={uid}/> {text}将在{time}后提醒你" }, cancel: { description: "取消一个倒计时", messages: { success: "<at id={uid}/> 倒计时已取消", "no-countdown": "<at id={uid}/> 当前没有正在运行的倒计时" } } } } };
27
+ }
28
+ });
29
+
30
+ // src/index.ts
31
+ var src_exports = {};
32
+ __export(src_exports, {
33
+ Config: () => Config,
34
+ apply: () => apply,
35
+ inject: () => inject,
36
+ name: () => name
37
+ });
38
+ module.exports = __toCommonJS(src_exports);
39
+ var import_koishi = require("koishi");
40
+
41
+ // src/utils/time.ts
42
+ var units = ["d", "h", "m", "s"];
43
+ var uTransform = {
44
+ 分钟: "m",
45
+ 分: "m",
46
+ 小时: "h",
47
+ 天: "d",
48
+ 秒: "s",
49
+ 时: "h"
50
+ };
51
+ function getTimeStrSess(session, time) {
52
+ return units.map((unit) => time[unit] ? time[unit] + session.text(`time.${unit}`) : "").filter((x) => x).join(" ");
53
+ }
54
+ __name(getTimeStrSess, "getTimeStrSess");
55
+ function parseTimeStr(str) {
56
+ if (!/^(([0-9]+)([hmsdHMSD]|分钟?|小?时|秒|天))+$/ig.test(str))
57
+ return null;
58
+ const matches = str.matchAll(/([0-9]+)([hmsdHMSD]|分钟?|小?时|秒|天)/ig);
59
+ const time = {};
60
+ for (const match of matches) {
61
+ let unit = match[2].toLowerCase();
62
+ if (uTransform[unit])
63
+ unit = uTransform[unit];
64
+ if (!units.includes(unit))
65
+ return null;
66
+ time[unit] = parseInt(match[1]);
67
+ }
68
+ return time;
69
+ }
70
+ __name(parseTimeStr, "parseTimeStr");
71
+ function getTimeAfter(time) {
72
+ const now = /* @__PURE__ */ new Date();
73
+ return new Date(now.getTime() + (time.s || 0) * 1e3 + (time.m || 0) * 60 * 1e3 + (time.h || 0) * 60 * 60 * 1e3 + (time.d || 0) * 24 * 60 * 60 * 1e3);
74
+ }
75
+ __name(getTimeAfter, "getTimeAfter");
76
+ function getTimeTo(end) {
77
+ const now = /* @__PURE__ */ new Date();
78
+ const delta = end.getTime() - now.getTime();
79
+ return {
80
+ s: Math.floor(delta / 1e3 % 60),
81
+ m: Math.floor(delta / 1e3 / 60 % 60),
82
+ h: Math.floor(delta / 1e3 / 60 / 60 % 24),
83
+ d: Math.floor(delta / 1e3 / 60 / 60 / 24)
84
+ };
85
+ }
86
+ __name(getTimeTo, "getTimeTo");
87
+
88
+ // src/data/countdowns.ts
89
+ var CountDownStore = [];
90
+ async function addCountDown(ctx, uid, platform, channel, time, text) {
91
+ text = text || "";
92
+ const until = getTimeAfter(time);
93
+ const model = await ctx.model.create("countdown", { uid, platform, channel, until, time, text });
94
+ CountDownStore.push(model);
95
+ return model;
96
+ }
97
+ __name(addCountDown, "addCountDown");
98
+ function getCountDown(ctx, uid) {
99
+ return CountDownStore.find((x) => x.uid === uid);
100
+ }
101
+ __name(getCountDown, "getCountDown");
102
+ async function removeCountDown(ctx, countDownOrId) {
103
+ const id = typeof countDownOrId === "number" ? countDownOrId : countDownOrId.id;
104
+ if (!id || !CountDownStore.find((x) => x.id === id))
105
+ return false;
106
+ CountDownStore.splice(CountDownStore.findIndex((x) => x.id === id), 1);
107
+ return await ctx.model.remove("countdown", { id });
108
+ }
109
+ __name(removeCountDown, "removeCountDown");
110
+ async function getAndRemoveTimeUpCountDown(ctx) {
111
+ const now = /* @__PURE__ */ new Date();
112
+ const res = CountDownStore.filter((x) => x.until < now);
113
+ res.forEach((x) => removeCountDown(ctx, x));
114
+ return res;
115
+ }
116
+ __name(getAndRemoveTimeUpCountDown, "getAndRemoveTimeUpCountDown");
117
+
118
+ // src/job/checkCountDown.ts
119
+ function registerCheckDown(ctx) {
120
+ ctx.setInterval(() => check(ctx), 1e3);
121
+ }
122
+ __name(registerCheckDown, "registerCheckDown");
123
+ async function check(ctx) {
124
+ const removed = await getAndRemoveTimeUpCountDown(ctx);
125
+ for (const countDown of removed) {
126
+ sendMessage(ctx, countDown.platform, countDown.channel, countDown.uid.toString(), "countdown.timeup", { uid: countDown.uid, text: countDown.text });
127
+ }
128
+ }
129
+ __name(check, "check");
130
+ async function sendMessage(ctx, platform, channel, uid, messageKey, param) {
131
+ const channelCheks = await Promise.all(ctx.bots.filter((t) => t.platform == platform).map((t) => ({
132
+ ch: t.getChannel(channel),
133
+ bot: t
134
+ })));
135
+ const bot = channelCheks.find((t) => t.ch)?.bot;
136
+ if (!bot) {
137
+ return false;
138
+ }
139
+ const user = await ctx.database.getUser(platform, uid);
140
+ const messageTxt = ctx.i18n.render(user.locales, [messageKey], param);
141
+ bot.sendMessage(channel, messageTxt);
142
+ }
143
+ __name(sendMessage, "sendMessage");
144
+
145
+ // src/features/countdown.ts
146
+ function registerCountDownCommand(ctx) {
147
+ ctx.command("countdown [time:string] [text:string]").action(async (args, time, text) => await process(ctx, args.session, time, text));
148
+ }
149
+ __name(registerCountDownCommand, "registerCountDownCommand");
150
+ async function process(ctx, session, _time, text) {
151
+ const ctd = await getCountDown(ctx, session.userId);
152
+ if (!_time) {
153
+ return ctd && ctd.until.getTime() > Date.now() ? session.text(".countdown", { time: getTimeStrSess(session, getTimeTo(ctd.until)), text: ctd.text }) : session.text(".no-countdown");
154
+ } else if (ctd)
155
+ return session.text(".has-countdown");
156
+ text = text || session.text("countdown.default-text");
157
+ const time = parseTimeStr(_time);
158
+ if (!time)
159
+ return session.text(".time-invalid");
160
+ await addCountDown(ctx, session.userId, session.platform, session.channelId, time, text);
161
+ return session.text(".success", { time: getTimeStrSess(session, time), text });
162
+ }
163
+ __name(process, "process");
164
+
165
+ // src/features/cancel.ts
166
+ function registerCancelCommand(ctx) {
167
+ ctx.command("countdown.cancel").action(async (args) => await process2(ctx, args.session));
168
+ }
169
+ __name(registerCancelCommand, "registerCancelCommand");
170
+ async function process2(ctx, session) {
171
+ const ctd = await getCountDown(ctx, session.userId);
172
+ if (!ctd)
173
+ return session.text(".no-countdown");
174
+ await removeCountDown(ctx, ctd);
175
+ return session.text(".success");
176
+ }
177
+ __name(process2, "process");
178
+
179
+ // src/reg.ts
180
+ var T = [
181
+ registerCheckDown,
182
+ registerCountDownCommand,
183
+ registerCancelCommand
184
+ ];
185
+ function registerAll(ctx) {
186
+ return Promise.all(T.map((t) => t(ctx)).filter((t) => typeof t === "object" && t.then));
187
+ }
188
+ __name(registerAll, "registerAll");
189
+
190
+ // src/data/database.ts
191
+ async function init(ctx) {
192
+ ctx.model.extend("countdown", {
193
+ id: "unsigned",
194
+ uid: "unsigned",
195
+ channel: "string",
196
+ platform: "string",
197
+ text: "string",
198
+ until: "timestamp",
199
+ time: "json"
200
+ }, {
201
+ autoInc: true
202
+ });
203
+ }
204
+ __name(init, "init");
205
+
206
+ // src/index.ts
207
+ var name = "countdown";
208
+ var Config = import_koishi.Schema.object({});
209
+ var inject = {
210
+ required: ["database"]
211
+ };
212
+ function apply(ctx) {
213
+ init(ctx);
214
+ registerAll(ctx);
215
+ ctx.i18n.define("zh-CN", require_zh_CN());
216
+ }
217
+ __name(apply, "apply");
218
+ // Annotate the CommonJS export names for ESM import in node:
219
+ 0 && (module.exports = {
220
+ Config,
221
+ apply,
222
+ inject,
223
+ name
224
+ });
@@ -0,0 +1,2 @@
1
+ import { Context } from "koishi";
2
+ export declare function registerCheckDown(ctx: Context): void;
package/lib/reg.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ import { Context } from "koishi";
2
+ export type FeatureRegister = (ctx: Context) => Promise<void> | any;
3
+ export declare function registerAll(ctx: Context): Promise<any[]>;
@@ -0,0 +1,15 @@
1
+ export type CountDownData = {
2
+ id: number;
3
+ uid: number;
4
+ channel: string;
5
+ platform: string;
6
+ until: Date;
7
+ text: string;
8
+ time: Time;
9
+ };
10
+ export type Time = {
11
+ s?: number;
12
+ m?: number;
13
+ h?: number;
14
+ d?: number;
15
+ };
@@ -0,0 +1,7 @@
1
+ import { Context, Session } from "koishi";
2
+ import { Time } from "../types/countdown";
3
+ export declare function getTimeStrSess(session: Session, time: Time): any;
4
+ export declare function getTimeStrCtx(ctx: Context, locales: string[], time: Time): any;
5
+ export declare function parseTimeStr(str: string): Time;
6
+ export declare function getTimeAfter(time: Time): Date;
7
+ export declare function getTimeTo(end: Date): Time;
package/package.json ADDED
@@ -0,0 +1,39 @@
1
+ {
2
+ "name": "koishi-plugin-countdown-clock",
3
+ "description": "Setup an countdown clock and notify the user after a period of time.",
4
+ "version": "1.0.0",
5
+ "main": "lib/index.js",
6
+ "typings": "lib/index.d.ts",
7
+ "files": [
8
+ "lib",
9
+ "dist"
10
+ ],
11
+ "license": "MIT",
12
+ "scripts": {},
13
+ "keywords": [
14
+ "chatbot",
15
+ "koishi",
16
+ "plugin"
17
+ ],
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "git+https://github.com/zxy19/koishi-countdown.git"
21
+ },
22
+ "devDependencies": {
23
+ "@koishijs/plugin-database-sqlite": "*"
24
+ },
25
+ "peerDependencies": {
26
+ "koishi": "^4.18.7"
27
+ },
28
+ "koishi": {
29
+ "description": {
30
+ "en": "Setup an countdown clock and notify the user after a period of time.",
31
+ "zh": "设置一个倒计时,用于在一段时间后提醒用户"
32
+ },
33
+ "service": {
34
+ "required": [
35
+ "database"
36
+ ]
37
+ }
38
+ }
39
+ }
package/readme.md ADDED
@@ -0,0 +1,5 @@
1
+ # koishi-plugin-countdown
2
+
3
+ [![npm](https://img.shields.io/npm/v/koishi-plugin-countdown?style=flat-square)](https://www.npmjs.com/package/koishi-plugin-countdown)
4
+
5
+ Setup an countdown clock and notify the user after a period of time.