This repository has been archived by the owner on Sep 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
commands.js
166 lines (140 loc) · 6.38 KB
/
commands.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
/**
* @typedef {import("discord.js").TextChannel} DiscordJs.TextChannel
* @typedef {import("discord.js").User} DiscordJs.User
*/
const Azure = require("./azure"),
Exception = require("./exception"),
Servers = require("./servers"),
settings = require("./settings"),
Warning = require("./warning");
/**
* @type {typeof import("./discord")}
*/
let Discord;
setTimeout(() => {
Discord = require("./discord");
}, 0);
// ### #
// # # #
// # ### ## # ## # ### # ## ## # ###
// # # # # # # # # # # ## # # ## #
// # # # # # # # # # #### # # # # ###
// # # # # # # # # # # # # # # # ## #
// ### ### # # # # #### # # ## # ####
/**
* A class that handles commands given by chat.
*/
class Commands {
// ### ## ### # # ## ### ###
// ## # ## # # # # # ## # # ##
// ## ## # # # ## # ##
// ### ## # # ## # ###
/**
* Gets the current list of servers and their status.
* @param {DiscordJs.User} user The user initiating the command.
* @param {DiscordJs.TextChannel} channel The channel the message was sent over.
* @param {string} message The text of the command.
* @returns {Promise<boolean>} A promise that resolves with whether the command completed successfully.
*/
async servers(user, channel, message) {
if (message) {
return false;
}
const msg = Discord.embedBuilder({
title: "Overload Azure Server Status",
fields: []
});
const offline = [],
online = [];
Object.keys(settings.servers).sort().forEach((region) => {
const server = settings.servers[region];
if (server.started) {
online.push(region);
} else {
offline.push(region);
}
});
if (offline.length > 0) {
msg.addFields({
name: "Offline Servers - Use `!start <region>` to start a server.",
value: offline.map((r) => `${r} - ${settings.servers[r].location}`).join("\n")
});
}
if (online.length > 0) {
msg.addFields({
name: "Online Servers",
value: online.map((r) => `${r} - ${settings.servers[r].ipAddress} - ${settings.servers[r].host} - ${settings.servers[r].location}`).join("\n")
});
}
await Discord.richQueue(msg, channel);
return true;
}
// # #
// # #
// ### ### ### ### ###
// ## # # # # # #
// ## # # ## # #
// ### ## # # # ##
/**
* Starts the server in the requested region.
* @param {DiscordJs.User} user The user initiating the command.
* @param {DiscordJs.TextChannel} channel The channel the message was sent over.
* @param {string} message The text of the command.
* @returns {Promise<boolean>} A promise that resolves with whether the command completed successfully.
*/
async start(user, channel, message) {
if (!message) {
return false;
}
message = message.toLowerCase();
if (!settings.servers[message]) {
await Discord.queue(`Sorry, ${user}, but this is not a valid server. Use the \`!servers\` command to see the list of servers.`, channel);
throw new Warning("Server does not exist.");
}
if (settings.servers[message].started) {
await Discord.queue(`Sorry, ${user}, but this server is already running. Did you mean to \`!extend ${message}\` instead?`, channel);
throw new Warning("Server already started.");
}
await Discord.queue("Please standby...", channel);
try {
await Azure.start(settings.servers[message]);
} catch (err) {
await Discord.queue(`Sorry, ${user}, but there was an error starting the server. An admin has been notified.`, channel);
throw new Exception("Error starting server.", err);
}
Servers.setup(settings.servers[message], message, channel);
await Discord.queue(`${user}, the ${message} server has been started at **${settings.servers[message].ipAddress}** (${settings.servers[message].host}) and should be available in a couple of minutes. The server will automatically shut down when idle for 15 minutes. Use the \`!extend ${message}\` command to reset the idle shutdown timer and move notifications about this server to a new channel.`, channel);
return true;
}
// # #
// # #
// ## # # ### ## ### ###
// # ## ## # # ## # # # #
// ## ## # ## # # # #
// ## # # ## ## # # ###
/**
* Extends a server in the requested region.
* @param {DiscordJs.User} user The user initiating the command.
* @param {DiscordJs.TextChannel} channel The channel the message was sent over.
* @param {string} message The text of the command.
* @returns {Promise<boolean>} A promise that resolves with whether the command completed successfully.
*/
async extend(user, channel, message) {
if (!message) {
return false;
}
message = message.toLowerCase();
if (!settings.servers[message]) {
await Discord.queue(`Sorry, ${user}, but this is not a valid server. Use the \`!servers\` command to see the list of servers.`, channel);
throw new Warning("Server does not exist.");
}
if (!settings.servers[message].started) {
await Discord.queue(`Sorry, ${user}, but this server is not running. Did you mean to \`!start ${message}\` instead?`, channel);
throw new Warning("Server not started.");
}
Servers.setup(settings.servers[message], message, channel);
await Discord.queue(`${user}, the ${message} server has been extended at **${settings.servers[message].ipAddress}** (${settings.servers[message].host}). The server will automatically shut down in one hour unless you issue the \`!extend ${message}\` command.`, channel);
return true;
}
}
module.exports = Commands;