-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
89 lines (72 loc) · 2.69 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
const { Client, GatewayIntentBits } = require('discord.js');
const fs = require('fs');
const initialState = require('./initialState.json');
require('dotenv').config();
const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent] });
const announcementChannelId = process.env.ANNOUNCEMENT_CHANNEL_ID;
const startDate = new Date();
const endDate = new Date("2023-04-16T16:00:00-04:00");
const challengeInterval = 24 * 60; // 1 day in minutes
const teamNames = ['Darkrai', "Cresselia", "Bidoof", "None"];
const stateFile = './state.json';
let challenges;
let activeChallenge = null;
let challengeTimeoutId = null;
function loadState() {
console.log('inside loadState');
if (fs.existsSync(stateFile)) {
const stateData = fs.readFileSync(stateFile, 'utf8');
challenges = JSON.parse(stateData);
} else {
challenges = initialState;
saveState();
}
console.log('End of loadState')
}
function saveState() {
console.log('inside saveState');
const stateData = JSON.stringify(challenges, null, 2);
fs.writeFileSync(stateFile, stateData, 'utf8');
}
function scheduleChallenge() {
console.log('inside scheduleChallenge');
if (new Date() >= endDate) {
return;
}
if (challengeTimeoutId !== null) {
clearTimeout(challengeTimeoutId);
}
challengeTimeoutId = setTimeout(() => {
const nextChallenge = challenges.find(challenge => challenge.timeSent === null);
if (nextChallenge) {
activeChallenge = nextChallenge;
activeChallenge.timeSent = new Date().toISOString();
saveState();
client.channels.cache.get(announcementChannelId).send(`@here ${activeChallenge.challengeText}`);
}
}, challengeInterval * 60000);
}
client.once('ready', () => {
console.log('Bot is online!');
loadState();
});
client.on('messageCreate', message => {
if (message.content.startsWith('!challenge start')) {
scheduleChallenge();
message.channel.send(`Shiny Wars Challenges have started!`);
}
if (message.content.startsWith('!challenge complete')) {
const teamName = message.content.split(' ')[2];
if (teamNames.includes(teamName) && activeChallenge && activeChallenge.timeCompleted === null) {
activeChallenge.timeCompleted = new Date().toISOString();
activeChallenge.challengeWonBy = teamName;
activeChallenge.timeToComplete = (new Date(activeChallenge.timeCompleted) - new Date(activeChallenge.timeSent)) / 1000;
saveState();
message.channel.send(`Team ${teamName} has completed the challenge!`);
scheduleChallenge();
} else {
message.channel.send(`Invalid team name or no active challenge.`);
}
}
});
client.login(process.env.DISCORD_BOT_TOKEN);