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

This commit is contained in:
SileNce5k 2025-05-14 06:51:03 +02:00
parent c74f323c1b
commit 2c90c20e50
Signed by: SileNce
GPG key ID: B0A142BB4291B204
4 changed files with 40 additions and 21 deletions

View file

@ -8,7 +8,7 @@ module.exports = {
moreHelp: ["Usage:" moreHelp: ["Usage:"
,"`<prefix>timer [add|create] <time_in_minutes> <message_to_send>`" ,"`<prefix>timer [add|create] <time_in_minutes> <message_to_send>`"
,"`<prefix>timer <time>(d|h|m|s|t) <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 edit <timer_id> <new_time_in_minutes> <new_message_to_send>` (not implemented)"
,"`<prefix>timer [delete|remove] <timer_id>`" ,"`<prefix>timer [delete|remove] <timer_id>`"
,"`<prefix>timer show <timer_id>`" ,"`<prefix>timer show <timer_id>`"
@ -18,7 +18,8 @@ module.exports = {
switch (args[0]) { switch (args[0]) {
case "add": case "add":
case "create": case "create":
sendText = await createTimer(message, args, false); args.shift()
sendText = await createTimer(message, args);
break; break;
case "edit": case "edit":
sendText = "not implemented yet" sendText = "not implemented yet"
@ -38,7 +39,7 @@ module.exports = {
break; break;
} }
if(!isNaN(parseTime(args[0], Math.floor(new Date() / 1000)))) if(!isNaN(parseTime(args[0], Math.floor(new Date() / 1000))))
sendText = await createTimer(message, args, true); sendText = await createTimer(message, args);
break; break;
} }
message.channel.send(sendText); message.channel.send(sendText);

View file

@ -1,16 +1,23 @@
const fs = require('fs'); const fs = require('fs');
const parseTime = require('./parseTime'); const parseTime = require('./parseTime');
const timeSince = require('./timeSince');
const sqlite3 = require('sqlite3').verbose(); const sqlite3 = require('sqlite3').verbose();
module.exports = async function (message, args, compatibility) { module.exports = async function (message, args) {
const databasePath = 'data/database.db' const databasePath = 'data/database.db'
if (args.length < 2) if (args.length < 2)
return message.channel.send("Please specify a time, and a message to send after the timer has finished"); 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 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)) { if (isNaN(timeInSeconds)) {
return message.channel.send("Please specify a time, and a message to send after the timer has finished") 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 reminderTime = currentUnixTime + timeInSeconds
let newTimerID; let newTimerID;
const db = new sqlite3.Database(databasePath) const db = new sqlite3.Database(databasePath)

View file

@ -1,7 +1,7 @@
module.exports = function(time, currentUnixTime){ module.exports = function(time, currentUnixTime){
let timeInSeconds = parseFloat(time.slice(0, time.length - 1)) let timeInSeconds = parseFloat(time)
let letter = time.slice(time.length - 1) const letterCount = time.length - timeInSeconds.toString().length;
if(!isNaN(letter)) return parseFloat(time) * 60; let letter = time.slice(time.length - letterCount);
switch (letter.toUpperCase()) { switch (letter.toUpperCase()) {
case "H": case "H":
timeInSeconds = timeInSeconds * 3_600; timeInSeconds = timeInSeconds * 3_600;
@ -14,7 +14,8 @@ module.exports = function(time, currentUnixTime){
case "D": case "D":
timeInSeconds = timeInSeconds * 86_400; timeInSeconds = timeInSeconds * 86_400;
break; 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; timeInSeconds = timeInSeconds - currentUnixTime;
break; break;
case "W": case "W":
@ -22,17 +23,6 @@ module.exports = function(time, currentUnixTime){
break; break;
default: default:
timeInSeconds = NaN; timeInSeconds = NaN;
if(time.includes(':'))
timeInSeconds = getTime(time, currentUnixTime);
} }
return timeInSeconds; return timeInSeconds;
} }
function getTime(time, currentUnixTime) {
return timeInSeconds;
}

21
util/timer/timeSince.js Normal file
View file

@ -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 };
}