-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscheduled.js
745 lines (627 loc) · 25.2 KB
/
scheduled.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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
//Axios for requests
const axios = require('axios');
//Coc api Token and domain values
const { cocApiToken, cocApiDomain, weeksToSaveCapitalRaids } = require('./config.json');
//Database file
var db = require('./db');
//Config for api request
const config = { headers: { Authorization: `Bearer ${cocApiToken}` } };
//imports premade functions
const pre = require("./preset/premade.js");
const util = require('util');
// node native promisify
const query = util.promisify(db.query).bind(db);
//discord button builder
const { ActionRowBuilder, ButtonBuilder, ButtonStyle, SlashCommandBuilder, ApplicationCommandPermissionType, EmbedBuilder } = require('discord.js');
module.exports = {
start
};
const cron = require('node-cron');
const schedule = require('node-schedule');
const clan = require('./commands/clan.js');
const moment = require('moment-timezone')
const dcWarSchedduled = []
const dcLeagueSchedduled = []
const localTimeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
let client;
async function start(dc) {
client = dc;
cron.schedule('0 10 * * 1', capitalWeekendRaid);
//capitalWeekendRaid();
cron.schedule('0 0 * * *', warRequest);
cron.schedule('0 12 * * *', warRequest);
cron.schedule('0 15 * * *', clanWarLeagueCheck);
warRequest();
clanWarLeagueCheck();
}
async function clanWarLeagueCheck() {
const allServers = await query('SELECT * FROM guildToClan WHERE notifyChannelId IS NOT NULL;');
if (allServers && allServers.length) {
for (let item of allServers) {
const clantag = item.clanTag;
const guildID = item.guildID;
//check if not already schedduled
if (!dcLeagueSchedduled.includes(guildID)) {
await axios.get(cocApiDomain + '/v1/clans/' + pre.routConvert(clantag) + '/currentwar/leaguegroup', config)
.then(async function (response) {
//Check if all tags are there
let rounds = response.data.rounds;
let season = response.data.season;
let lastRound = rounds[rounds.length - 1];
if (lastRound.warTags[0] == "#0") {
console.log("last WarTag of CWL is still #0, waiting...");
}
else {
//If all the WarTags are there, take the last war and see when it ends
await axios.get(cocApiDomain + '/v1/clanwarleagues/wars/' + pre.routConvert(lastRound.warTags[0]), config)
.then(async function (warResponse) {
if (warResponse.data.state == "warEnded") {
//if war is ended, save now, this case should not be possible but idk
console.log("war of CWL is already ended, this should not be possible but yeah... triggering end NOW");
await clanWarLeagueEnd(guildID, clantag, item.notifyChannelId, rounds, season);
}
else {
const endingWarTime = moment.utc(warResponse.data.endTime, 'YYYYMMDDTHHmmss.SSS[Z]');
const apiCallTime = moment.utc(endingWarTime).add(1, 'minute').startOf('minute');
scheduleLeagueExecution(apiCallTime, guildID, clantag, item.notifyChannelId, rounds, season);
}
})
.catch(function (error) {
throw error;
});
}
})
.catch(function (error) {
if (error.response.status == 404 && error.response.data.reason == "notFound") {
console.error("clan not in CWL");
}
else {
throw error;
}
});
}
}
}
}
async function scheduleLeagueExecution(apiCallTime, guildID, clantag, notifyChannelId, rounds, season) {
const localEndingWarTime = apiCallTime.tz(localTimeZone);
const minute = localEndingWarTime.minute();
const hour = localEndingWarTime.hour();
const dayOfMonth = localEndingWarTime.date();
const month = localEndingWarTime.month();
const scheduleString = `${minute} ${hour} ${dayOfMonth} ${month + 1} *`;
dcLeagueSchedduled.push(guildID)
console.log("LeagueEnd schedduled for guild " + guildID + "(channel: " + notifyChannelId + ") with clan " + clantag + " at " + localEndingWarTime.toString());
const job = schedule.scheduleJob(scheduleString, () => {
// Remove guildID from arraY
const index = dcLeagueSchedduled.indexOf(guildID);
if (index > -1) {
dcLeagueSchedduled.splice(index, 1);
} else {
console.error("league: Something went wrong, guildid was not found in array but scheduled")
}
clanWarLeagueEnd(guildID, clantag, notifyChannelId, rounds, season);
});
}
async function clanWarLeagueEnd(guildID, clantag, notifyChannelId, rounds, season) {
var warIDs = [];
//Every round
for (let round of rounds) {
//check if all data is there, otherwise log error
for (let warTag of round.warTags) {
if (warTag == "#0") {
console.error("error: some warTag from CLW was still #0");
return;
}
}
//Every war in Round (just one of this is the correct clan though)
let warID = 0;
for (let warTag of round.warTags) {
await axios.get(cocApiDomain + '/v1/clanwarleagues/wars/' + pre.routConvert(warTag), config)
.then(async function (response) {
if (response.data.state == "warEnded") {
//searching for the correct clan
if (response.data.clan.tag == clantag) {
//console.log("True");
warID = await clanWarLeagueWarSave(response.data, response.data.clan, response.data.opponent, notifyChannelId, guildID);
}
else if (response.data.opponent.tag == clantag) {
//console.log("True");
warID = await clanWarLeagueWarSave(response.data, response.data.opponent, response.data.clan, notifyChannelId, guildID);
}
else {
//console.log("False");
}
}
else {
console.error("Some war has not ended, but it should have")
}
})
.catch(function (error) {
throw error;
});
if (warID != 0) {
//console.log("BREAK");
break; // Exit the loop if clan was found in a war of all the wars in thr group
}
//Wait between clanwarleague war request
await pre.delay(1500);
}
warIDs.push(warID);
}
await new Promise((resolve, reject) => {
db.query(
"INSERT INTO clanwarleagues (season, clanTag, guildID, warIDs) VALUES (?, ?, ?, ?)",
[
season,
clantag,
guildID,
JSON.stringify(warIDs)
],
(err, result, fields) => {
if (err) reject(err);
resolve();
}
);
});
let postMessage = "CWL is over!\nThank you to everyone who attacked! :heart: :crossed_swords: \nThis was season " + season + "\n\n Data will be saved now :)";
const buttonsRow1 = new ActionRowBuilder();
let currentRow = buttonsRow1; // Start adding buttons to the first row
var newButton = new ButtonBuilder()
.setCustomId(`cwllog_all_` + season)
.setLabel('All')
.setStyle(ButtonStyle.Primary);
currentRow.addComponents(newButton);
let buttonCount = 1;
for (const id of warIDs) {
const newButton = new ButtonBuilder()
.setCustomId(`cwllog_${id}`)
.setLabel(`Day ${buttonCount}`)
.setStyle(ButtonStyle.Primary);
currentRow.addComponents(newButton);
buttonCount++;
// Check if we need to create a second row
if (buttonCount === 5) {
currentRow = new ActionRowBuilder();
}
}
// Only include buttonsRow2 if it has buttons
const components = [buttonsRow1];
if (currentRow.components.length > 0) {
components.push(currentRow);
}
// Send the message with all the action rows
const channel = await client.channels.fetch(notifyChannelId);
await channel.send({ content: postMessage, components });
}
async function clanWarLeagueWarSave(war, ourClan, opponentClan, notifyChannelId, guildId) {
const channel = await client.channels.fetch(notifyChannelId);
let startWarTime = moment.utc(war.startTime, 'YYYYMMDDTHHmmss.SSS[Z]');
let warstartTimeSQL = startWarTime.format('YYYY-MM-DD');
var won;
if (ourClan.stars == opponentClan.stars) {
if (ourClan.destructionPercentage > opponentClan.destructionPercentage) {
won = "true";
}
else if (ourClan.destructionPercentage < opponentClan.destructionPercentage) {
won = "false";
}
else {
won = "same score";
}
}
else if (ourClan.stars > opponentClan.stars) {
won = "true";
}
else if (ourClan.stars < opponentClan.stars) {
won = "false";
}
else {
won = "error";
}
function getOpponentMapPosition(opponentMembers, memberTag) {
for (const member of opponentMembers) {
if (member.tag === memberTag) {
return member.mapPosition;
}
}
return null;
}
function getOpponentName(opponentMembers, memberTag) {
for (const member of opponentMembers) {
if (member.tag === memberTag) {
return member.name;
}
}
return null;
}
function fillWithZeroIfNotAttacked(arr) {
if (arr.length === 1) {
return arr;
}
for (let i = arr.length; i < 1; i++) {
arr.push(0);
}
return arr;
}
const attackPromise = new Promise((resolve, reject) => {
db.query(
"INSERT INTO clanwars (clanTag, isLeague, guildID, warStartDay, opponentTag, opponentName, won, teamSize, clanUsedAttacks, opponentUsedAttacks, clanStars, opponentStars, clanPercentage, opponentPercentage) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
[
ourClan.tag,
true,
guildId,
warstartTimeSQL,
opponentClan.tag,
opponentClan.name,
won,
war.teamSize,
ourClan.attacks,
opponentClan.attacks,
ourClan.stars,
opponentClan.stars,
ourClan.destructionPercentage,
opponentClan.destructionPercentage
],
(err, result, fields) => {
if (err) reject(err);
resolve(result.insertId);
}
);
});
const warId = await attackPromise;
for (let member of ourClan.members) {
var attackIDs = [];
if (member.hasOwnProperty('attacks')) {
for (const attack of member.attacks) {
const attackPrimise = new Promise((resolve, reject) => {
db.query(
"INSERT INTO clanwarattacks (defenderTag, defenderName, defenderPosition, stars, destructionPercentage) VALUES (?, ?, ?, ?, ?)",
[
attack.defenderTag,
getOpponentName(opponentClan.members, attack.defenderTag),
getOpponentMapPosition(opponentClan.members, attack.defenderTag),
attack.stars,
attack.destructionPercentage
],
(err, result, fields) => {
if (err) reject(err);
resolve(result.insertId);
}
);
});
attackIDs.push(await attackPrimise);
}
}
//Fill with 0, when attack1 or attack2 is 0 that means "didnt attack"
attackIDs = fillWithZeroIfNotAttacked(attackIDs);
db.query(
"INSERT INTO clanwarleaguemembers (warId, memberTag, memberName, memberPosition, attack) VALUES (?, ?, ?, ?, ?)",
[
warId,
member.tag,
member.name,
member.mapPosition,
attackIDs[0]
],
(err, result, fields) => {
if (err) throw (err);
}
);
}
return warId;
}
async function capitalWeekendRaid() {
const allServers = await query('SELECT * FROM guildToClan WHERE notifyChannelId IS NOT NULL');
if (allServers && allServers.length) {
for (let item of allServers) {
const clantag = item.clanTag;
const guildID = item.guildID;
await axios.get(cocApiDomain + '/v1/clans/' + pre.routConvert(clantag) + '/capitalraidseasons', config)
.then(async function (response) {
if (response.data.items.length > 0) {
const newestCapitalRaid = response.data.items[0];
let endTime = moment.utc(newestCapitalRaid.endTime, 'YYYYMMDDTHHmmss.SSS[Z]');
let startTime = moment.utc(newestCapitalRaid.startTime, 'YYYYMMDDTHHmmss.SSS[Z]');
let now = moment.utc();
let oneDaysAgo = moment.utc().subtract(3, 'days');
if (endTime.isBetween(oneDaysAgo, now) && newestCapitalRaid.state == "ended") {
//create an interaction, because i programmed updating like that...
const interaction = {};
interaction.guildId = guildID;
await pre.updateClanMembers(interaction);
await pre.delay(4000);
//for saving it in the database later
let membersListToSave = [];
const channel = await client.channels.fetch(item.notifyChannelId);
let startedAt = startTime.format('DD.MM.YYYY');
let endedAt = endTime.format('DD.MM.YYYY');
let postChunks = [];
postChunks.push("Raid Weekend is over!\nThank you to everyone who attacked! :heart:\n")
postChunks.push(startedAt + " - " + endedAt);
postChunks.push("\nTotal Loot: " + newestCapitalRaid.capitalTotalLoot);
postChunks.push("\nRaids completed: " + newestCapitalRaid.raidsCompleted);
postChunks.push("\nTotal attacks: " + newestCapitalRaid.totalAttacks);
postChunks.push("\nEnemy districts destroyed: " + newestCapitalRaid.enemyDistrictsDestroyed);
postChunks.push("\nOffensive reward: " + newestCapitalRaid.offensiveReward);
postChunks.push("\nDefensive reward: " + newestCapitalRaid.defensiveReward);
postChunks.push("\nMembers who attacked:\n");
var i = 1;
//everyone that attacked
let attackingmembers = newestCapitalRaid.members;
await attackingmembers.sort((a, b) => b.capitalResourcesLooted - a.capitalResourcesLooted);
for (let member of attackingmembers) {
let name = member.name;
let attacks = member.attacks;
let possibleAttacks = member.attackLimit + member.bonusAttackLimit;
let looted = member.capitalResourcesLooted;
postChunks.push(i + ". " + name + " (" + attacks + "/" + possibleAttacks + " attacks, " + looted + " looted)\n");
//add to list for saving it later
membersListToSave.push({ tag: member.tag, knownName: name, attacked: attacks });
i++;
}
//Find who didnt attack
const users = await query('SELECT userTag, userName FROM users WHERE guildID = ' + item.guildID);
if (users && users.length) {
let notAttacking = users.filter(user => {
return !attackingmembers.some(attacker => attacker.tag === user.userTag);
});
if (notAttacking.length > 0) {
postChunks.push("\n\nThese people did not attack this week:\n");
let i = 1;
for (let notAttackingMember of notAttacking) {
postChunks.push(i + ". " + notAttackingMember.userName + " (" + notAttackingMember.userTag + ")\n");
//add to list for saving it later
membersListToSave.push({ tag: notAttackingMember.userTag, knownName: notAttackingMember.userName, attacked: 0 });
i++;
}
}
else {
postChunks.push("\n\nIn this Raid everyone from your clan attacked!");
}
}
postChunks.push("\nThis will now save to the database :)");
const chunks = [];
const maxLength = 1999;
let currentChunk = '';
postChunks.forEach(line => {
if ((currentChunk + line).length <= maxLength) {
currentChunk += line;
} else {
chunks.push(currentChunk.trim());
currentChunk = line;
}
});
if (currentChunk.length > 0) {
chunks.push(currentChunk.trim());
}
for (let chunk of chunks) {
await channel.send({ content: chunk });
}
for (let saveMember of membersListToSave) {
db.query("INSERT INTO capitalRaids (timeAdded, clanTag, guildID, memberTag, memberName, attacked) VALUES (NOW(), " + db.escape(clantag) + ", " + db.escape(guildID) + ", " + db.escape(saveMember.tag) + ", " + db.escape(saveMember.knownName) + ", " + db.escape(saveMember.attacked) + ")",
function (err, result, fields) {
if (err) throw err;
});
}
}
}
})
.catch(function (error) {
throw error;
});
}
}
//Delete everything thats older than specified weeks
/*db.query("DELETE FROM capitalRaids WHERE timeAdded < DATE_SUB(NOW(), INTERVAL " + weeksToSaveCapitalRaids + " WEEK);",
function (err, result, fields) {
if (err) throw err;
});*/
}
const fs = require('fs').promises;
async function scheduleWarExecution(apiCallTime, guildID, clantag, notifyChannelId) {
const localEndingWarTime = apiCallTime.tz(localTimeZone);
const minute = localEndingWarTime.minute();
const hour = localEndingWarTime.hour();
const dayOfMonth = localEndingWarTime.date();
const month = localEndingWarTime.month();
const scheduleString = `${minute} ${hour} ${dayOfMonth} ${month + 1} *`;
dcWarSchedduled.push(guildID)
console.log("War schedduled for guild " + guildID + "(channel: " + notifyChannelId + ") with clan " + clantag + " at " + localEndingWarTime.toString());
const job = schedule.scheduleJob(scheduleString, () => {
//remove from array
const index = dcWarSchedduled.indexOf(guildID);
if (index > -1) {
dcWarSchedduled.splice(index, 1);
} else {
console.error("war: Something went wrong, guildid was not found in array but scheduled")
}
warOver(guildID, clantag, notifyChannelId);
});
}
async function warOver(guildId, clanTag, notifyChannelId) {
await axios.get(cocApiDomain + '/v1/clans/' + pre.routConvert(clanTag) + '/currentwar', config)
.then(async function (response) {
const war = response.data;
const channel = await client.channels.fetch(notifyChannelId);
let startPrepTime = moment.utc(war.preparationStartTime, 'YYYYMMDDTHHmmss.SSS[Z]');
let startWarTime = moment.utc(war.startTime, 'YYYYMMDDTHHmmss.SSS[Z]');
let endWarTime = moment.utc(war.endTime, 'YYYYMMDDTHHmmss.SSS[Z]');
let warstartTimeSQL = startWarTime.format('YYYY-MM-DD');
let startedAt = startWarTime.format('DD.MM.YYYY');
let endedAt = endWarTime.format('DD.MM.YYYY');
//create an interaction, because i programmed updating like that...
/*const interaction = {};
interaction.guildId = guildId;
await pre.updateClanMembers(interaction);
await pre.delay(4000);*/
let postChunks = [];
//Calculation, because the war ends in a few secs
var won;
if (war.clan.stars == war.opponent.stars) {
if (war.clan.destructionPercentage > war.opponent.destructionPercentage) {
won = "true";
}
else if (war.clan.destructionPercentage < war.opponent.destructionPercentage) {
won = "false";
}
else {
won = "same score";
}
}
else if (war.clan.stars > war.opponent.stars) {
won = "true";
}
else if (war.clan.stars < war.opponent.stars) {
won = "false";
}
else {
won = "error";
}
let postMessage = "Clanwar is over!\nThank you to everyone who attacked! :heart: :crossed_swords:";
const warInfo = new EmbedBuilder()
.setColor(0x0099FF)
.setTitle("War info")
.setDescription(startedAt?.toString() + " - " + endedAt?.toString())
.addFields(
{ name: 'Won', value: won },
{ name: 'War size', value: (war.teamSize?.toString() || "N/A") },
{ name: 'Attacks used', value: `${war.clan?.attacks || "N/A"} - ${war.opponent?.attacks || "N/A"}` },
{ name: 'Stars', value: `${war.clan?.stars || "N/A"} - ${war.opponent?.stars || "N/A"}` },
{ name: 'Percentage destroyed', value: `${war.clan?.destructionPercentage || "N/A"} - ${war.opponent?.destructionPercentage || "N/A"}` },
)
.setTimestamp()
.setFooter({ text: 'This will be saved now :)' });
const warLogButton = new ButtonBuilder()
.setCustomId('warlog_' + warstartTimeSQL)
.setLabel('War Log')
.setStyle(ButtonStyle.Primary);
const buttons = new ActionRowBuilder()
.addComponents(warLogButton);
await channel.send({ content: postMessage, embeds: [warInfo], components: [buttons] });
function getOpponentMapPosition(clanWarData, memberTag) {
const opponentMembers = clanWarData.opponent.members;
for (const member of opponentMembers) {
if (member.tag === memberTag) {
return member.mapPosition;
}
}
return null;
}
function getOpponentName(clanWarData, memberTag) {
const opponentMembers = clanWarData.opponent.members;
for (const member of opponentMembers) {
if (member.tag === memberTag) {
return member.name;
}
}
return null;
}
function fillToTwoWithNumZero(arr) {
if (arr.length === 2) {
return arr;
}
for (let i = arr.length; i < 2; i++) {
arr.push(0);
}
return arr;
}
const attackPromise = new Promise((resolve, reject) => {
db.query(
"INSERT INTO clanwars (clanTag, isLeague, guildID, warStartDay, opponentTag, opponentName, won, teamSize, clanUsedAttacks, opponentUsedAttacks, clanStars, opponentStars, clanPercentage, opponentPercentage) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
[
clanTag,
false,
guildId,
warstartTimeSQL,
war.opponent.tag,
war.opponent.name,
won,
war.teamSize,
war.clan.attacks,
war.opponent.attacks,
war.clan.stars,
war.opponent.stars,
war.clan.destructionPercentage,
war.opponent.destructionPercentage
],
(err, result, fields) => {
if (err) reject(err);
resolve(result.insertId);
}
);
});
const warId = await attackPromise;
for (let member of war.clan.members) {
var attackIDs = [];
if (member.hasOwnProperty('attacks')) {
for (const attack of member.attacks) {
const attackPrimise = new Promise((resolve, reject) => {
db.query(
"INSERT INTO clanwarattacks (defenderTag, defenderName, defenderPosition, stars, destructionPercentage) VALUES (?, ?, ?, ?, ?)",
[
attack.defenderTag,
getOpponentName(war, attack.defenderTag),
getOpponentMapPosition(war, attack.defenderTag),
attack.stars,
attack.destructionPercentage
],
(err, result, fields) => {
if (err) reject(err);
resolve(result.insertId);
}
);
});
attackIDs.push(await attackPrimise);
}
}
//Fill with 0, when attack1 or attack2 is 0 that means "didnt attack"
attackIDs = fillToTwoWithNumZero(attackIDs);
db.query(
"INSERT INTO clanwarmembers (warId, memberTag, memberName, memberPosition, attack1, attack2) VALUES (?, ?, ?, ?, ?, ?)",
[
warId,
member.tag,
member.name,
member.mapPosition,
attackIDs[0],
attackIDs[1]
],
(err, result, fields) => {
if (err) throw (err);
}
);
}
})
.catch(function (error) {
throw error;
});
}
async function warRequest() {
const allServers = await query('SELECT * FROM guildToClan WHERE notifyChannelId IS NOT NULL'); //466971916108824577
if (allServers && allServers.length) {
for (let item of allServers) {
const clantag = item.clanTag;
const guildID = item.guildID;
await axios.get(cocApiDomain + '/v1/clans/' + pre.routConvert(clantag) + '/currentwar', config)
.then(async function (response) {
const war = response.data;
const endingWarTime = moment.utc(war.endTime, 'YYYYMMDDTHHmmss.SSS[Z]');
const apiCallTime = moment.utc(endingWarTime).startOf('minute');
if (war.state == "inWar" /*|| war.state == "preparation"*/) {
if (!dcWarSchedduled.includes(guildID)) {
scheduleWarExecution(apiCallTime, guildID, clantag, item.notifyChannelId);
}
}
})
.catch(function (error) {
throw error;
});
}
}
//Delete everything thats older than specified weeks
/*db.query("DELETE FROM capitalRaids WHERE timeAdded < DATE_SUB(NOW(), INTERVAL " + weeksToSaveCapitalRaids + " WEEK);",
function (err, result, fields) {
if (err) throw err;
});*/
}