-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
432 lines (417 loc) · 14.2 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
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
// Require the necessary discord.js classes
const fs = require('node:fs');
const path = require('node:path');
const { Events, EmbedBuilder, userMention, bold } = require('discord.js');
const mongoose = require('mongoose');
const dotenv = require('dotenv');
const { client } = require('./client.js');
const Notification = require('./models/notification');
const User = require('./models/user');
const Duel = require('./models/duel');
const Config = require('./models/config');
const register = require('./buttons/register');
const play = require('./buttons/play');
const leaderboard = require('./buttons/leaderboard');
const { rooms } = require('./constants');
dotenv.config();
mongoose.connect(process.env.MONGODB_URI);
const commandsPath = path.join(__dirname, 'commands');
const commandFiles = fs
.readdirSync(commandsPath)
.filter((file) => file.endsWith('.js'));
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
// Set a new item in the Collection with the key as the command name and the value as the exported module
if ('data' in command && 'execute' in command) {
client.commands.set(command.data.name, command);
} else {
console.log(
`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`
);
}
}
client.on(Events.InteractionCreate, async (interaction) => {
if (interaction.isChatInputCommand()) {
const command = interaction.client.commands.get(interaction.commandName);
if (!command) {
console.error(
`No command matching ${interaction.commandName} was found.`
);
return;
}
try {
await command.execute(interaction);
} catch (error) {
console.error(error);
if (interaction.replied || interaction.deferred) {
await interaction.followUp({
content: 'There was an error while executing this command!',
ephemeral: true
});
} else {
await interaction.reply({
content: 'There was an error while executing this command!',
ephemeral: true
});
}
}
} else if (interaction.isButton()) {
if (interaction.customId === 'registerButton') {
await register(interaction);
} else if (interaction.customId === 'playButton') {
await play(interaction);
} else if (interaction.customId === 'leaderboardButton') {
await leaderboard(interaction);
}
}
});
const sendMessageAndPin = async (channel, message) => {
const sentMessage = await channel.send(message);
await sentMessage.pin();
return sentMessage;
};
setInterval(async () => {
if (mongoose.connection.readyState !== 1) return;
const session = await mongoose.startSession();
try {
await session.withTransaction(async () => {
const [
sudden_deaths,
inactivity_deaths,
inactivity_health,
sudden_health
] = await Promise.all([
Notification.find({ type: 'sudden_death' })
.session(session)
.limit(20)
.lean(),
Notification.find({
type: 'inactivity_death'
})
.session(session)
.limit(20)
.lean(),
Notification.find({
type: 'inactivity_health'
})
.session(session)
.limit(20)
.lean(),
Notification.find({
type: 'sudden_health'
})
.session(session)
.limit(20)
.lean()
]);
const inactivity_health_reduced = inactivity_health.filter(
(i) =>
!sudden_deaths.some((j) => j.discord_id === i.discord_id) &&
!inactivity_deaths.some((j) => j.discord_id === i.discord_id)
);
const sudden_health_reduced = sudden_health.filter(
(i) =>
!sudden_deaths.some((j) => j.discord_id === i.discord_id) &&
!inactivity_deaths.some((j) => j.discord_id === i.discord_id)
);
const embeds = [
{
title: ':rotating_light: Overload Protocol :rotating_light:',
description: `These players have suddenly died:\n\n${sudden_deaths
.map((i) => `${userMention(i.discord_id)}\n`)
.join('')}`,
array: sudden_deaths,
array_reduced: sudden_deaths
},
{
title: ':skull: Inactivity Deaths :skull:',
description: `These players have died since they were inactive:\n\n${inactivity_deaths
.map((i) => `${userMention(i.discord_id)}\n`)
.join('')}`,
array: inactivity_deaths,
array_reduced: inactivity_deaths
},
{
title: '',
description: `:mending_heart: These players have lost ${bold(
'5 HP'
)} since they were inactive:\n\n${inactivity_health_reduced
.map((i) => `${userMention(i.discord_id)}\n`)
.join('')}`,
array: inactivity_health,
array_reduced: inactivity_health_reduced
},
{
title: '',
description: `:mending_heart: These players have lost ${bold(
'5 HP'
)} since overload protocol is active:\n\n${sudden_health_reduced
.map((i) => `${userMention(i.discord_id)}\n`)
.join('')}`,
array: sudden_health,
array_reduced: sudden_health_reduced
}
];
try {
const channel =
(await client.channels.cache.get(rooms.feed)) ||
(await client.channels.fetch(rooms.feed));
if (channel) {
// Winner message
const alive_count = await User.countDocuments({
health_points: { $gt: 0 }
}).session(session);
if (alive_count <= 1) {
const config = await Config.findOne({ id: 0 }).session(session);
const winner = (
await User.aggregate(
[
{
$match: {
health_points: { $gt: 0 }
}
},
{
$lookup: {
from: 'stats',
localField: 'discord_id',
foreignField: 'discord_id',
as: 'stats'
}
},
{
$set: {
stats: {
$first: '$stats'
}
}
}
],
{ session }
)
)?.at(0);
const is_game_started = config?.game_start_date
? new Date() >= config?.game_start_date
: false;
if (is_game_started && !config?.is_game_over) {
let deadUsers = await User.aggregate([
{
$lookup: {
from: 'stats',
localField: 'discord_id',
foreignField: 'discord_id',
as: 'stats'
}
},
{
$lookup: {
from: 'deaths',
localField: 'discord_id',
foreignField: 'discord_id',
as: 'deaths'
}
},
{
$set: {
deaths: {
$first: '$deaths'
}
}
},
{
$match: {
deaths: {
$exists: true
}
}
},
{
$set: {
stats: {
$first: '$stats'
}
}
},
{
$sort: {
'deaths.doc_created_at': -1,
health_points: -1,
'stats.kills': -1,
'stats.inflicted_damage': -1
}
},
{
$limit: 20
}
]);
// Update the health points of the dead users to 0 for display purposes
deadUsers = deadUsers.map((i) => ({ ...i, health_points: 0 }));
const prizes = [
'$150 + Spinner NFT',
'$100 + Spinner NFT',
'$50 + Spinner NFT',
'$30 + Spinner NFT',
'$20 + Spinner NFT',
'$15 + Spinner NFT',
'$15 + Spinner NFT',
'$15 + Spinner NFT',
'$15 + Spinner NFT',
'$15 + Spinner NFT',
'$10 + Tier 1 Weapon NFT',
'$10 + Tier 1 Weapon NFT',
'$10 + Tier 1 Weapon NFT',
'$10 + Tier 1 Weapon NFT',
'$10 + Tier 1 Weapon NFT',
'$5 + Tier 1 Weapon NFT',
'$5 + Tier 1 Weapon NFT',
'$5 + Tier 1 Weapon NFT',
'$5 + Tier 1 Weapon NFT',
'$5 + Tier 1 Weapon NFT'
];
const embedBuilder = new EmbedBuilder()
.setTitle(':tada: We have a winner! :tada:')
.setDescription(
`${userMention(
winner ? winner.discord_id : deadUsers[0].discord_id
)} has won the game! \n\n` +
bold(
'Pilot Name | Health Points | Kill Count | Damage Inflicted | Prize\n\n'
) +
(winner ? [winner, ...deadUsers] : deadUsers)
.slice(0, 20)
.map(
(user, index) =>
`${index + 1}. ${userMention(user.discord_id)} | ${
user.health_points
} | ${user.stats?.kills ?? 0} | ${
user.stats?.inflicted_damage ?? 0
} | ${bold(prizes[index])}`
)
.join('\n')
);
await sendMessageAndPin(channel, { embeds: [embedBuilder] });
config.is_game_over = true;
await config.save({ session });
}
}
for await (const embed of embeds) {
if (embed.array_reduced.length) {
if (embed.title) {
const embedBuilder = new EmbedBuilder()
.setTitle(embed.title)
.setDescription(embed.description);
await channel.send({ embeds: [embedBuilder] });
} else {
const embedBuilder = new EmbedBuilder().setDescription(
embed.description
);
await channel.send({ embeds: [embedBuilder] });
}
}
}
const arraysCombined = embeds.reduce((acc, cur) => {
acc.push(...cur.array);
return acc;
}, []);
if (arraysCombined.length) {
await Notification.deleteMany(
{ discord_id: { $in: arraysCombined.map((i) => i.discord_id) } },
{ session }
);
}
}
} catch (error) {
console.error(error);
}
});
} catch (error) {
// await session.abortTransaction();
console.error('Transaction aborted:', error);
} finally {
await session.endSession();
}
}, 5000);
setInterval(async () => {
if (mongoose.connection.readyState !== 1) return;
const session = await mongoose.startSession();
try {
await session.withTransaction(async () => {
await Duel.updateMany(
{
is_duel_in_progress: true,
doc_updated_at: { $lt: new Date(Date.now() - 1000 * 60) }
},
{ is_duel_in_progress: false },
{ session }
);
});
} catch (error) {
console.error('Transaction aborted:', error);
} finally {
await session.endSession();
}
}, 10000);
setInterval(async () => {
if (mongoose.connection.readyState !== 1) return;
const session = await mongoose.startSession();
try {
await session.withTransaction(async () => {
const config = await Config.findOne({ id: 0 }).session(session);
const channel =
(await client.channels.cache.get(rooms.game)) ||
(await client.channels.fetch(rooms.game));
if (!channel) return;
let next_ep_regen_date = new Date(config?.game_start_date);
// TODO: Add another variable to config for the EP regen interval
next_ep_regen_date.setHours(next_ep_regen_date.getHours() - 1);
while (next_ep_regen_date < Date.now()) {
const new_date = new Date(next_ep_regen_date);
new_date.setTime(new_date.getTime() + 4 * 60 * 60 * 1000);
next_ep_regen_date = new_date;
}
const messageContent = `Next EP regen <t:${Math.floor(
next_ep_regen_date.getTime() / 1000
)}:R>`;
const messages = await channel.messages.fetchPinned();
const epRegenMessages = messages.filter((i) =>
i.content.startsWith('Next EP regen')
);
// TODO: Test this code block.
if (config?.is_game_over) {
for await (const message of epRegenMessages.values()) {
await message.delete();
}
return;
}
if (epRegenMessages.size > 1) {
for await (const message of epRegenMessages.values()) {
await message.delete();
}
await sendMessageAndPin(channel, messageContent);
} else if (epRegenMessages.size === 1) {
const message = epRegenMessages.first();
const timestampSecond = message.content
.slice(message.content.indexOf('<t:'), messageContent.indexOf(':R>'))
.replace('<t:', '');
if (
Math.floor(next_ep_regen_date.getTime() / 1000) >
parseInt(timestampSecond)
) {
await message.edit(messageContent);
}
} else {
const isGameStarted = config.game_start_date
? new Date() >= config.game_start_date
: false;
if (isGameStarted) {
await sendMessageAndPin(channel, messageContent);
}
}
});
} catch (error) {
console.error('Transaction aborted:', error);
} finally {
await session.endSession();
}
}, 10000);