From 00114fca644358e228b0fef8e15e78f07c817c24 Mon Sep 17 00:00:00 2001 From: Virginia Senioria <91khr@users.noreply.github.com> Date: Sat, 21 Oct 2023 17:33:00 +0800 Subject: [PATCH] feat plugins/auto_reply: use tz name add watch npm script --- package.json | 2 + src/plugins/auto_reply/index.ts | 69 +++++++++++++++++---------------- 2 files changed, 38 insertions(+), 33 deletions(-) diff --git a/package.json b/package.json index fe9682a..aa4bbc7 100644 --- a/package.json +++ b/package.json @@ -8,6 +8,7 @@ "start": "cd dist && node index.js", "preview": "npm run build && npm run start", "build": "tsc && tsc-alias", + "watch": "sh -c '(tsc-alias -w&); tsc -w'", "pretty": "prettier -c .", "pretty:fix": "prettier -w . && eslint . --fix --ext .ts,.js", "lint": "eslint . --ext .ts,.js", @@ -48,6 +49,7 @@ }, "dependencies": { "chalk": "^5.3.0", + "dayjs": "^1.11.10", "js-yaml": "^4.1.0", "node-telegram-bot-api": "^0.61.0", "proxy-agent": "^6.3.1" diff --git a/src/plugins/auto_reply/index.ts b/src/plugins/auto_reply/index.ts index eafd72a..dc7828f 100644 --- a/src/plugins/auto_reply/index.ts +++ b/src/plugins/auto_reply/index.ts @@ -2,24 +2,24 @@ import { App } from '@/types/app.js'; import { PluginInit } from '@/types/plugin.js'; import { getName } from '@/util/string.js'; import { Message } from 'node-telegram-bot-api'; +import dayjs from 'dayjs'; +import dayjs_utc from 'dayjs/plugin/utc.js'; +import dayjs_timezone from 'dayjs/plugin/timezone.js'; + +[dayjs_utc, dayjs_timezone].forEach((it) => dayjs.extend(it)); type Data = { - [user: number]: { - last_reply: Date; - tz: number; - }; + last_reply: { [grp: number]: number }; + tz: string; }; const handleReply = async (app: App, msg: Message) => { - const msgdate = new Date(msg.date * 1000); - await using curdb = await app.db.db('auto_reply'); - const chat = curdb.data[msg.chat.id]; - if (!(msg.from!.id in chat)) { - chat[msg.from!.id] = { last_reply: new Date(0), tz: 0 }; - } - const tz = chat[msg.from!.id].tz; - const msghr = msgdate.getUTCHours() + tz; - const repdate = new Date(chat[msg.from!.id].last_reply); + if (msg.from === undefined) return; + await using db = await app.db.db('auto_reply'); + const usrdb = db.data[msg.from.id]; + const msgdate = dayjs.unix(msg.date).tz(usrdb.tz); + const msghr = msgdate.hour(); + const repdate = dayjs.unix(usrdb.last_reply[msg.chat.id]).tz(usrdb.tz); const reply_invs: [number, number, string][] = [ [22, 23, '很晚了呢, 揉揉${user}, 该睡觉了呢, 不要熬夜哦'], [0, 1, '很晚了呢, 揉揉${user}, 该睡觉了呢, 不要熬夜哦'], @@ -31,23 +31,18 @@ const handleReply = async (app: App, msg: Message) => { }; const reply_it = reply_invs.find(([lo, hi]) => lo <= msghr && msghr <= hi); if (reply_it === undefined) return; - const rephr = repdate.getUTCHours() + tz; + const rephr = repdate.hour(); const [lo, hi, rep] = reply_it; - if ( - lo <= rephr && - rephr <= hi && - msgdate.getTime() - repdate.getTime() <= (hi - lo + 1) * 3600_000 - ) - return; + if (lo <= rephr && rephr <= hi && msgdate.diff(repdate, 'hour') <= hi - lo + 1) return; void app.bot.sendMessage( msg.chat.id, rep.replaceAll(/\${(\w*)}/g, (_, s: string) => vars[s]) ); - chat[msg.from!.id].last_reply = msgdate; + usrdb.last_reply[msg.chat.id] = msgdate.unix(); }; const init: PluginInit = (init_app) => { - init_app.db.register('auto_reply', [() => ({})]); + init_app.db.register('auto_reply', [() => ({ last_reply: {}, tz: 'Asia/Shanghai' })]); init_app.registGlobalMessageHandle({ chat_type: 'all', description: '向用户发送一些自动回复', @@ -55,20 +50,28 @@ const init: PluginInit = (init_app) => { }); init_app.registCommand({ command: 'set_tz', - description: '设置用户当前的时区, 以UTC+n计, 不计算夏令时', + description: '设置用户当前的时区名, 例如, Europe/Rome', handle: async (app, msg, text) => { - await using curdb = await app.db.db('auto_reply'); - const chat = curdb.data[msg.chat.id]; - text = text?.trim(); - if (!(msg.from!.id in chat)) { - chat[msg.from!.id] = { last_reply: new Date(0), tz: 0 }; - } - const tz = text ? parseInt(text, 10) : NaN; - if (isNaN(tz) || tz.toString() !== text) { - void app.bot.sendMessage(msg.chat.id, `'${text}'不是一个有效的十进制数字!`); + if (msg.from === undefined) return; + if (!text) { + void app.bot.sendMessage(msg.chat.id, '需要一个时区名', { + reply_to_message_id: msg.message_id, + }); return; } - chat[msg.from!.id].tz = tz; + text = text.trim(); + try { + dayjs().tz(text); + } catch (e) { + if (e instanceof RangeError) { + void app.bot.sendMessage(msg.chat.id, '无法识别时区名', { + reply_to_message_id: msg.message_id, + }); + return; + } + } + await using db = await app.db.db('auto_reply'); + db.data[msg.from.id].tz = text; void app.bot.sendMessage(msg.chat.id, '时区设置成功', { reply_to_message_id: msg.message_id, });