2021-07-26 19:31:46 +00:00
|
|
|
/*
|
|
|
|
private_reminders.json example:
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"user": "user id",
|
|
|
|
"tz": "Etc/UTC",
|
|
|
|
"hour": 0,
|
|
|
|
"message": "it midnight"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
*/
|
|
|
|
|
|
|
|
const timer = require("../lib/timer");
|
|
|
|
const fs = require("fs");
|
|
|
|
const {resolve} = require("path");
|
|
|
|
|
|
|
|
if (!fs.existsSync(resolve(__dirname, "..", "..", "private_reminders.json")))
|
|
|
|
return;
|
|
|
|
|
|
|
|
const tzFormatterCache = {};
|
|
|
|
const dmCache = {};
|
|
|
|
|
|
|
|
const reminderData = require(resolve(
|
|
|
|
__dirname,
|
|
|
|
"..",
|
|
|
|
"..",
|
|
|
|
"private_reminders.json"
|
|
|
|
));
|
|
|
|
|
|
|
|
hf.database.run(
|
|
|
|
"CREATE TABLE IF NOT EXISTS private_reminders (user STRING NOT NULL PRIMARY KEY, last_run STRING NOT NULL) WITHOUT ROWID"
|
|
|
|
);
|
|
|
|
|
|
|
|
function setLastRun(id, date) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
hf.database.run(
|
|
|
|
"REPLACE INTO private_reminders VALUES ($key,$value)",
|
|
|
|
{
|
|
|
|
$value: date,
|
|
|
|
$key: id,
|
|
|
|
},
|
|
|
|
(err) => {
|
|
|
|
if (err == null) {
|
|
|
|
resolve(true);
|
|
|
|
} else {
|
|
|
|
reject(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function getLastRun(id) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
hf.database.get(
|
|
|
|
"SELECT last_run FROM private_reminders WHERE user = $key",
|
|
|
|
{
|
|
|
|
$key: id,
|
|
|
|
},
|
|
|
|
(err, row) => {
|
|
|
|
if (err == null) {
|
2021-07-27 17:03:55 +00:00
|
|
|
resolve(row?.last_run);
|
2021-07-26 19:31:46 +00:00
|
|
|
} else {
|
|
|
|
reject(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-08-22 20:21:09 +00:00
|
|
|
hf.bot.once("ready", () => {
|
|
|
|
timer.add(
|
|
|
|
"private_reminders",
|
|
|
|
async () => {
|
|
|
|
for (const data of reminderData) {
|
|
|
|
if (!tzFormatterCache[data.tz]) {
|
|
|
|
tzFormatterCache[data.tz] = Intl.DateTimeFormat("en-US", {
|
|
|
|
dateStyle: "short",
|
|
|
|
timeStyle: "short",
|
|
|
|
hour12: false,
|
|
|
|
timeZone: data.tz,
|
|
|
|
});
|
|
|
|
}
|
2021-07-26 19:31:46 +00:00
|
|
|
|
2021-08-22 20:21:09 +00:00
|
|
|
if (!dmCache[data.user]) {
|
|
|
|
dmCache[data.user] = await hf.bot.getDMChannel(data.user);
|
|
|
|
}
|
2021-07-26 19:31:46 +00:00
|
|
|
|
2021-08-22 20:21:09 +00:00
|
|
|
const [date, time] = tzFormatterCache[data.tz]
|
|
|
|
.format(Date.now())
|
|
|
|
.split(", ");
|
|
|
|
let [hour, minutes] = time.split(":");
|
|
|
|
hour = parseInt(hour);
|
|
|
|
minutes = parseInt(minutes);
|
|
|
|
const lastRan = await getLastRun(data.user);
|
2021-07-26 19:31:46 +00:00
|
|
|
|
2021-08-22 20:21:09 +00:00
|
|
|
if (date != lastRan && hour == data.hour && minutes == 0) {
|
|
|
|
dmCache[data.user].createMessage(":alarm_clock: " + data.message);
|
|
|
|
await setLastRun(data.user, date);
|
|
|
|
}
|
2021-07-26 19:31:46 +00:00
|
|
|
}
|
2021-08-22 20:21:09 +00:00
|
|
|
},
|
|
|
|
1000
|
|
|
|
);
|
|
|
|
});
|