forked from Australis-0/TriumphAndTragedy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
305 lines (256 loc) · 9.49 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
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
console.log(`Bot started.`);
//Node.js imports
console.time(`Loading "underscore" ..`);
global._ = require("underscore");
console.timeEnd(`Loading "underscore" ..`);
console.time(`Loading "ngraph.graph" ..`);
global.bn_graph = require("ngraph.graph");
console.timeEnd(`Loading "ngraph.graph" ..`);
console.time(`Loading "ngraph.path" ..`);
global.bn_path = require("ngraph.path");
console.timeEnd(`Loading "ngraph.path" ..`);
console.time(`Loading "canvas" ..`);
global.Canvas = require("canvas");
console.timeEnd(`Loading "canvas" ..`);
console.time(`Loading "diacriticless" ..`);
global.diacriticless = require("diacriticless");
console.timeEnd(`Loading "diacriticless" ..`);
console.time(`Loading "discord.js" ..`);
global.Discord = require("discord.js");
console.timeEnd(`Loading "discord.js" ..`);
console.time(`Loading "fs" ..`);
global.fs = require("fs");
console.timeEnd(`Loading "fs" ..`);
console.time(`Loading "node-html-parser" ..`);
global.HTML = require("node-html-parser");
console.timeEnd(`Loading "node-html-parser" ..`);
console.time(`Loading "opusscript" ..`);
global.opus = require("opusscript");
console.timeEnd(`Loading "opusscript" ..`);
console.time(`Loading "path" ..`);
global.path = require("path");
console.timeEnd(`Loading "path" ..`);
console.time(`Loading "convert-svg-to-png" ..`);
global.SVG = require("convert-svg-to-png");
console.timeEnd(`Loading "convert-svg-to-png" ..`);
console.time(`Loading "@discordjs/voice" ..`);
global.voice = require("@discordjs/voice");
console.timeEnd(`Loading "@discordjs/voice" ..`);
console.log(`Node.js libraries imported.`);
//Import Core Framework
global.FileManager = require("./core/file_manager");
//Import Core Functions - See startup.js for full directory
FileManager.import("./startup");
loadBotFiles();
//Start bot
startBot();
//Global error handling
process.on("unhandledRejection", (error) => {
log.error(`Unhandled promise rejection. ${error.toString()}`);
console.log(error);
});
//Reaction/interaction framework
client.on("interactionCreate", (interaction) => {
buttonHandler(interaction);
selectHandler(interaction);
});
client.on("messageReactionAdd", async (reaction, user) => {
reactionHandler(reaction, user);
});
//Command handling
client.on("messageCreate", async (message) => {
//Fetch local parameters
username = message.author.username;
user_id = message.author.id;
input = message.content;
//Parse arguments
if (settings.no_space) input = input.replace(settings.prefix, `${settings.prefix} `);
var arg = splitCommandLine(input);
//Check output
log.info(`
Author: ${username}
Arguments: ${arg.join(", ")}
Original Content: ${input}
`);
if (!message.author.bot && !ignore_channels.includes(message.channel.id)) {
//Debug commands (these ones have a prefix)
{
if (equalsIgnoreCase(arg[0], settings.prefix)) {
//Used to eval
if (equalsIgnoreCase(arg[1], "console")) {
if (message.member.roles.cache.find(role => settings.administrator_roles.includes(role.id))) {
var full_code = [];
for (var i = 2; i < arg.length; i++) full_code.push(arg[i]);
eval(full_code.join(" "));
//Send back prompt
message.channel.send("Console command executed. Warning! This command can be highly unstable if not used correctly.").then((msg) => {
//Delete console command output after 10 seconds
setTimeout(function() { msg.delete(); }, 10000);
});
}
}
//console.log
if (equalsIgnoreCase(arg[1], "console.log") || equalsIgnoreCase(arg[1], "log")) {
if (message.member.roles.cache.find(role => settings.administrator_roles.includes(role.id))) {
var full_code = [];
for (var i = 2; i < arg.length; i++) full_code.push(arg[i]);
var actual_code = eval(full_code.join(" "));
var actual_string = actual_code.toString();
if (typeof actual_code == "object")
actual_string = JSON.stringify(actual_code);
var log_array = splitString(actual_string, 200);
var log_embed_array = splitEmbed(log_array, {
title: truncateString(full_code.join(" "), 60),
title_pages: true,
fixed_width: true
});
//Remove functionality for previous debug logs
var all_interfaces = Object.keys(interfaces);
for (var i = 0; i < all_interfaces.length; i++) {
var local_ui = interfaces[all_interfaces[i]];
if (local_ui.debug) {
delete main.interfaces[all_interfaces[i]];
delete interfaces[all_interfaces[i]];
}
}
message.channel.send(config.localisation.blank).then((msg) => {
createPageMenu(msg, {
embed_pages: log_embed_array,
debug: true,
user: message.author.id
});
});
}
}
}
}
//Lobby commands (these also have a prefix, with the exception of visual prompts)
{
if (equalsIgnoreCase(arg[0], settings.prefix)) {
if (equalsIgnoreCase(arg[1], "play")) {
createNewGame(user_id, message);
}
}
}
//Game input commands
{
if (getGame(user_id)) {
var game_obj = interfaces[getGame(user_id)];
if (game_obj.channel == message.channel.id) {
setTimeout(function(){
message.delete();
}, 1000);
commandHandler(getGame(user_id), arg.join(" "));
//Cache usernames/nicknames
try {
getUsernames(user_id);
} catch {}
//Check if game is still active
if (returnGameFromChannelID(message.channel.id))
interfaces[returnGameFromChannelID(message.channel.id)].last_active = new Date().getTime();
}
}
}
}
});
//Initialise variables before anything else!
initGlobalLoop();
//Logic loops, 1-second logic loop
setInterval(function(){
//Bot clock
bot_clock++;
//Cache interfaces
main.interfaces = interfaces;
//Delete inactive channels
clearInactiveGames();
//ABRS - Save backups!
var current_date = new Date().getTime();
var current_turn_time = 0;
var battle_difference = current_date - returnSafeNumber(main.global.battle_tick);
var queue_time_difference = current_date - main.last_queue_check;
var time_difference = current_date - main.last_backup;
var turn_time_difference = current_date - main.last_turn;
//Backup processing
if (time_difference > settings.backup_timer*1000) {
main.last_backup = current_date;
log.info(`Saving automatic backup.`);
writeSave({ file_limit: settings.backup_limit });
}
//Combat processing
if (battle_difference > (settings.turn_timer*1000)/10)
nextBattleTick();
//Date processing
if (main.season_started) {
current_turn_time = getTimeModifier();
//This is how much time has elapsed in years. The numerator 1000 represents milliseconds
var time_elapsed = current_turn_time*(1000/(settings.turn_timer*1000));
var processed_time = parseYears(time_elapsed);
//Add to date
main.date.year += processed_time.year;
main.date.month += processed_time.month;
main.date.day += processed_time.day;
main.date.hour += processed_time.hour;
if (main.date.month > 12) {
main.date.month = 1;
main.date.year++;
}
if (main.date.day > processed_time.days_in_months[main.date.month - 1]) {
main.date.day = 1;
main.date.month++;
}
if (main.date.hour > 23) {
main.date.hour = 0;
main.date.day++;
}
}
//Queue processing
if (!main.season_started) {
//Check if enough players have joined for the season to start
if (Object.keys(main.users).length >= config.defines.common.starting_players) {
main.season_started = true;
reinitialiseGameEmbeds();
}
//Otherwise, go on with the activity checks ..
if (queue_time_difference > returnSafeNumber(config.defines.common.activity_check)*1000*60*60*24) {
main.last_queue_check = current_date;
//Clear inactive users
var all_current_users = Object.keys(main.users);
for (var i = 0; i < all_current_users.length; i++) {
var local_user = main.users[all_current_users[i]];
if (current_date - local_user.last_queue_check >= returnSafeNumber(config.defines.common.activity_check)*1000*60*60*24) {
try {
returnChannel(settings.alert_channel).send(`<@${main.global.user_map[all_current_users[i]]}> was dropped from the queue due to inactivity.`);
deleteCountry(all_current_users[i]);
reinitialiseGameEmbeds();
} catch {}
}
}
//Send check to all remaining users
var all_users = Object.keys(main.users);
for (var i = 0; i < all_users.length; i++)
checkActivityInQueue(all_users[i]);
}
}
//Turn processing for all users
if (turn_time_difference > settings.turn_timer*1000) {
main.last_backup = current_date;
main.last_turn = current_date;
//Process nextGlobalTurn() for global processes and calculations
if (main.season_started) {
clearBadInterfaces();
nextBattleTick(true);
nextGlobalTurn();
}
}
}, 1000);
//Logic loops, 30-second logic loop
setInterval(function(){
//Write to database.js
try {
fs.writeFile('database.js', JSON.stringify(main), function (err, data) {
if (err) return log.info(err);
});
} catch (e) {
log.error(`Ran into an error whilst attempting to save to database.js! ${e}.`);
}
}, 30000);