Use an sqlite3 database for timer feature
There were some other small edits as well, mostly formatting or better logging in certain functions
This commit is contained in:
parent
a098ab6616
commit
d41b28ec91
13 changed files with 2277 additions and 59 deletions
|
@ -1,15 +1,37 @@
|
|||
const sendTimerReminder = require('./sendTimerReminder')
|
||||
const fs = require('fs')
|
||||
module.exports = function (client) {
|
||||
const sqlite3 = require('sqlite3').verbose();
|
||||
module.exports = async function (client) {
|
||||
const checkTimer = require('./checkTimer')
|
||||
const db = new sqlite3.Database('data/database.db')
|
||||
let currentUnixTime = Math.floor(new Date() / 1000);
|
||||
await new Promise((resolve, reject) => {
|
||||
db.get(
|
||||
`SELECT * FROM timers WHERE reminderTime <= ? AND hasPassed = ?`,
|
||||
[currentUnixTime, false],
|
||||
function (error, timer) {
|
||||
if (error) {
|
||||
console.error(error);
|
||||
reject(error);
|
||||
} else {
|
||||
if (timer !== undefined) {
|
||||
db.run(`UPDATE timers SET hasPassed = ? WHERE ID = ?`, [true, timer.ID],
|
||||
function (error) {
|
||||
if (error) {
|
||||
console.error(`Error while updating ${timer.ID} setPassed to true`, error);
|
||||
reject(error);
|
||||
} else {
|
||||
console.log(`Updated ${timer.ID}, set hasPassed to true.`);
|
||||
}
|
||||
})
|
||||
sendTimerReminder(client, timer);
|
||||
}
|
||||
|
||||
for(let i = 0; i < client.timers.length; i++){
|
||||
if(parseInt(client.timers[i].reminderDate) <= Math.floor(new Date() / 1000)){
|
||||
sendTimerReminder(client, client.timers[i]);
|
||||
client.timers.splice(i, 1);
|
||||
i--
|
||||
fs.writeFileSync('data/timers.json', JSON.stringify(client.timers, null, 4))
|
||||
}
|
||||
}
|
||||
db.close();
|
||||
resolve();
|
||||
}
|
||||
}
|
||||
);
|
||||
})
|
||||
setTimeout(checkTimer, 1000, client);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue