-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscheduleMessage.js
More file actions
134 lines (112 loc) · 3.66 KB
/
scheduleMessage.js
File metadata and controls
134 lines (112 loc) · 3.66 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
const {
findAssumedWord,
findByDate,
findStartDateByBinarySearch,
compareTime,
findByType,
getPrefix,
} = require("./commands/schedule");
const { currentDirectory } = require("./commands/commandConfig.json");
const schedule = require(`${currentDirectory}/schedules/schedule.json`);
const scheduleTypes = require(`${currentDirectory}/schedules/scheduleTypes.json`);
const { MessageEmbed } = require("discord.js");
let currentEventIndex = 0;
var firstTime = true;
function getNextScheduleEvent() {
let nmbr = 0;
nmbr = getNextEventIndex();
currentEventIndex = nmbr;
const { Startdatum, Starttid, Sluttid, Kurs__1, Lokal, Typ } =
schedule[nmbr];
console.log(`Event course: ${Kurs__1}!`);
console.log(`Event type: ${Typ}`);
const testEmbed = new MessageEmbed()
.setColor("#0099FF")
.setTitle(`${Typ}`)
.setDescription(`${Lokal}`)
.addFields(
{ name: `Kurs:`, value: `${Kurs__1}`, inline: true },
{ name: `Startar:`, value: `${Starttid}`, inline: true },
{ name: `Slutar:`, value: `${Sluttid}`, inline: true },
{ name: `Datum`, value: `${Startdatum}`, inline: true }
);
return testEmbed;
}
function getNextEventIndex() {
var now = new Date();
for (let i = 0; i < schedule.length; i++) {
const { Startdatum, Starttid } = schedule[i];
var then = getTimeInMilli(Startdatum, Starttid);
if (
then.getDate == now.getDate &&
then.getMonth == now.getMonth &&
then.getFullYear == now.getFullYear
) {
if (then > now) {
return i;
}
} else if (then > now) {
return i;
}
}
return -1;
}
function getTimeInMilli(startDate, startTime) {
startDate = startDate.split("-");
startTime = startTime.split(":");
let startYear = startDate[0];
let startMonth = startDate[1];
let startDay = startDate[2];
let startHours = startTime[0];
let startMinutes = startTime[1];
var then = new Date(
startYear,
startMonth - 1,
startDay,
startHours,
startMinutes
);
return then;
}
function getNextTime(channel) {
console.log("Getting next time");
if (currentEventIndex < schedule.length) {
currentEventIndex++;
} else {
console.log("Schdule is empty");
return null;
}
const { Startdatum, Starttid } = schedule[currentEventIndex];
var now = new Date();
now.setHours(now.getHours() + 2);
var then = getTimeInMilli(Startdatum, Starttid);
then.setHours(then.getHours() + 2);
console.log(now);
console.log(then);
var nextTime = then - now - 1000 * 60 * 15;
if (nextTime < 0) {
var aEvent = getAScheduleEvent(currentEventIndex);
channel.send("And");
channel.send({ embeds: [aEvent] });
nextTime = getNextTime(channel);
}
return nextTime;
}
function getAScheduleEvent(number) {
const { Startdatum, Starttid, Sluttid, Kurs__1, Lokal, Typ } =
schedule[number];
console.log(`Event course: ${Kurs__1}!`);
console.log(`Event type: ${Typ}`);
const testEmbed = new MessageEmbed()
.setColor("#0099FF")
.setTitle(`${Typ}`)
.setDescription(`${Lokal}`)
.addFields(
{ name: `Kurs:`, value: `${Kurs__1}`, inline: true },
{ name: `Startar:`, value: `${Starttid}`, inline: true },
{ name: `Slutar:`, value: `${Sluttid}`, inline: true },
{ name: `Datum`, value: `${Startdatum}`, inline: true }
);
return testEmbed;
}
module.exports = { getNextScheduleEvent, getNextTime };