Rewrite timer command to use JSON

* Rewrite parseTime to use seconds instead of ms
	*  Move parseTime to timer subdirectory

* Implement checkTimer and sendTimerReminder.
This commit is contained in:
SileNce5k 2022-06-08 16:29:13 +02:00
parent a4b75b0161
commit 04f39582a4
No known key found for this signature in database
GPG key ID: C507260E7F2583AD
7 changed files with 55 additions and 14 deletions

View file

@ -1,4 +1,4 @@
const parseTime = require('../../util/parseTime'); const parseTime = require('../../util/timer/parseTime');
module.exports = { module.exports = {
name: "timer", name: "timer",
description: "Set a timer for a time in minutes.", description: "Set a timer for a time in minutes.",
@ -6,16 +6,20 @@ module.exports = {
,"`<prefix>timer <time_in_minutes> <message_to_send>`" ,"`<prefix>timer <time_in_minutes> <message_to_send>`"
,"`<prefix>timer <time>(m|h|s) <message_to_send>`" ,"`<prefix>timer <time>(m|h|s) <message_to_send>`"
,"Bot will mention you after the time has passed, with the custom message."], ,"Bot will mention you after the time has passed, with the custom message."],
execute({message, args}) { execute({client, message, args}) {
if(args.length < 2) if(args.length < 2 || isNaN(time))
return message.channel.send("Please specify a time in minutes, 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 time = parseTime(args[0]); let currentUnixTime = Math.floor(new Date() / 1000);
if(isNaN(time)) let timeInSeconds = parseTime(args[0]);
return message.channel.send("Specify a time in number of minutes"); let customMessage = args.slice(1).join(" ");
let sendText = args.slice(1).join(" "); let reminderTime = currentUnixTime + timeInSeconds
setTimeout(function(){ const newTimer = {
message.channel.send(`<@${message.author.id}>, ${sendText}`); "user": `${message.author.id}`,
}, time); "reminderDate": reminderTime,
message.channel.send(`I will remind you <t:${Math.floor(new Date() / 1000) + (time / 1000) - 4}:R>`); "channel": `${message.channel.id}`,
"customMessage": `${customMessage}`
}
client.timers.push(newTimer);
message.channel.send(`I will remind you <t:${reminderTime}:R>`);
} }
}; };

View file

@ -20,6 +20,7 @@ client.settings = new Discord.Collection();
client.commands = new Discord.Collection(); client.commands = new Discord.Collection();
client.serverPrefixes = new Discord.Collection(); client.serverPrefixes = new Discord.Collection();
client.netmodules = new Discord.Collection(); client.netmodules = new Discord.Collection();
client.timers = require('./data/timers.json')
client.settings.set("presenceType", presenceType); client.settings.set("presenceType", presenceType);
client.settings.set("presenceText", presenceText); client.settings.set("presenceText", presenceText);

View file

@ -3,8 +3,9 @@ const customReplaceWithVariables = require('../util/custom_commands/customReplac
module.exports = function(client, owners, message, globalPrefix){ module.exports = function(client, owners, message, globalPrefix){
let prefix = globalPrefix; let prefix = globalPrefix;
if (client.serverPrefixes.get(message.guild.id)) { let serverPrefix = client.serverPrefixes.get(message.guild.id);
prefix = client.serverPrefixes.get(message.guild.id) if (serverPrefix) {
prefix = serverPrefix;
} }
let args = message.content.slice(prefix.length).split(" ") let args = message.content.slice(prefix.length).split(" ")

View file

@ -1,4 +1,5 @@
const loadServerPrefixes = require('../util/loadServerPrefixes'); const loadServerPrefixes = require('../util/loadServerPrefixes');
const checkTimer = require('../util/timer/checkTimer');
const updatePresence = require('../util/updatePresence'); const updatePresence = require('../util/updatePresence');
module.exports = function(client, enableLoginMessage, loginChannel, loginMessage) { module.exports = function(client, enableLoginMessage, loginChannel, loginMessage) {
@ -12,4 +13,5 @@ module.exports = function(client, enableLoginMessage, loginChannel, loginMessage
console.log("Failed trying to send a message on login.\n") console.log("Failed trying to send a message on login.\n")
} }
loadServerPrefixes(client) loadServerPrefixes(client)
checkTimer(client);
} }

11
util/timer/checkTimer.js Normal file
View file

@ -0,0 +1,11 @@
const checkTimer = require('./checkTimer')
const sendTimerReminder = require('./sendTimerReminder')
module.exports = function (client) {
client.timers.forEach(timer => {
if(parseInt(timer.reminderDate) >= Math.floor(new Date() / 1000)){
sendTimerReminder(client, timer);
client.timers.pop(timer);
}
});
setTimeout(checkTimer, 1000, client);
}

19
util/timer/parseTime.js Normal file
View file

@ -0,0 +1,19 @@
module.exports = function(time){
let timeInSeconds = parseInt(time.slice(0, time.length - 1))
let letter = time.slice(time.length - 1)
if(!isNaN(letter)) return parseInt(time) * 60;
switch (letter.toUpperCase()) {
case "H":
timeInSeconds = timeInSeconds * 3600; // 3 600 000
break;
case "M":
timeInSeconds = timeInSeconds * 60; // 60 000
break;
case "S":
timeInSeconds = timeInSeconds; // 1 000
break;
default:
timeInSeconds = -1;
}
return timeInSeconds;
}

View file

@ -0,0 +1,3 @@
module.exports = function (client, timer) {
client.channels.cache.get(timer.channel).send(`<@${timer.user}>, ${timer.customMessage}`);
}