-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
231 lines (200 loc) · 7.24 KB
/
main.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
const Discord = require(`discord.js`);
const client = new Discord.Client({
partials: [`MESSAGE`, `CHANNEL`, `REACTION`],
intents: [
Discord.Intents.FLAGS.GUILDS, // for join and leave events
Discord.Intents.FLAGS.GUILD_MESSAGES, // for receiving commands through messages
Discord.Intents.FLAGS.GUILD_MESSAGE_REACTIONS // for receiving rating reactions
],
retryLimit: 0 // prevent random 500s from failing requests immediately - currently disabled for testing
});
const cron = require(`cron`).CronJob;
require(`dotenv`).config();
const discordAPI = require(`./src/discordAPI`);
const tmAPI = require(`./src/tmApi`);
const redisAPI = require(`./src/redisApi`);
const format = require(`./src/format`);
const commands = require(`./src/commands`);
const utils = require(`./src/utils`);
const constants = require(`./src/constants`);
const discordToken = process.env.DISCORD_TOKEN;
const deployMode = process.env.DEPLOY_MODE;
const adminServerID = process.env.ADMIN_SERVER_ID;
const adminChannelID = process.env.ADMIN_CHANNEL_ID;
const commandIDs = {};
// COTD pings for 7pm (Europe)
new cron(
`00 50 18 * * *`,
async () => {
await discordAPI.sendCOTDPings(client, constants.cupRegions.europe);
},
null,
true,
`Europe/Paris`
);
// COTD pings for 3am (America)
new cron(
`00 50 02 * * *`,
async () => {
await discordAPI.sendCOTDPings(client, constants.cupRegions.america);
},
null,
true,
`Europe/Paris`
);
// COTD pings for 11am (Asia)
new cron(
`00 50 10 * * *`,
async () => {
await discordAPI.sendCOTDPings(client, constants.cupRegions.asia);
},
null,
true,
`Europe/Paris`
);
// display the current totd every day at 19:00:20
new cron(
`20 00 19 * * *`,
async () => {
await discordAPI.distributeTOTDMessages(client);
},
null,
true,
`Europe/Paris`
);
// refresh bingo every week on Monday at 19:00:10 (just after the TOTD because that counts yesterday's bingo votes)
new cron(
`10 00 19 * * 1`,
async () => {
await discordAPI.archiveBingoBoards();
},
null,
true,
`Europe/Paris`
);
const setupRedis = async () => {
const redisClient = await redisAPI.login();
const bingoBoards = await redisAPI.getAllBingoBoards(redisClient);
if (!bingoBoards) {
await redisAPI.resetBingoBoards(redisClient);
}
redisAPI.logout(redisClient);
}
client.on(`ready`, async () => {
console.log(`Ready as ${client.user.tag}!`);
await tmAPI.login();
await tmAPI.loginOAuth();
// in production, refresh TOTD to make sure there is a thumbnail in the images for cached messages
if (deployMode === `prod`) {
await discordAPI.getTOTDMessage(true);
}
await setupRedis();
// register all the commands (this might take a minute due to Discord rate limits)
const globalCommandConfigs = commands.globalCommands.map((commandConfig) => commandConfig.slashCommand);
const adminCommandConfigs = commands.adminCommands.map((commandConfig) => commandConfig.slashCommand);
const adminGuild = await client.guilds.fetch(adminServerID);
let globalCommandManager;
if (deployMode !== `prod`) {
// use admin guild for global commands in dev mode
globalCommandManager = adminGuild.commands;
} else {
globalCommandManager = client.application.commands;
}
const adminCommandManager = adminGuild.commands;
for (const commandConfig of globalCommandConfigs) {
if (commandConfig) {
const commandRes = await globalCommandManager.create(commandConfig);
commandIDs[commandConfig.name] = commandRes.id;
console.log(`Registered global command: ${commandConfig.name}`);
}
}
for (const commandConfig of adminCommandConfigs) {
if (commandConfig) {
await adminCommandManager.create(commandConfig);
console.log(`Registered admin command: ${commandConfig.name}`);
}
}
const existingCommands = await globalCommandManager.fetch();
const joinedCommands = commands.globalCommands.concat(commands.adminCommands);
existingCommands.forEach((command) => {
const foundCommand = joinedCommands.find((c) => c.slashCommand.name === command.name);
if (!foundCommand) {
console.log(`Removing command ${command.name}`);
globalCommandManager.delete(command.id);
}
});
});
client.on(`interactionCreate`, async (interaction) => {
if (interaction.isCommand()) {
if (!interaction.guildId) {
// bot doesn't support DMs for now, reply with an explanation
return await interaction.reply(`Sorry, I don't support DMs. Please use my commands in a server that we share.`);
}
console.log(`Received command: ${interaction.commandName} (#${interaction.channel.name} in ${interaction.guild?.name})`);
const joinedCommands = commands.globalCommands.concat(commands.adminCommands);
const matchedCommand = joinedCommands.find((commandConfig) => commandConfig?.slashCommand?.name === interaction.commandName);
if (matchedCommand) {
try {
await matchedCommand.action(interaction, client, commandIDs);
} catch (e) {
console.error(e);
}
} else {
console.error(`No matching command found`);
}
} else if (interaction.isAutocomplete()) {
console.log(`Received autocomplete for ${interaction.commandName} (#${interaction.channel.name} in ${interaction.guild.name} - @${interaction.user.username})): ${interaction.options.getFocused()}`);
const joinedCommands = commands.globalCommands.concat(commands.adminCommands);
const matchedCommand = joinedCommands.find((commandConfig) => commandConfig.slashCommand.name === interaction.commandName);
if (matchedCommand) {
try {
await matchedCommand.action(interaction, client);
} catch (e) {
console.log(e);
}
} else {
console.log(`No matching command found`);
}
}
});
client.on(`messageCreate`, async (msg) => {
if (msg.mentions.has(client.user.id, {ignoreEveryone: true})) {
console.log(`Proxying mention to admin server...`);
const adminChannel = await client.channels.fetch(adminChannelID);
const proxyMessage = format.formatProxyMessage(msg);
utils.sendMessage(adminChannel, proxyMessage);
}
});
const handleReaction = async (reaction, user, add) => {
if (reaction.partial) {
// if reaction is partial (not cached), try to fetch it fully
try {
await reaction.fetch();
} catch (error) {
console.error(`Something went wrong when fetching the full reaction: `, error);
return;
}
}
if (
reaction.message.author && (reaction.message.author.id === client.user.id) // check the message was sent by the bot
&& user.id !== client.user.id // check the reaction was not sent by the bot
) {
discordAPI.updateTOTDReactionCount(reaction, add, user);
}
};
client.on(`messageReactionAdd`, (reaction, user) => {
handleReaction(reaction, user, true);
});
client.on(`messageReactionRemove`, async (reaction, user) => {
handleReaction(reaction, user, false);
});
client.on(`guildCreate`, (guild) => {
console.log(`Joined new server: ${guild.name} (${guild.id})`);
});
client.on(`guildDelete`, (guild) => {
console.log(`Left server: ${guild.name} (${guild.id})`);
});
/* client.on(`rateLimit`, (rateLimit) => {
console.warn(`Rate limit reached: ${rateLimit.limit} on ${rateLimit.method} ${rateLimit.path} (global: ${rateLimit.global}) - wait for ${rateLimit.timeout}`);
}); */
client.login(discordToken);