Skip to content

Commit

Permalink
feat plugins/auto_reply: set_tz
Browse files Browse the repository at this point in the history
  • Loading branch information
91khr committed Oct 20, 2023
1 parent 47b5b78 commit 543395e
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions src/plugins/auto_reply/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@ import { Message } from 'node-telegram-bot-api';
type Data = {
[user: number]: {
last_reply: Date;
tz: number;
};
};

const handleReply = async (app: App, msg: Message) => {
const msgdate = new Date(msg.date * 1000);
const msghr = msgdate.getHours();
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) };
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);
const reply_invs: [number, number, string][] = [
[22, 23, '很晚了呢, 揉揉${user}, 该睡觉了呢, 不要熬夜哦'],
Expand All @@ -29,7 +31,7 @@ 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.getHours();
const rephr = repdate.getUTCHours() + tz;
const [lo, hi, rep] = reply_it;
if (
lo <= rephr &&
Expand All @@ -51,6 +53,27 @@ const init: PluginInit = (init_app) => {
description: '向用户发送一些自动回复',
handle: handleReply,
});
init_app.registCommand({
command: 'set_tz',
description: '设置用户当前的时区, 以UTC+n计, 不计算夏令时',
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}'不是一个有效的十进制数字!`);
return;
}
chat[msg.from!.id].tz = tz;
void app.bot.sendMessage(msg.chat.id, '时区设置成功', {
reply_to_message_id: msg.message_id,
});
},
});
};

export { init };

0 comments on commit 543395e

Please sign in to comment.