Skip to content

Commit

Permalink
feat plugins/auto_reply: use tz name
Browse files Browse the repository at this point in the history
add watch npm script
  • Loading branch information
91khr committed Oct 21, 2023
1 parent 543395e commit e0160a5
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 34 deletions.
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -48,8 +49,11 @@
},
"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"
"proxy-agent": "^6.3.1",
"timezone": "link:dayjs/plugin/timezone",
"utc": "link:dayjs/plugin/utc"
}
}
69 changes: 36 additions & 33 deletions src/plugins/auto_reply/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Data>('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<Data>('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}, 该睡觉了呢, 不要熬夜哦'],
Expand All @@ -31,44 +31,47 @@ 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: '向用户发送一些自动回复',
handle: handleReply,
});
init_app.registCommand({
command: 'set_tz',
description: '设置用户当前的时区, 以UTC+n计, 不计算夏令时',
description: '设置用户当前的时区名, 例如, Europe/Rome',
handle: async (app, msg, text) => {
await using curdb = await app.db.db<Data>('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<Data>('auto_reply');
db.data[msg.from.id].tz = text;
void app.bot.sendMessage(msg.chat.id, '时区设置成功', {
reply_to_message_id: msg.message_id,
});
Expand Down

0 comments on commit e0160a5

Please sign in to comment.