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,11 +1,32 @@
|
|||
const fs = require('fs');
|
||||
const sqlite3 = require('sqlite3').verbose();
|
||||
module.exports = async function (authorID, timerID) {
|
||||
const db = new sqlite3.Database('data/database.db')
|
||||
let sendText = "";
|
||||
await new Promise((resolve, reject) => {
|
||||
db.all('SELECT * FROM timers WHERE id = ? AND user = ? AND hasPassed = ?', [parseInt(timerID), authorID, false], (err, rows) => {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
return;
|
||||
}
|
||||
if (rows.length > 1) {
|
||||
sendText = "More than one timer has this ID"
|
||||
} else if (rows.length === 0) {
|
||||
sendText = `A timer with the ID ${timerID} was not found.\n`
|
||||
} else {
|
||||
db.run('UPDATE timers SET hasPassed = ? WHERE ID = ? AND user = ?', [true, parseInt(timerID), authorID], function (err) {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
sendText = "Updating timers failed. Check console.";
|
||||
}
|
||||
else {
|
||||
sendText = `Timer with ID:${timerID} deleted.`;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = function (client, authorID, timerID) {
|
||||
});
|
||||
})
|
||||
|
||||
let timerToDelete = client.timers.find(timer => timer.ID === parseInt(timerID) && timer.user === authorID);
|
||||
if (timerToDelete === undefined)
|
||||
return "Timer not found";
|
||||
client.timers.splice(client.timers.indexOf(timerToDelete), 1);
|
||||
fs.writeFileSync('data/timers.json', JSON.stringify(client.timers, null, 4))
|
||||
return `Timer with ID:${timerID} deleted.`;
|
||||
db.close();
|
||||
return sendText;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue