make private daily reminders system

maybe one day ill make a full remind system, who knows
This commit is contained in:
Cynthia Foxwell 2021-07-26 13:31:46 -06:00
parent 1b03d4093a
commit 733c690ec7
2 changed files with 105 additions and 1 deletions

3
.gitignore vendored
View file

@ -4,4 +4,5 @@ config.json
config.prod.json
config.testing.json
apikeys.json
database.db
database.db
private_reminders.json

View file

@ -0,0 +1,103 @@
/*
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) {
resolve(row?.value);
} else {
reject(err);
}
}
);
});
}
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,
});
}
if (!dmCache[data.user]) {
dmCache[data.user] = await hf.bot.getDMChannel(data.user);
}
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);
if (date != lastRan && hour == data.hour && minutes == 0) {
dmCache[data.user].createMessage(":alarm_clock: " + data.message);
await setLastRun(data.user, date);
}
}
},
1000
);