-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.ts
289 lines (226 loc) · 9.76 KB
/
bot.ts
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
// Node Modules
import * as cron from "cron";
import * as dotenv from "dotenv";
import { Client, Intents, Guild, GuildBasedChannel } from "discord.js";
// Custom Scripts
import { connectDB } from "./src/databaseUtils";
import * as aws from "./src/awsUtils";
/* ============================================ */
/* SETUP */
/* ============================================ */
// Load the .env environment variables
dotenv.config({path: `.env`});
// Connect to the database
connectDB();
// Discord Client
const client = new Client({ intents: [
Intents.FLAGS.GUILDS, // Access servers
Intents.FLAGS.GUILD_MESSAGES, // Send message to servers
Intents.FLAGS.GUILD_MESSAGE_TYPING,
Intents.FLAGS.DIRECT_MESSAGES // Direct messages to users
]});
// =================================================================
// SERVER SELECTORS
// =================================================================
let devServer: Guild
let devServer_general
let nubuSquad: Guild
let nubuSquad_nubs
// =================================================================
// IDs
// =================================================================
// Server IDs
const devServerID = "931035832909963304";
// User IDs
const ricardite = "367146292075560960";
const almendro = "369643860986560512";
const eddysanoli = "467506601956343811";
const felipe = "729166267801534504";
const bot = "930983546816974899";
const whitelist = [ricardite, almendro, eddysanoli, felipe];
const blacklist = [bot];
// =================================================================
// REGEX
// =================================================================
// IPV4 Regex
const ipv4_regex = /((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.|$)){4}$/gm;
// IPV6 Regex
const ipv6_regex = /(?:(?:[0-9A-Fa-f]{1,4}))*(?::(?:(?:[0-9A-Fa-f]{1,4}))*)+:(?:(?:[0-9A-Fa-f]{1,4}))$/gm;
// Command regex (!command param)
let cmd_regex = /!.+ (.+)$/gm;
// =================================================================
// EVENTS
// NOTE: Discord.js event based. Events are accessed using "client.on()"
// =================================================================
// Event: Logs when bot has successfully logged in
client.on("ready", function () {
// SERVER SELECTORS =======================
// Dev Server
// - #general
devServer = client.guilds.cache.get(devServerID);
devServer_general = devServer.channels.cache.get("931035832909963307");
// Nubu Squad
// - #nubs
nubuSquad = client.guilds.cache.get("369642385866752000");
nubuSquad_nubs = nubuSquad.channels.cache.get("369642385866752003");
// ========================================
// Welcome message
devServer_general.send("Hey, I'm back baby!");
});
// Event: Scheduled events
client.once("ready", () => {
// Cron guide:
// ┌────────────── second (optional)
// │ ┌──────────── minute
// │ │ ┌────────── hour
// │ │ │ ┌──────── day of month
// │ │ │ │ ┌────── month
// │ │ │ │ │ ┌──── day of week
// │ │ │ │ │ │
// │ │ │ │ │ │
// * * * * * *
// Scheduled function: Minecraft Wednesday (Tuesday - 5 PM Guatemala / 11 PM US)
let minecraftWednesday = new cron.CronJob('00 00 17 * * 2', () => {
nubuSquad_nubs.send("I'm proud to announce that today is Minecraft Wednesday my dudes! The server will be started shortly.");
aws.startServer(nubuSquad_nubs);
});
// Scheduled function: Feliz Jueves (Thursday - 12 PM Guatemala / 6 PM US)
let felizJueves = new cron.CronJob('00 00 12 * * 4', () => {
nubuSquad_nubs.send("Feliz jueves!");
});
// Enable scheduled events
minecraftWednesday.start();
felizJueves.start();
});
// Event: Message
client.on("messageCreate", async (msg) => {
// Extract metadata from message
let msgServerID = msg.guildId;
let msgChannelID = msg.channelId;
let msgUserID = msg.author.id;
let msgUsername = msg.author.username;
// Select server and channel of current message
let msgServer = client.guilds.cache.get(msgServerID);
let msgChannel = msgServer.channels.cache.get(msgChannelID);
// Command (!ec2-status):
// Check AWS instance status if server is "Dev Server"
if (msg.content === "!ec2-status") {
// Retrieve instance statuses
let instancesInfo = await aws.getEC2Info();
console.log("Instance Info: ", instancesInfo);
// Base instance info text
let statusText = "Instance Info: \n";
// Add a new line of text for each instance
instancesInfo.forEach(instance => {
statusText += `${instance.name} (${instance.id}): ${instance.status}\n`;
});
// Reply to the user
msg.reply(statusText);
}
// Command (!start-server):
// Start the gaming server
if (whitelist.includes(msgUserID) && msg.content === "!start-server") {
aws.startServer(msgChannel);
}
// Command (!stop-server):
// Stop the gaming server
if (whitelist.includes(msgUserID) && msg.content === "!stop-server") {
aws.stopServer(msg);
}
// Command (!stop-instance):
// Stop the EC2 instance
if (whitelist.includes(msgUserID) && msg.content === "!stop-instance") {
aws.stopEC2Instance(msg);
}
// Command (!isitWednesday):
// Is it wednesday my dudes?
if (msg.content === "!isitWednesday") {
// Get current day of the week
let now = new Date();
let day = now.getDay();
// Print the corresponding response
if (day === 3) { msg.reply("It is wednesday my dudes!"); }
else if (day === 2) { msg.reply("It is minecraft wednesday my dudes!"); }
else { msg.reply("It is not wednesday my dudes :("); }
}
// Command (!add-ipv6):
// Add IPV6 address to security group
if (whitelist.includes(msgUserID) && !blacklist.includes(msgUserID) && msg.content.includes("!add-ipv6")) {
// Add an IPV6 address only if one is detected in the command
if (ipv6_regex.test(msg.content)){
let newIPV6 = msg.content.match(ipv6_regex)[0];
aws.addIPV6(msg, newIPV6, "Automated Addition");
}
else {
msg.reply("Unable to add IPV6 address to security group.");
}
}
// Command (!add-ipv4):
// Add IPV4 address to security group
if (whitelist.includes(msgUserID) && !blacklist.includes(msgUserID) && msg.content.includes("!add-ipv4")) {
// Add an IPV4 only if one is detected in the command
if (ipv4_regex.test(msg.content)){
let newIPV4 = msg.content.match(ipv4_regex)[0];
aws.addIPV4(msg, newIPV4, "Automated Addition");
}
else {
msg.reply("Unable to add IPV4 address to security group.");
}
}
// Command (!change-level):
// Change the level used by the minecraft server
if (whitelist.includes(msgUserID) && !blacklist.includes(msgUserID) && msg.content.includes("!change-level")) {
// Check if the command is correctly structured
if (cmd_regex.test(msg.content)){
// Create an executable regex and evaluate it
let regex = new RegExp(cmd_regex);
let levelName = regex.exec(msg.content)[1];
// Send the "sed" commands to change the level name
aws.changeLevelName(msg, levelName);
}
else {
msg.reply("Unable to change the current minecraft level.");
}
}
// Command (!server-status):
// Check the status of the minecraft server
if (msg.content === "!server-status") {
const minecraftServerUp = await aws.getServerStatus();
console.log(minecraftServerUp);
// Reply with the status based on the value of "minecraftServerUp"
if (minecraftServerUp){
msg.reply(`Minecraft Server is Up 😄`);
}
else {
msg.reply("Minecraft Server is Down 😞");
}
console.log("Retrieve Server Status: Success");
}
// Command (!test):
// For testing new functionality
if (whitelist.includes(msgUserID) && !blacklist.includes(msgUserID) && msg.content === "!test") {
msg.reply(`Fuck you ${msgUsername}!`);
console.log("Test: Success");
}
// Command (!help):
// Get all the commands available
if (!blacklist.includes(msgUserID) && msg.content === "!help") {
msg.reply(`Commands available:
- !start-server: Start the EC2 instance and the minecraft server
- !stop-server: Stop the minecraft server
- !stop-instance: Turn off the EC2 instance
- !ec2-status: See status of each EC2 instance on AWS
- !isitWednesday: Is it wednesday my dudes?
- !add-ipv4 IP: Add access to specific IPV4 address
- !add-ipv6 IP: Add access to specific IPV6 address
- !test: Test new functionality or be cursed at
- !change-level: Change current level being loaded by the minecraft server
`);
}
});
// =================================================================
// DISCORD LOGIN
// =================================================================
// Login to Discord with bot's token
// (Bot Link: https://discord.com/api/oauth2/authorize?client_id=930983546816974899&permissions=534727097920&scope=bot)
client.login(process.env.TOKEN);