-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdater.js
163 lines (142 loc) · 4.02 KB
/
updater.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
const path = require('path')
require('dotenv').config({ path: path.resolve('.env') })
console.log('Starting updater')
const Contest = require('./models/contest')
const Bot = require('./models/bot')
const Participant = require('./models/participant')
require('./models')
const play = require('./helpers/play')
const postsUpdate = require('./helpers/postsUpdate')
const postPublish = require('./helpers/postPublish')
const postCreate = require('./helpers/postCreate')
const { Telegraf } = require('telegraf')
const bot = new Telegraf('', { handlerTimeout: 1 })
const I18n = require('telegraf-i18n')
const i18n = new I18n({
directory: 'locales',
defaultLanguage: 'ru',
defaultLanguageOnMissing: true
})
// eslint-disable-next-line no-extend-native
Number.prototype.format = function (n, x) {
const re = '\\d(?=(\\d{' + (x || 3) + '})+' + (n > 0 ? '\\.' : '$') + ')'
return this.toFixed(Math.max(0, ~~n)).replace(new RegExp(re, 'g'), '$& ')
}
const schedule = require('node-schedule')
schedule.scheduleJob('* * * * *', async () => {
const now = new Date()
now.setSeconds(0)
now.setMilliseconds(0)
const [playAt, posts, publish] = await Promise.all([
Contest.find({
'config.status': false,
'config.playAt': {
$gte: now.getTime(),
$lt: now.getTime() + 60000,
$exists: true
},
deleted: false
}),
Contest.find({
posts: { $exists: true, $ne: [] },
deleted: false
}),
Contest.find({
'post.time': {
$gte: now.getTime(),
$lt: now.getTime() + 60000,
$exists: true
},
deleted: false
})
])
for (let contest of playAt) {
const findBot = await Bot.findOne({ id: contest.bot })
bot.token = findBot.token
bot.username = findBot.username
const result = await play(contest, bot.telegram)
if (result)
contest = await Contest.findOneAndUpdate(
{ _id: contest._id },
{ 'config.status': true, 'config.playAt': undefined },
{ new: true }
)
console.log(
`${new Date().toLocaleString('ru')} contest ${contest._id} played`
)
await Promise.all([
postsUpdate(contest, bot),
bot.telegram.sendMessage(
contest.creator,
i18n.t(contest.language, 'playAt.planned', { name: contest.name }),
{
parse_mode: 'HTML'
}
)
])
}
for (const contest of posts) {
const findBot = await Bot.findOne({ id: contest.bot })
bot.token = findBot.token
bot.username = findBot.username
const membersCount = await Participant.countDocuments({
contest: contest._id,
status: { $nin: ['banned', 'unsubscribed'] }
})
if (
contest.config.status !== contest.lastPostsUpdate?.status ||
membersCount !== contest.lastPostsUpdate?.membersCount
) {
console.log(
`${new Date().toLocaleString('ru')} contest ${
contest._id
} posts updated`
)
await postsUpdate(contest, bot)
}
}
for (const contest of publish) {
const findBot = await Bot.findOne({ id: contest.bot })
bot.token = findBot.token
bot.username = findBot.username
const post = await postCreate(
contest,
contest.config.status,
contest.language,
bot.username
)
const messages = await Promise.all(
contest.post.channels.map((channel) =>
postPublish(bot.telegram, channel.id, post)
)
)
await Contest.updateOne(
{ _id: contest._id },
{
$addToSet: {
posts: {
$each: messages.map((message) => ({
messageId: message.message_id,
chatId: message.chat.id,
type: post.type
}))
}
}
}
)
await bot.telegram.sendMessage(
contest.creator,
i18n.t(contest.language, 'publish.plan.time.planned', {
name: contest.name
}),
{
parse_mode: 'HTML'
}
)
console.log(
`${new Date().toLocaleString('ru')} contest ${
contest._id
} posts published`
)
}
})