Skip to content

Commit

Permalink
[Dev] Randomify auto_reply
Browse files Browse the repository at this point in the history
  • Loading branch information
Lhcfl committed Nov 30, 2023
1 parent 6c221f4 commit 7a68645
Showing 1 changed file with 54 additions and 7 deletions.
61 changes: 54 additions & 7 deletions src/plugins/auto_reply/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,71 @@ import dayjs_timezone from 'dayjs/plugin/timezone.js';

[dayjs_utc, dayjs_timezone].forEach((it) => dayjs.extend(it));

const reply_invs: [number, number, string[]][] = [
[
22,
23,
[
'很晚了呢, 揉揉${user}, 该睡觉了呢, 不要熬夜哦',
'咕咕晚上好!${user} 该睡觉了,不要熬夜哦!',
'晚上好喵!${user} 有点晚了,记得早睡哦!',
],
],
[
0,
1,
[
'很晚了呢, 揉揉${user}, 该睡觉了呢, 不要熬夜哦',
'已经第二天了呢, 揉揉${user}, 该睡觉了呢, 不要熬夜哦',
'夜深了,揉揉${user}, 不要熬夜哦,早点睡觉哦',
],
],
[
2,
4,
[
'是凌晨了呢, 揉揉抱抱${user}, 要注意身体呀, 记得睡觉的说',
'晚安喵~ ${user} 很晚了,记得早点休息哦!',
'嗨呀!都凌晨了qaq ${user} 要放下手机早点入睡哦!',
'很晚很晚了呢,${user},记得要好好休息哦!',
'真的很晚了呢, 揉揉抱抱${user}, 要注意身体呀, 记得睡觉的说',
'该睡觉了呜…… ${user}, 要注意身体呀,现在真的很晚了',
],
],
[
7,
10,
[
'早安, ${user}~ 新的一天也会有新的美好的~',
'早安,${user}大人!今天会是充满活力的一天呢~',
'嗨, ${user}! 早晨的新鲜空气总是令人振奋呢,希望你能有一个美好的一天',
'${user}酱,睡得如何喵?期待与您一起迎接新的一天。',
'早安喵~ ${user}酱!今天要元气满满哦!',
],
],
];

function getSample<T>(a: T[]) {
return a[Math.floor(Math.random() * a.length)];
}

type Data = {
last_reply: { [grp: number]: number };
tz: string;
};

const handleReply = async (app: App, msg: Message) => {
if (msg.from === undefined) return;

// We expect Linquebot to auto-reply more randomly to prevent sudden interruptions.
if (Math.random() > 0.1) 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}, 该睡觉了呢, 不要熬夜哦'],
[2, 4, '是凌晨了呢, 揉揉抱抱${user}, 要注意身体呀, 记得睡觉的说'],
[7, 10, '早安, ${user}~ 新的一天也会有新的美好的~'],
];

const vars: { [k: string]: string } = {
user: getName(msg.from),
};
Expand All @@ -36,7 +83,7 @@ const handleReply = async (app: App, msg: Message) => {
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])
getSample(rep).replaceAll(/\${(\w*)}/g, (_, s: string) => vars[s])
);
usrdb.last_reply[msg.chat.id] = msgdate.unix();
};
Expand Down

0 comments on commit 7a68645

Please sign in to comment.