-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpomo.js
378 lines (322 loc) · 12.2 KB
/
pomo.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
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
var Logger = require('./logger');
const dotenv = require('dotenv');
dotenv.config();
const { MessageActionRow, MessageButton } = require('discord.js');
class Pomo {
constructor(channelId,
work,
short_break,
long_break,
sessions,
channel,
pausable,
User,
paused = false,
paused_time = null,
paused_tick = 0,
stopped = false,
session_status = null,
session_time = null,
pomodoro_counter = 0,
total_pomodoros = 0,
interval = null,
tick = 1000,
count_in_min = 0,
session_time_raw = 0,
sessions_remaining = 0,
mode = 1,
count = 0
) {
// guild and channel
this.channelId = channelId;
this.work = work;
this.short_break = short_break;
this.long_break = long_break;
this.sessions = sessions;
// current channel
this.channel = channel
//pause stuff
this.pausable = pausable
this.paused = paused
this.paused_time = paused_time
this.paused_tick = paused_tick
//stopped
this.stopped = stopped
//counters and status
this.session_status = session_status
this.session_time = session_time
this.pomodoro_counter = pomodoro_counter
this.total_pomodoros = total_pomodoros
//time related
this.interval = interval
this.tick = tick
this.count_in_min = count_in_min
this.session_time_raw = session_time_raw
this.tick_frequency = process.env.TICK_FREQUENCY || 60
//DB stuff
this.User = User
this.MyUser = null
console.log(sessions_remaining)
// session
if (this.sessions == undefined || this.sessions == "undefined" || !this.sessions ){
this.sessions = 4
}
if (sessions_remaining == undefined || sessions_remaining == "undefined" || !sessions_remaining ){
this.sessions_remaining = this.sessions
}else{
this.sessions_remaining = sessions_remaining
}
this.mode = mode
this.count = count
// logger
this.logger = new Logger().getInstance()
// process
this.myProcess()
}
async exitHandler(evtOrExitCodeOrError) {
try {
await this.updateDB(false,true)
} catch (e) {
console.error('EXIT HANDLER ERROR', e);
}
process.exit(isNaN(+evtOrExitCodeOrError) ? 1 : +evtOrExitCodeOrError);
}
myProcess(){
[
'beforeExit', 'uncaughtException', 'unhandledRejection',
'SIGHUP', 'SIGINT', 'SIGQUIT', 'SIGILL', 'SIGTRAP',
'SIGABRT','SIGBUS', 'SIGFPE', 'SIGUSR1', 'SIGSEGV',
'SIGUSR2', 'SIGTERM',
].forEach(evt => process.on(evt, (evt)=> {this.exitHandler(evt)}));
}
dateCreator(session_time){
var dateObj = Date.now()
this.session_time_raw = session_time
dateObj += session_time*this.tick*this.tick_frequency;
return new Date(dateObj)
}
timeSubtractor(){
var now
if (this.paused || this.stopped){
now = this.paused_time
}else{
now = new Date();
}
var session = this.session_time;
var diffMs = (session-now); // milliseconds between now & Christmas
var diffDays = Math.floor(diffMs / 86400000); // days
var diffHrs = Math.floor((diffMs % 86400000) / 3600000); // hours
var diffMins = Math.floor(((diffMs % 86400000) % 3600000) / 60000); // minutes
var diffSeconds = Math.round((((diffMs % 86400000) % 3600000) % 60000) / 1000); // minutes
var diffString = ``
if (diffDays > 0)
{
diffString += `${diffDays} days, `
}
if (diffHrs > 0)
{
diffString += `${diffHrs} hours, `
}
if (diffMins > 0)
{
diffString += `${diffMins} minutes, `
}
diffString += `${diffSeconds} seconds`
return diffString
}
getStatus(){
var currentOrPaused
if (this.paused){
currentOrPaused = ">>PAUSED<<"
}else{
currentOrPaused = ">>RUNNING<<"
}
let statusString = `
Current Status: ${this.session_status}
Time remaining in the ${currentOrPaused} session is: ${this.timeSubtractor()}
You have done ${this.pomodoro_counter} in this session and
this channel has completed ${this.total_pomodoros} pomodoros so far!
sessions remaining until long break: ${this.sessions_remaining}
`
this.log("got status update!")
return statusString;
}
async updateDB(increment = false,exiting = false){
if (exiting){
this.pause(false)
}
this.MyUser.work = this.work
this.MyUser.short_break = this.short_break
this.MyUser.long_break = this.long_break
this.MyUser.sessions = this.sessions
this.MyUser.pausable = this.pausable
this.MyUser.paused = this.paused
this.MyUser.paused_tick = this.paused_tick
this.MyUser.stopped = this.stopped
this.MyUser.session_status = this.session_status
this.MyUser.session_time = this.session_time
this.MyUser.pomodoro_counter = this.pomodoro_counter
if (increment === true){
this.MyUser.total_pomodoros += 1
this.log("Total Pomodoros: " + this.MyUser.total_pomodoros )
}
this.MyUser.tick = this.tick
this.MyUser.count_in_min = this.count_in_min
this.MyUser.session_time_raw = this.session_time_raw
this.MyUser.sessions_remaining = this.sessions_remaining
this.MyUser.mode = this.mode
this.MyUser.count = this.count
this.total_pomodoros = this.MyUser.total_pomodoros
this.log("Saving User");
await this.MyUser.save()
this.log("User Saved");
}
start()
{
this.stopped = false
this.paused = false
this.session_status = "Work session is in progress!"
this.session_time = this.dateCreator(this.work)
this.log(this.session_status)
this.interval = setInterval(()=>{this.doWork()}, this.tick)
var myobj = { _id: this.channelId, total_pomodoros: 0 };
this.User.findById(this.channelId, (err,user) => {
if (err || !user){
this.MyUser = new this.User(myobj)
this.updateDB()
}else{
this.MyUser = user
this.total_pomodoros = this.MyUser.total_pomodoros
this.log("User already exists in the DB!")
}
})
this.count = 0
}
log(text){
var text = `${this.channelId}>> ${text}`
this.logger.info(text)
}
doWork(){
try{
this.count_in_min = Math.floor(this.count / this.tick_frequency)
this.log(`${this.count_in_min}, ${this.count}, ${this.sessions}, ${this.sessions_remaining}, ${this.mode,this.session_time}, ${this.mode}, ${this.tick_frequency}`)
if (this.count_in_min == this.work && this.sessions_remaining > 1 && this.mode == 1)
{
this.pomodoro_counter++
console.log(this.session_time,this.short_break, this.dateCreator(this.short_break))
this.session_time = this.dateCreator(this.short_break)
this.session_status = "Short break session is in progress!"
this.log(this.session_status)
this.channel.send(`Work session ended! Short Break starts now!!\n${this.getStatus()}`)
this.sessions_remaining--
this.mode = 2
this.count = 0
}else if (this.count_in_min == this.work && this.sessions_remaining === 1 && this.mode == 1){
this.pomodoro_counter++
this.session_time = this.dateCreator(this.long_break)
this.session_status = "Long break session is in progress!"
this.log(this.session_status)
this.channel.send(`Work session ended! Long Break starts now!!\n${this.getStatus()}`)
this.sessions_remaining = this.sessions
this.mode = 3
this.count = 0
} else if (this.count_in_min == this.short_break && this.mode == 2 ) {
const StartNextButton = new MessageActionRow()
.addComponents(
new MessageButton()
.setCustomId('StartNextPomodoro')
.setLabel('Start')
.setStyle('SUCCESS')
);
this.paused = true
this.session_time = Date.now()
this.paused_time = Date.now()
this.channel.send({ content: 'Start the next pomodoro!', components: [StartNextButton] });
clearInterval(this.interval)
this.updateDB(true)
} else if (this.count_in_min == this.long_break && this.mode == 3) {
const StartNextButton = new MessageActionRow()
.addComponents(
new MessageButton()
.setCustomId('StartNextPomodoro')
.setLabel('Start')
.setStyle('SUCCESS')
);
this.paused = true
this.session_time = Date.now()
this.paused_time = Date.now()
this.channel.send({ content: 'Start the next pomodoro!', components: [StartNextButton] });
clearInterval(this.interval)
this.updateDB(true)
}
this.count++;
} catch(e){
if (e instanceof TypeError){
this.stop()
}else{
this.log(e)
}
}
}
stop(){
this.log("PomPom has stopped")
clearInterval(this.interval)
this.stopped = true
}
pause(save = true){
this.paused_time = Date.now()
this.paused = true
this.paused_tick = this.count_in_min
clearInterval(this.interval)
if (save)
this.updateDB()
this.log("PomPom is paused")
}
unpause(save = true){
this.paused = false
this.session_time = this.dateCreator(this.session_time_raw - this.paused_tick)
if (save)
this.updateDB()
this.interval = setInterval(()=>{this.doWork()}, this.tick)
this.log("PomPom is unpaused")
}
restore(){
this.User.findById(this.channelId, (err,user) => {
if (!err && user){
this.MyUser = user
this.total_pomodoros = this.MyUser.total_pomodoros
this.log("User already exists in the DB!")
if (this.stopped === false && this.mode === 1){
this.log("User Restored")
this.log(this.session_status)
this.unpause(false)
}else if(this.stopped === true){
this.session_time = Date.now()
this.paused_time = Date.now()
}else if (this.mode > 1){
this.session_time = Date.now()
this.paused_time = Date.now()
}
}
})
}
startNextPomodoroButton(){
if (this.mode >= 2){
clearInterval(this.interval)
this.session_time = this.dateCreator(this.work)
this.session_status = "Work session is in progress!"
this.paused = false
this.log(this.session_status)
if (this.mode === 2){
this.channel.send(`Short break session ended! Work starts now!!\n${this.getStatus()}`)
}else if (this.mode === 3){
this.channel.send(`Long break session ended! Work starts now!!\n\n${this.getStatus()}`)
}
this.mode = 1
this.count = 0
this.updateDB()
this.interval = setInterval(()=>{this.doWork()}, this.tick)
}
}
}
module.exports = Pomo;