Allow dates to be used in timer command
All checks were successful
CI / CI (push) Successful in 1m24s
All checks were successful
CI / CI (push) Successful in 1m24s
And some general cleanup of a couple of functions.
This commit is contained in:
parent
c74f323c1b
commit
ed77c2aaf1
5 changed files with 48 additions and 27 deletions
|
@ -4,11 +4,11 @@ const parseTime = require('../../util/timer/parseTime');
|
|||
const showTimer = require('../../util/timer/showTimer');
|
||||
module.exports = {
|
||||
name: "timer",
|
||||
description: "Set a timer for a time in minutes.",
|
||||
description: "Set a timer for a date or time duration.",
|
||||
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"
|
||||
|
@ -37,8 +38,8 @@ module.exports = {
|
|||
sendText = "Please specify a time, and a message to send after the timer has finished";
|
||||
break;
|
||||
}
|
||||
if(!isNaN(parseTime(args[0], Math.floor(new Date() / 1000))))
|
||||
sendText = await createTimer(message, args, true);
|
||||
if(!isNaN(parseTime(args[0], Math.floor(new Date() / 1000))) || !isNaN(args[0]))
|
||||
sendText = await createTimer(message, args);
|
||||
break;
|
||||
}
|
||||
message.channel.send(sendText);
|
||||
|
|
|
@ -48,6 +48,7 @@ client.whitelist = {
|
|||
guild: new Collection(),
|
||||
user: new Collection()
|
||||
}
|
||||
process.env.TZ = "UTC";
|
||||
|
||||
createAndLoadWhitelistTable(client.whitelist);
|
||||
|
||||
|
|
|
@ -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)=>{
|
||||
|
|
|
@ -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
20
util/timer/timeUntil.js
Normal 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 };
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue