-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
292 lines (253 loc) · 10 KB
/
Copy pathindex.js
File metadata and controls
292 lines (253 loc) · 10 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
//Starting files to import Discord functionality and config files for bot.
const { Client } = require("discord.js");
const { config } = require("dotenv");
const client = new Client();
const fs = require('fs');
const Discord = require('discord.js');
const helper = require("./helper");
const setConstantReminder = require("./commands/setConstantReminder");
const constantRemind = require("./commands/constantRemind");
const { SSL_OP_SSLEAY_080_CLIENT_DH_BUG } = require("constants");
client.commands = new Discord.Collection();
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'))
let boolean = 0;
//Path for token.
config({
path: __dirname + "/.env"
});
//Turn the bot on and set presence.
client.on("ready", () => {
console.log("BBBE Bot is online!");
client.user.setPresence({
status: "online",
game: {
name: "our awesome group!",
type: "WATCHING"
}
})
todayschedule();
schedule();
console.log("Finished schedule");
constantRe();
schedule2();
console.log("Finished Schedule2");
notifyAssignments();
timetest();
console.log("Finished time test");
});
for (const file of commandFiles){
const command = require(`./commands/${file}`);
client.commands.set(command.name, command);
}
//Handles messages
client.on("message", async message => {
//Prefix for all commands
const prefix = "!";
//If the message does not start with prefix ignore it.
if(!message.content.startsWith(prefix) || message.author.bot) return;
//If message does have prefix splice it by spaces for analysis. We use regex to account for multiple spaces.
const args = message.content.slice(prefix.length).trim().split(/ +/);
const command = args.shift().toLowerCase();
if (!client.commands.has(command)) return;
try {
client.commands.get(command).execute(message, args);
//console.log(`${command}\t${message}\t${args}`)
}
catch(error){
console.error(error);
message.reply('Command error');
}
console.log(`${message.author.username} said: ${message.content}`);
});
client.login(process.env.TOKEN);
function timetest(){
setTimeout(function(){
notifyAssignments();
timetest();
}, 15*1000*60); //every 15 mins
}
function schedule(){
setTimeout(function(){
todayschedule();
schedule();
},300*1000*60);
}
function schedule2(){
if(boolean==0){
setTimeout(function(){
constantRe();
boolean = 1;
schedule2();
},1000)
}
else{
setTimeout(function(){
boolean = 0;
schedule2();
},1000*60*30)
}
}
function constantRe(){
let users = []; //array of users to notify
if(fs.existsSync('usersToNotify.json')) {
const data = JSON.parse(fs.readFileSync('usersToNotify.json'));
//get users to notify
for (key in data){
if (data[key] == "1") {
users.push(key);
}
}
for (let i = 0; i < users.length; i++){ //for every user
console.log(users[i]);
let uid = users[i].substring(2).slice(0, -1);
if(!fs.existsSync('./constantReminders.json')){
break;
}
const data = fs.readFileSync('./constantReminders.json');
let objs = JSON.parse(data);
if(objs[users[i]] == undefined){
continue;
}
let promises = []
promises.push(client.users.fetch(uid))
Promise.all(promises).then((results) => {
var today = new Date();
var day = today.getDay();
var daylist = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
console.log("Today is : " + daylist[day] + ".");
var hour = today.getHours();
var minute = today.getMinutes();
var second = today.getSeconds();
var prepand = (hour >= 12)? "PM":"AM";
hour = (hour >= 12)? hour - 12: hour;
if (hour===0 && prepand==='PM')
{
if (minute===0 && second===0)
{
hour=12;
}
else
{
hour=12;
prepand='PM';
}
}
if (hour===0 && prepand==='AM')
{
if (minute===0 && second===0)
{
hour=12;
}
else
{
hour=12;
}
}
console.log("Current Time : "+hour + prepand + " : " + minute + " : " + second);
for(var j = 0; j < objs[users[i]].length; j++){
if(objs[users[i]][j].day===daylist[day]){
if(parseInt(objs[users[i]][j].hour)===hour){
client.users.cache.get(uid).send(`Remember:\n${objs[users[i]][j].name} at ${objs[users[i]][j].hour}:${objs[users[i]][j].minute}:${objs[users[i]][j].second} ${objs[users[i]][j].timeofday}\n`)
boolean = 1;
if(parseInt(objs[users[i]][j].minute)===minute){
if(objs[users[i]][j].timeofday===prepand){
client.users.cache.get(uid).send(`Remember:\n${objs[users[i]][j].name} at ${objs[users[i]][j].hour}:${objs[users[i]][j].minute}:${objs[users[i]][j].second} ${objs[users[i]][j].timeofday}\n`)
}
}
}
}
}
})
}
}
}
function todayschedule(){
let users = []; //array of users to notify
if(fs.existsSync('usersToNotify.json')) {
const data = JSON.parse(fs.readFileSync('usersToNotify.json'));
//get users to notify
for (key in data){
if (data[key] == "1") {
users.push(key);
}
}
for (let i = 0; i < users.length; i++){ //for every user
console.log(users[i]);
let uid = users[i].substring(2).slice(0, -1);
if(!fs.existsSync('./constantReminders.json')){
break;
}
const data = fs.readFileSync('./constantReminders.json');
let objs = JSON.parse(data);
if(objs[users[i]] == undefined){
continue;
}
var today = new Date();
var day = today.getDay();
var daylist = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
console.log("Today is : " + daylist[day] + ".");
let promises = []
promises.push(client.users.fetch(uid))
Promise.all(promises).then((results) => {
console.log(results[0].id)
client.users.cache.get(results[0].id).send("Today you have:\n");
for(var j = 0; j < objs[users[i]].length; j++){
if(objs[users[i]][j].day===daylist[day]){
client.users.cache.get(results[0].id).send(`${objs[users[i]][j].name} at ${objs[users[i]][j].hour}:${objs[users[i]][j].minute}:${objs[users[i]][j].second} ${objs[users[i]][j].timeofday}\n`)
}
}
})
}
}
}
function notifyAssignments(){
let users = []; //array of users to notify
if(fs.existsSync('usersToNotify.json')) {
const data = JSON.parse(fs.readFileSync('usersToNotify.json'));
//get users to notify
for (key in data){
if (data[key] == "1") {
users.push(key);
}
}
for (let i = 0; i < users.length; i++){ //for every user
//get access token
let token = helper.getUserToken(users[i]);
//get course ids
let courseIDs = [];
let uid = users[i].substring(2).slice(0, -1);
let courses = helper.getUserCourses(uid);
console.log(uid)
for (key in courses){
courseIDs.push(key);
///console.log(key);
}
let promises = []; //list of promises for assignments
for (let j = 0; j < courseIDs.length; j++){
//go through all courses and all assignments
let url = "https://canvas.instructure.com/api/v1/courses/" + courseIDs[j] + "/assignments?per_page=50&access_token=" + token;
promises.push(helper.httpsGetJSON2(url));
}
Promise.all(promises).then((results) => { //when all calls are made, print assignments
//console.log(results[1][0].name);
for (let j = 0; j < results.length; j++){
for (let k = 0; k < results[j].length; k++){
//if (helper.dateInFuture(results[i].due_at))
if (results[j][k] != undefined && results[j][k].due_at != null && helper.dateInFuture(results[j][k].due_at)){
//console.log(results[j][k].name)
if (helper.isDue(results[j][k].due_at, 1, 0, 0, 15))
client.users.cache.get(uid).send(results[j][k].name + "is due in 1 day.")
else if (helper.isDue(results[j][k].due_at, 0, 3, 0, 15))
client.users.cache.get(uid).send(results[j][k].name + "is due in 3 hours.")
else if (helper.isDue(results[j][k].due_at, 0, 1, 0, 15))
client.users.cache.get(uid).send(results[j][k].name + "is due in 1 hour.")
else if (helper.isDue(results[j][k].due_at, 0, 0, 30, 15))
client.users.cache.get(uid).send(results[j][k].name + "is due in 30 minutes.")
}
}
}
//console.log(results);
});
}
}
}