From fecd6877ea3c9052eb4d9097c6bbd6cc08b23429 Mon Sep 17 00:00:00 2001 From: SileNce5k Date: Fri, 2 Jun 2023 11:15:47 +0200 Subject: [PATCH] Rewrite timers.js to use database --- commands/misc/timers.js | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/commands/misc/timers.js b/commands/misc/timers.js index eb44bed..9917dfe 100644 --- a/commands/misc/timers.js +++ b/commands/misc/timers.js @@ -1,13 +1,33 @@ +const sqlite3 = require('sqlite3').verbose(); module.exports = { name: "timers", - description: "Check your own timers", - execute({client, message}) { + description: "List all your timers.", + async execute({message}) { let authorTimers = ""; - client.timers.forEach(timer => { - if(timer.user === message.author.id) - authorTimers += `${timer.ID} : `; + 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) + authorTimers += `${timer.ID} : \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); } }; \ No newline at end of file