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);
|
||||
}
|
||||
|
|
33
util/timer/convertJSONToSQL.js
Normal file
33
util/timer/convertJSONToSQL.js
Normal file
|
@ -0,0 +1,33 @@
|
|||
const fs = require('fs');
|
||||
const sqlite3 = require('sqlite3').verbose();
|
||||
module.exports = async function () {
|
||||
const timers = require('../../data/timers.json')
|
||||
const db = new sqlite3.Database('data/database.db');
|
||||
return new Promise((resolve, reject) => {
|
||||
for (let i = 0; i < timers.length; i++) {
|
||||
let user = timers[i].user;
|
||||
let reminderTime = timers[i].reminderDate;
|
||||
let channel = timers[i].channel;
|
||||
let customMessage = timers[i].customMessage;
|
||||
let hasPassed = false;
|
||||
|
||||
db.run(`INSERT INTO timers (
|
||||
user,
|
||||
reminderTime,
|
||||
channel,
|
||||
customMessage,
|
||||
hasPassed
|
||||
) VALUES (?, ?, ?, ?, ?)`, [user, reminderTime, channel, customMessage, hasPassed], function (error) {
|
||||
if (error) {
|
||||
console.error(`Error while converting timers.json to SQL: ${error}`)
|
||||
reject(error);
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
}
|
||||
db.close();
|
||||
console.log("Converted timers.json to SQL successfully.");
|
||||
resolve();
|
||||
})
|
||||
}
|
|
@ -1,7 +1,8 @@
|
|||
const fs = require('fs');
|
||||
const parseTime = require('./parseTime');
|
||||
|
||||
module.exports = function (client, message, args, compatibility) {
|
||||
const sqlite3 = require('sqlite3').verbose();
|
||||
module.exports = async function (message, args, compatibility) {
|
||||
const databasePath = 'data/database.db'
|
||||
if (args.length < 2)
|
||||
return message.channel.send("Please specify a time, and a message to send after the timer has finished");
|
||||
let currentUnixTime = Math.floor(new Date() / 1000);
|
||||
|
@ -11,16 +12,52 @@ module.exports = function (client, message, args, compatibility) {
|
|||
}
|
||||
let customMessage = compatibility ? args.slice(1).join(" ") : args.slice(2).join(" ");
|
||||
let reminderTime = currentUnixTime + timeInSeconds
|
||||
let newTimerID = ++client.lastTimerID;
|
||||
const newTimer = {
|
||||
"ID": newTimerID,
|
||||
"user": `${message.author.id}`,
|
||||
"reminderDate": reminderTime,
|
||||
"channel": `${message.channel.id}`,
|
||||
"customMessage": `${customMessage}`
|
||||
}
|
||||
fs.writeFileSync('data/lastTimerID.txt', newTimerID.toString());
|
||||
client.timers.push(newTimer);
|
||||
fs.writeFileSync('data/timers.json', JSON.stringify(client.timers, null, 4))
|
||||
return `A new timer with ID:${newTimerID} created.\nI will remind you <t:${reminderTime.toFixed(0)}:R> (<t:${reminderTime.toFixed(0)}:f>)`
|
||||
let newTimerID;
|
||||
const db = new sqlite3.Database(databasePath)
|
||||
let sendText = await new Promise((resolve, reject)=>{
|
||||
db.run(`INSERT INTO timers (
|
||||
user,
|
||||
reminderTime,
|
||||
channel,
|
||||
customMessage,
|
||||
hasPassed
|
||||
) VALUES (?, ?, ?, ?, ?)`,
|
||||
[
|
||||
message.author.id,
|
||||
reminderTime,
|
||||
message.channel.id,
|
||||
customMessage,
|
||||
false
|
||||
], function (error) {
|
||||
if (error) {
|
||||
console.error(error)
|
||||
let sendText = "Error while creating the timer. Check console.";
|
||||
reject(sendText);
|
||||
}else{
|
||||
let lastRowID = this.lastID;
|
||||
db.get(
|
||||
`SELECT id FROM timers WHERE rowid = ?`,
|
||||
lastRowID,
|
||||
function (error, row) {
|
||||
if (error) {
|
||||
let sendText = "Error while getting the ID of the new timer. Check console.";
|
||||
console.error(error);
|
||||
reject(sendText);
|
||||
} else {
|
||||
newTimerID = row.ID;
|
||||
let sendText = `A new timer with ID:${newTimerID} created.\nI will remind you <t:${reminderTime.toFixed(0)}:R> (<t:${reminderTime.toFixed(0)}:f>)`
|
||||
console.log(`New timer with ID:${newTimerID} created.`);
|
||||
resolve(sendText);
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
});
|
||||
})
|
||||
console.log(sendText);
|
||||
db.close();
|
||||
|
||||
|
||||
return sendText;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -1,12 +1,25 @@
|
|||
const fs = require('fs');
|
||||
|
||||
module.exports = function (client, authorID, timerID) {
|
||||
let timerToShow = client.timers.find(timer => timer.ID === parseInt(timerID));
|
||||
if (timerToShow === undefined)
|
||||
return "Timer not found";
|
||||
if (timerToShow.user !== authorID){
|
||||
return "You can only show info about your own timers.";
|
||||
}
|
||||
return `${timerToShow.ID} will remind you <t:${timerToShow.reminderDate.toFixed(0)}:R> (<t:${timerToShow.reminderDate.toFixed(0)}:f>) with the message "${timerToShow.customMessage}"`;
|
||||
|
||||
const sqlite3 = require('sqlite3').verbose();
|
||||
module.exports = async function (authorID, timerID) {
|
||||
const databasePath = `data/database.db`;
|
||||
const db = new sqlite3.Database(databasePath);
|
||||
let sendText = "";
|
||||
await new Promise((resolve, reject) => {
|
||||
db.get(`SELECT * FROM timers WHERE ID = ? AND user = ?`, [timerID, authorID],
|
||||
function (error, timer){
|
||||
if(error){
|
||||
sendText = "An error occured while trying to read timer from database. Check console.";
|
||||
console.error("Error while trying to read timer from database: ", error)
|
||||
reject(error);
|
||||
}else{
|
||||
if(timer === undefined){
|
||||
sendText = "Timer not found";
|
||||
}else{
|
||||
sendText = `${timer.ID} will remind you <t:${timer.reminderDate.toFixed(0)}:R> (<t:${timer.reminderDate.toFixed(0)}:f>)"`;
|
||||
}
|
||||
resolve();
|
||||
}
|
||||
})
|
||||
})
|
||||
db.close();
|
||||
return sendText;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue