-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathReminder.js
158 lines (149 loc) · 5.51 KB
/
Reminder.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
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
"use strict";
if (!FS.existsSync("./data/reminders.json")) FS.writeFileSync("./data/reminders.json", "{}");
let reminders = JSON.parse(FS.readFileSync("./data/reminders.json"));
/* REMINDER FORMAT
'[date]': {
type: User/Room,
target: User/Room,
message
}
*/
module.exports = {
check: function () {
let now = Date.now().toString();
let dates = Object.keys(reminders);
dates.push(now);
dates = dates.sort();
for (let stamp of dates) {
if (stamp === now) return;
let reminder = reminders[stamp];
if (reminder.type === "room") {
let room = Rooms[reminder.target];
if (!room) {
Send("", "/join " + reminder.target);
continue;
}
room.send(reminder.message);
} else {
let user = Users[reminder.target];
if (!user) {
// User is offline or not in the room
Commands.mail(Users.self, Users.self, [reminder.target, reminder.message]);
} else {
// Send the reminder to the user
user.send(reminder.message);
}
}
this.delete(stamp);
}
},
parse: function (user, room, message) {
if (!message.startsWith(Config.username)) return;
message = message.substring(Config.username.length).trim();
if (!message.toLowerCase().startsWith("remind me")) return;
message = message.substring(11).trim();
let parts = {};
if (message.toLowerCase().startsWith("in")) {
let to = message.indexOf(" to ");
parts.in = message.substring(2, to + 1).trim();
parts.to = message.substring(to).trim();
} else if (message.toLowerCase().startsWith("to")) {
let to = message.indexOf(" in ");
parts.to = message.substring(0, to + 1).trim();
parts.in = message.substring(to + 2).trim();
} else if (message.includes("in")) {
let to = message.indexOf(" in ");
parts.to = message.substring(0, to + 1).trim();
parts.in = message.substring(to + 2).trim();
} else {
return "Invalid syntax.";
}
console.log(parts);
parts.in = parts.in
.replace(/ ?and ?/gi, ",")
.replace(/ ?, ?/g, ",")
.split(",");
let fromnow = 0;
let timethings = {
minutes: 0,
hours: 0,
days: 0,
weeks: 0,
};
for (let i of parts.in) {
let x = i.split(" ");
let type = toId(x[1]);
let val = parseInt(x[0]);
if (isNaN(val) || !type) continue;
if (type.charAt(type.length - 1) !== "s") type += "s";
switch (type) {
case "minutes":
fromnow += val * 1000 * 60;
timethings.minutes += val;
while (timethings.minutes >= 60) {
timethings.minutes -= 60;
timethings.hours += 1;
}
break;
case "hours":
fromnow += val * 1000 * 60 * 60;
timethings.hours += val;
while (timethings.hours >= 24) {
timethings.hours -= 24;
timethings.days += 1;
}
break;
case "days":
fromnow += val * 1000 * 60 * 60 * 24;
timethings.days += val;
while (timethings.days >= 7) {
timethings.days -= 7;
timethings.weeks += 1;
}
break;
case "weeks":
fromnow += val * 1000 * 60 * 60 * 24 * 7;
timethings.weeks += val;
break;
default:
break;
}
}
let ret = [];
if (timethings.weeks) ret.push(timethings.weeks + " week" + (timethings.weeks === 1 ? "" : "s"));
if (timethings.days) ret.push(timethings.days + " day" + (timethings.days === 1 ? "" : "s"));
if (timethings.hours) ret.push(timethings.hours + " hour" + (timethings.hours === 1 ? "" : "s"));
if (timethings.minutes) ret.push(timethings.minutes + " minute" + (timethings.minutes === 1 ? "" : "s"));
console.log(timethings);
let timestr = ret.join(" and ");
let ands = timestr.match(/and/gi);
while (ands && ands.length > 1) {
timestr = timestr.replace(" and", ",");
ands = timestr.match(/and/gi);
}
let msg = user.name + " you wanted me to remind you " + parts.to;
let endtime = Date.now() + fromnow;
while (reminders[endtime]) endtime += 1;
let type = "room";
let target = room;
if (room === user) type = "user";
if (!user.can(room, "+")) {
type = "user";
target = user;
}
reminders[endtime.toString()] = {
type,
target: target.id,
message: msg,
};
target.send("I'll remind you in " + timestr);
this.save();
},
delete: function (id) {
delete reminders[id];
this.save();
},
save: function () {
FS.writeFileSync("./data/reminders.json", JSON.stringify(reminders, null, 4));
},
};