-
Notifications
You must be signed in to change notification settings - Fork 0
/
cronFunctions.js
95 lines (85 loc) · 3.02 KB
/
cronFunctions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
var utils = require('./utils');
module.exports = {
// Runs cronic function
runCronFunctions: function (pool, guild, discord) {
// Calculates time to run time functions
var nextDate = new Date();
if (nextDate.getMinutes() === 0) {
callEveryMinute();
} else {
if (nextDate.getHours() >= 1) {
nextDate.setHours(nextDate.getHours() + (24 - (nextDate.getHours() - 1)));
} else {
nextDate.setHours(1);
}
// Calls utils.logTopOfTheMonth and utils.logMostPlayedGamesOfTheDay every day
nextDate = cleanDate(nextDate);
var difference = nextDate - new Date();
setTimeout(function() {
callEveryDay(pool, guild, discord); // callEveryDay at 1AM UTC
}, difference);
// Call utils.logMemes every 12 hours
nextDate = new Date();
var hourDifference = nextDate.getHours() % 12;
nextDate.setHours(nextDate.getHours() + (12 - hourDifference));
nextDate = cleanDate(nextDate);
var difference = nextDate - new Date();
setTimeout(function() {
callEveryTwelveHours(guild, discord);
}, difference); // callEveryTwelveHours
// Call utils.registerMostPlayedGames every hour
nextDate = new Date();
var hourDifference = nextDate.getHours() % 1;
nextDate.setHours(nextDate.getHours() + (1 - hourDifference));
nextDate = cleanDate(nextDate);
var difference = nextDate - new Date();
setTimeout(function() {
callEveryTenMinutes(guild, discord);
}, difference); // callEveryTenMinutes
// Call utils.updateVoicePoints every minute
nextDate = new Date();
nextDate.setMinutes(nextDate.getMinutes() + 1);
nextDate.setSeconds(0);
difference = nextDate - new Date();
setTimeout(function() {
callEveryMinute(pool, guild); // callEveryMinute
}, difference);
}
}
};
// Calls utils.logTopOfTheMonth every day
function callEveryDay(pool, guild, discord) {
utils.logTopOfTheMonth(pool, guild, true, new discord.RichEmbed(), new discord.RichEmbed());
utils.logMostPlayedGamesOfTheDay(guild, new discord.RichEmbed());
setInterval(function() {
utils.logTopOfTheMonth(pool, guild, true, new discord.RichEmbed(), new discord.RichEmbed());
utils.logMostPlayedGamesOfTheDay(guild, new discord.RichEmbed());
}, 1000 * 60 * 60 * 24);
}
// Calls utils.logMemes every 12 hours
function callEveryTwelveHours(guild, discord) {
utils.logMemes(guild, new discord.RichEmbed());
setInterval(function() {
utils.logMemes(guild, new discord.RichEmbed());
}, 1000 * 60 * 60 * 12);
}
// Calls utils.registerMostPlayedGames every 10 minutes
function callEveryTenMinutes(guild, discord) {
utils.registerMostPlayedGames(guild, new discord.RichEmbed());
setInterval(function() {
utils.registerMostPlayedGames(guild, new discord.RichEmbed());
}, 1000 * 60 * 10);
}
// Calls utils.updateVoicePoints every minute
function callEveryMinute(pool, guild) {
utils.updateVoicePoints(pool, guild);
setInterval(function() {
utils.updateVoicePoints(pool, guild);
}, 1000 * 60 * 1);
}
// Resets date minutes and seconds to zero
function cleanDate(date) {
date.setMinutes(0);
date.setSeconds(0);
return date;
}