-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
321 lines (272 loc) · 10.1 KB
/
index.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
if (process.env.NODE_ENV !== 'production') {
require('dotenv').config()
}
const logger = require('./utils/signale')
const packageJson = require('./package.json')
if (/^v(\d+)\./.exec(process.version)[1] < packageJson.engines.node) {
logger.error('Please use ' + packageJson.engines.node + ' node version')
process.exit(1)
}
const client = require('./utils/discord')()
const mongoose = require('./utils/mongoose')
const Agenda = require('agenda')
const agenda = new Agenda()
const { userInfo } = require('./utils/user')
const { challengeInfo, getCategory } = require('./utils/challenge')
const { updateUsers, fetchChallenges } = require('./utils/updates')
const { getScoreboard, updateScoreboards } = require('./utils/scoreboard')
const { getAgenda: initAgendaVotes } = require('./utils/ctftime')
const { getLastChallenges } = require('./utils/rss')
const db = mongoose.connection
db.on('error', console.error.bind(console, 'connection error:'))
db.once('open', async function() {
logger.success('Mongo OK')
logger.info('Agenda deleted', (await db.collection('agenda').deleteMany({})).deletedCount)
/** Schema configuration **/
const userSchemaTemplate = {
id_auteur: { type: String, required: true, unique: true, index: true },
nom: { type: String, required: true, index: true },
statut: String,
score: { type: Number, default: 0, index: true },
position: { type: Number, default: 0, index: true },
challenges: Array,
solutions: Array,
validations: Array,
timestamp: Date
}
const categories = getCategory(null, true)
Object.keys(categories).forEach(key => {
userSchemaTemplate[`score_${key}`] = { type: Number, index: true }
})
const userSchema = new mongoose.Schema(userSchemaTemplate)
userSchema.index({ score: -1, nom: -1 })
userSchema.index({ score: -1, nom: 1 })
userSchema.index({ score: 1, nom: -1 })
userSchema.index({ score: 1, nom: 1 })
Object.keys(categories).forEach(key => {
userSchema.index({ [`score_${key}`]: -1, nom: -1 })
userSchema.index({ [`score_${key}`]: -1, nom: 1 })
userSchema.index({ [`score_${key}`]: 1, nom: -1 })
userSchema.index({ [`score_${key}`]: 1, nom: 1 })
})
userSchema.methods.userInfo = userInfo
mongoose.model('user', userSchema)
const challengeSchemaTemplate = {
id_challenge: { type: Number, required: true, unique: true, index: true },
id_rubrique: { type: Number, index: true },
titre: { type: String, index: true },
soustitre: String,
rubrique: String,
lang: String,
date_publication: Date,
score: { type: Number, index: true },
auteurs: Array,
validations: Number, // Deprecated
difficulte: String,
url_challenge: String,
timestamp: Date
}
const challengeSchema = new mongoose.Schema(challengeSchemaTemplate)
challengeSchema.methods.challengeInfo = challengeInfo
challengeSchema.index(
{ titre: 'text', rubrique: 'text' },
{ weights: { titre: 10, rubrique: 2 } }
)
const Challenges = mongoose.model('challenge', challengeSchema)
// Remove validations as it's not used anymore
await Challenges.updateMany({}, { $unset: { 'validations': 1 } })
const discordChannelsSchemaTemplate = {
channelId: { type: String, required: true, unique: true, index: true },
guildId: { type: String, required: true, unique: true, index: true },
users: Array,
scoreboard: {
type: {
messageId: { type: String, required: true, unique: true, index: true },
channelId: { type: String, required: true, unique: true, index: true }
},
required: false
}
}
const discordChannelsSchema = new mongoose.Schema(discordChannelsSchemaTemplate)
const Channels = mongoose.model('channels', discordChannelsSchema)
// User list synced to a role and a root-me user
const syncedUserSchemaTemplate = {
discordId: { type: String, required: true },
rootmeId: { type: String, required: true },
roleId: { type: String, required: true },
guildId: { type: String, required: true }
}
const syncedUserSchema = new mongoose.Schema(syncedUserSchemaTemplate)
syncedUserSchema.index({ guildId: 1, id_role: 1 })
mongoose.model('syncedusers', syncedUserSchema)
// Profile pictures cache
const profilePicturesSchemaTemplate = {
rootmeId: { type: String, required: true, unique: true, index: true },
image: { type: String, required: true }
}
const profilePicturesSchema = new mongoose.Schema(profilePicturesSchemaTemplate)
profilePicturesSchema.index({ rootmeId: 1 })
mongoose.model('profilepictures', profilePicturesSchema)
if (!await Challenges.countDocuments({})) {
logger.info('Loading challenges, please wait...')
try {
await fetchChallenges()
logger.success('Challenges loaded successfully')
} catch (err) {
logger.error('Error while loading challenges', err)
}
} else {
logger.info('Challenges already loaded')
}
/** Agenda configuration **/
let LOADING_CHALLENGES = false
let LOADING_USERS = false
agenda.define('UPDATE_USERS', {}, async (job, done) => {
if (LOADING_USERS === false) {
LOADING_USERS = true
updateUsers((await Channels.find({})).map(v => v.channelId)).then(() => {
logger.success('UPDATE_USERS OK')
LOADING_USERS = false
done()
}).catch(async err => {
logger.error('UPDATE_USERS ERROR', err)
setTimeout(() => {
LOADING_USERS = false
done()
}, 10000)
})
} else {
logger.info('Nope ! Already loading (users).')
done()
}
})
agenda.define('UPDATE_CHALLENGES', {}, async (job, done) => {
if (LOADING_CHALLENGES === false) {
LOADING_CHALLENGES = true
fetchChallenges((await Channels.find({})).map(v => v.channelId)).then(() => {
logger.success('UPDATE_CHALLENGES OK')
LOADING_CHALLENGES = false
done()
}).catch(async err => {
logger.error('UPDATE_CHALLENGES ERROR', err)
setTimeout(() => {
LOADING_CHALLENGES = false
done()
}, 10000)
})
} else {
logger.info('Nope ! Already loading (challenges).')
done()
}
})
agenda.define('GET_FRESH_CHALLENGES', {}, (job, done) => {
getLastChallenges().then(() => {
logger.success('GET_FRESH_CHALLENGES OK')
done()
}).catch(async err => {
logger.error('GET_FRESH_CHALLENGES ERROR', err)
done()
})
})
agenda.mongo(db.db, 'agenda')
agenda.on('ready', async () => {
setTimeout(() => {
agenda.start().then(() => {
logger.success('Agenda started successfully')
})
}, 2500)
if (!process.env.NO_UPDATE) {
if (!process.env.NO_UPDATE_USERS) {
const UPDATE_USERS = agenda.create('UPDATE_USERS', {}).priority('highest')
await UPDATE_USERS.repeatEvery(process.env.REPEAT_USERS || '40 seconds', { skipImmediate: false }).save()
}
if (!process.env.NO_UPDATE_CHALLENGES) {
const UPDATE_CHALLENGES = agenda.create('UPDATE_CHALLENGES', {}).priority('lowest')
await UPDATE_CHALLENGES.repeatEvery(process.env.REPEAT_CHALLENGES || '15 minutes', { skipImmediate: false }).save()
}
if (!process.env.NO_UPDATE_RSS) {
const GET_FRESH_CHALLENGES = agenda.create('GET_FRESH_CHALLENGES', {}).priority('lowest')
await GET_FRESH_CHALLENGES.repeatEvery('10 minutes', { skipImmediate: false }).save()
}
}
})
/** Discord.js **/
client.once('ready', () => {
logger.success('Ready!')
try {
// Initialize agenda with the good db
const v = initAgendaVotes(db.db)
logger.success('Agenda (votes) initialized successfully')
v.on('ready', async () => {
logger.success('Agenda (votes) started successfully')
v.start()
})
} catch (err) {
logger.error('Error while initializing agenda (votes)', err)
}
setTimeout(() => {
updateScoreboards().catch(() => {
logger.error('Error while updating scoreboards')
})
}, 1500)
})
client.on('interactionCreate', async interaction => {
if (!interaction.isCommand()) return
if (!client.commands.has(interaction.commandName)) return
try {
logger.log(interaction.commandName)
await client.commands.get(interaction.commandName).execute(interaction)
} catch (error) {
logger.error(error)
const content = 'There was an error while executing this command!'
try {
await interaction.reply({ content, ephemeral: true })
} catch (err) {
await interaction.editReply({ content, ephemeral: true }).catch((e) => {
logger.error('Error while editing error interaction', e)
})
logger.error(err)
}
}
})
client.on('interactionCreate', async interaction => {
if (!interaction.isButton()) return
try {
if (/^go_.+_page_/.test(interaction.customId)) {
const [, data,, page] = interaction.customId.split('_')
let category, role, roleObj
if (data.includes('::')) [category, role] = data.trim().split('::')
else category = data.trim()
if (role) {
const guild = client.guilds.cache.get(interaction.guildId)
if (guild) roleObj = guild.roles.cache.get(role)
if (!roleObj || !roleObj.id) return interaction.reply({ content: 'Le rôle n\'existe pas ou plus :(', ephemeral: true })
}
return interaction.update(await getScoreboard({
guildId: interaction.guildId,
index: Number(page) || 0,
category: Number(category) || undefined,
role: roleObj || undefined,
fromArrow: true
}))
} else if (interaction.customId.startsWith('go_page_')) {
const page = interaction.customId.match(/page_(\d+)/)?.[1]?.trim()
return interaction.update(await getScoreboard({
guildId: interaction.guildId,
index: Number(page) || 0,
fromArrow: true
}))
}
} catch (e) {
logger.error(e)
try {
await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true })
} catch (err) {
logger.error(err)
}
}
})
client.login(process.env.TOKEN).then(() => {
logger.success('Logged')
})
})