-
Notifications
You must be signed in to change notification settings - Fork 17
/
cron.js
88 lines (77 loc) · 2.66 KB
/
cron.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
const cron = require('node-cron')
const send = require('./app/send')
const task = require('./app/controllers/task')
const actions = require('./app/actions')
const conf = require('./conf.json')
// Active quests
const activeTask = []
// Run active tasks
cron.schedule(conf.cron, async () => {
try {
const list = await task.currentList()
if (list === null) throw new Error('No active assignments')
for (let item of list) {
const id = item._id.toString()
// Missing running tasks
if (activeTask.includes(id)) continue
switch (item.type) {
case 'Лайк + Подписка':
activeTask.push(id)
actions
.followLike(item)
.then(res => {
// Remove from list
const keyActiveTask = activeTask.indexOf(id)
delete activeTask[keyActiveTask]
if (res.name === 'AuthenticationError') {
send.message(
item.user,
`⛔️ При входе в ${item.login} возникла ошибка, отредактируйте акккаунт!`
)
throw new Error('Ошибка авторизации')
}
// notify the user when the task is completed
if (res) {
send.message(
item.user,
`Задание ${item.type} завершено для аккаунта ${item.login}`
)
}
})
.catch(e => console.log(e))
break
case 'Отписка':
activeTask.push(id)
actions
.unFollow(item)
.then(res => {
// Remove from list
const keyActiveTask = activeTask.indexOf(id)
delete activeTask[keyActiveTask]
if (res.name === 'AuthenticationError') {
send.message(
item.user,
`⛔️ При входе в ${item.login} возникла ошибка, отредактируйте акккаунт!`
)
throw new Error('Ошибка авторизации')
}
// notify the user when the task is completed
if (res) {
send.message(
item.user,
`Задание ${item.type} завершено для аккаунта ${item.login}`
)
}
})
.catch(e => console.log(e))
break
default:
// Job type is not defined
break
}
}
} catch (e) {
// No active assignments
return e
}
})