From 2c90c20e505f0e4cd4f3091eb63465e43e1571cc Mon Sep 17 00:00:00 2001
From: SileNce5k <ozzynexus@gmail.com>
Date: Wed, 14 May 2025 06:51:03 +0200
Subject: [PATCH] Allow dates to be used in timer command

---
 commands/misc/timer.js    |  7 ++++---
 util/timer/createTimer.js | 13 ++++++++++---
 util/timer/parseTime.js   | 20 +++++---------------
 util/timer/timeSince.js   | 21 +++++++++++++++++++++
 4 files changed, 40 insertions(+), 21 deletions(-)
 create mode 100644 util/timer/timeSince.js

diff --git a/commands/misc/timer.js b/commands/misc/timer.js
index 746689b..5996ab4 100644
--- a/commands/misc/timer.js
+++ b/commands/misc/timer.js
@@ -8,7 +8,7 @@ module.exports = {
 	moreHelp: ["Usage:"
 	,"`<prefix>timer [add|create] <time_in_minutes> <message_to_send>`"
 	,"`<prefix>timer <time>(d|h|m|s|t) <message_to_send>`"
-	,"`<prefix>timer <time_in_minutes> <message_to_send>`"
+	,"`<prefix>timer <future_date> <message_to_send>`"
 	,"`<prefix>timer edit <timer_id> <new_time_in_minutes> <new_message_to_send>` (not implemented)"
 	,"`<prefix>timer [delete|remove] <timer_id>`"
 	,"`<prefix>timer show <timer_id>`"
@@ -18,7 +18,8 @@ module.exports = {
 		switch (args[0]) {
 			case "add":
 			case "create":
-				sendText = await createTimer(message, args, false);
+				args.shift()
+				sendText = await createTimer(message, args);
 				break;
 			case "edit":
 				sendText = "not implemented yet"
@@ -38,7 +39,7 @@ module.exports = {
 					break;
 				}
 				if(!isNaN(parseTime(args[0], Math.floor(new Date() / 1000))))
-					sendText = await createTimer(message, args, true);
+					sendText = await createTimer(message, args);
 				break;
 		}
 		message.channel.send(sendText);
diff --git a/util/timer/createTimer.js b/util/timer/createTimer.js
index 3910d09..3d64ae6 100644
--- a/util/timer/createTimer.js
+++ b/util/timer/createTimer.js
@@ -1,16 +1,23 @@
 const fs = require('fs');
 const parseTime = require('./parseTime');
+const timeSince = require('./timeSince');
 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");
 	let currentUnixTime = Math.floor(new Date() / 1000);
-	let timeInSeconds = compatibility ? parseTime(args[0], currentUnixTime) : parseTime(args[1], currentUnixTime);
+
+	let timeInSeconds;
+	if(Date.parse(args[0]) && parseFloat(args[0]).toString() === args[0]){
+		timeInSeconds = timeSince(args[0]);
+	}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")
 	}
-	let customMessage = compatibility ? args.slice(1).join(" ") : args.slice(2).join(" ");
+	let customMessage = args.slice(1).join(" ")
 	let reminderTime = currentUnixTime + timeInSeconds
 	let newTimerID;
 	const db = new sqlite3.Database(databasePath)
diff --git a/util/timer/parseTime.js b/util/timer/parseTime.js
index 732fe80..370d661 100644
--- a/util/timer/parseTime.js
+++ b/util/timer/parseTime.js
@@ -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;
+	let 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;
-}
diff --git a/util/timer/timeSince.js b/util/timer/timeSince.js
new file mode 100644
index 0000000..61ef82f
--- /dev/null
+++ b/util/timer/timeSince.js
@@ -0,0 +1,21 @@
+module.exports = function(timeElapsed) {
+
+    let countDownDate = new Date(timeElapsed).getTime();
+    let now = new Date().getTime();
+  
+    let 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) {
+      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: days, hours: hours, minutes: minutes, seconds: seconds, totalInSeconds: totalInSeconds };
+}