Allow dates to be used in timer command
All checks were successful
CI / CI (push) Successful in 1m24s

And some general cleanup of a couple of functions.
This commit is contained in:
SileNce5k 2025-05-17 11:15:05 +02:00
parent c74f323c1b
commit ed77c2aaf1
Signed by: SileNce
GPG key ID: B0A142BB4291B204
5 changed files with 48 additions and 27 deletions

View file

@ -1,17 +1,26 @@
const fs = require('fs');
const parseTime = require('./parseTime');
const timeUntil = require('./timeUntil');
const sqlite3 = require('sqlite3').verbose();
module.exports = async function (message, args, compatibility) {
module.exports = async function (message, args) {
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");
return "Please specify a time, and a message to send after the timer has finished";
let currentUnixTime = Math.floor(new Date() / 1000);
let timeInSeconds = compatibility ? parseTime(args[0], currentUnixTime) : parseTime(args[1], currentUnixTime);
let timeInSeconds;
if(!isNaN(Date.parse(args[0])) && isNaN(parseTime(args[0], currentUnixTime))){
timeInSeconds = timeUntil(args[0]).totalInSeconds;
if(timeInSeconds < 0){
return "The date must not be in the past."
}
}else {
timeInSeconds = parseTime(args[0], currentUnixTime);
}
if (isNaN(timeInSeconds)) {
return message.channel.send("Please specify a time, and a message to send after the timer has finished")
return "Please specify a time, and a message to send after the timer has finished"
}
let customMessage = compatibility ? args.slice(1).join(" ") : args.slice(2).join(" ");
let reminderTime = currentUnixTime + timeInSeconds
const customMessage = args.slice(1).join(" ")
const reminderTime = currentUnixTime + timeInSeconds
let newTimerID;
const db = new sqlite3.Database(databasePath)
let sendText = await new Promise((resolve, reject)=>{

View file

@ -1,7 +1,7 @@
module.exports = function(time, currentUnixTime){
let timeInSeconds = parseFloat(time.slice(0, time.length - 1))
let letter = time.slice(time.length - 1)
if(!isNaN(letter)) return parseFloat(time) * 60;
let timeInSeconds = parseFloat(time)
const letterCount = time.length - timeInSeconds.toString().length;
const letter = time.slice(time.length - letterCount);
switch (letter.toUpperCase()) {
case "H":
timeInSeconds = timeInSeconds * 3_600;
@ -14,7 +14,8 @@ module.exports = function(time, currentUnixTime){
case "D":
timeInSeconds = timeInSeconds * 86_400;
break;
case "T": // TODO: Make it so that I can have multiple letters per case, so that "TS" would work here.
case "TS": // Unix timestamp
case "T":
timeInSeconds = timeInSeconds - currentUnixTime;
break;
case "W":
@ -22,17 +23,6 @@ module.exports = function(time, currentUnixTime){
break;
default:
timeInSeconds = NaN;
if(time.includes(':'))
timeInSeconds = getTime(time, currentUnixTime);
}
return timeInSeconds;
}
function getTime(time, currentUnixTime) {
return timeInSeconds;
}

20
util/timer/timeUntil.js Normal file
View file

@ -0,0 +1,20 @@
module.exports = function(targetTime) {
const countDownDate = new Date(targetTime).getTime();
const now = new Date().getTime();
const distance = countDownDate - now;
let days = Math.floor(distance / (1000 * 60 * 60 * 24));
let hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
let minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
let seconds = Math.floor((distance % (1000 * 60)) / 1000);
if (seconds < 0) { // Due to how the math above works, if the input time is in the past, the time will be off by 1.
days = days + 1;
hours = hours + 1;
minutes = minutes + 1;
seconds = seconds + 1;
}
const totalInSeconds = (days * 24 * 60 * 60) + (hours * 60 * 60) + (minutes * 60) + seconds;
return { days, hours, minutes, seconds, totalInSeconds };
}