Rewrite timers.js to use database

This commit is contained in:
SileNce5k 2023-06-02 11:15:47 +02:00
parent b0e9f88879
commit fecd6877ea
No known key found for this signature in database
GPG key ID: 961132EB78C8915F

View file

@ -1,13 +1,33 @@
const sqlite3 = require('sqlite3').verbose();
module.exports = { module.exports = {
name: "timers", name: "timers",
description: "Check your own timers", description: "List all your timers.",
execute({client, message}) { async execute({message}) {
let authorTimers = ""; let authorTimers = "";
client.timers.forEach(timer => { let sendText = "";
const db = new sqlite3.Database('data/database.db');
await new Promise((resolve, reject) => {
db.all(`SELECT * FROM timers WHERE user = ? AND hasPassed = ?`, [message.author.id, false], function (error, timers){
if(error){
console.error("Error while trying to read timer from database: ", error)
sendText = "An error occured while trying to read timer from database. Check console.";
reject(error);
}else{
timers.forEach(timer => {
if(timer.user === message.author.id) if(timer.user === message.author.id)
authorTimers += `${timer.ID} : <t:${timer.reminderDate}:R>`; authorTimers += `${timer.ID} : <t:${timer.reminderTime.toFixed(0)}:R>\n`;
});
if(authorTimers === ""){
sendText = "You have no timers set.";
}else if(sendText === ""){
sendText = `Your timers:\n${authorTimers}`;
}
resolve();
}
});
}); });
let sendText = "" === authorTimers ? `You have no timers` : `Your timers are:\n${authorTimers}`
message.channel.send(sendText); message.channel.send(sendText);
} }
}; };