This repository was archived by the owner on Mar 7, 2021. It is now read-only.
forked from rmgirardin/mouse-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
181 lines (156 loc) · 7.27 KB
/
Copy pathindex.js
File metadata and controls
181 lines (156 loc) · 7.27 KB
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
if (process.version.slice(1).split(".")[0] < 8) throw new Error("Node 8.0.0 or higher is required. Update Node on your system.");
function getObjects(obj, key, val) {
var objects = [];
for (var i in obj) {
if (!obj.hasOwnProperty(i)) continue;
if (typeof obj[i] == "object") {
objects = objects.concat(getObjects(obj[i], key, val));
} else if (i == key && obj[key] == val) {
objects.push(obj);
}
}
return objects;
}
const Discord = require("discord.js");
const {promisify} = require("util");
const readdir = promisify(require("fs").readdir);
const Enmap = require("enmap");
const EnmapLevel = require("enmap-level");
const request = require("request-promise-native");
const charactersJS = require("./modules/characters.js");
const shipsJS = require("./modules/ships.js");
const client = new Discord.Client();
// Here we load the config and functions files
client.config = require("./config.js");
require("./modules/functions.js")(client);
require("./modules/db-handler.js")(client);
// Also the Logger
client.logger = require("./modules/logger");
// Aliases and commands are put in collections where they can be read from,
// catalogued, listed, etc.
client.commands = new Enmap();
client.aliases = new Enmap();
client.swgohData = new Enmap();
// Setting up the databases
client.pointsTable = new Enmap({provider: new EnmapLevel({ name: "points" })});
client.cache = new Enmap({provider: new EnmapLevel({ name: "cache" })});
const init = async () => {
// Now we load **commands** into memory, as a collection
const cmdFiles = await readdir("./commands/");
client.logger.log(client, `Loading ${cmdFiles.length} commands:`,);
cmdFiles.forEach(cmdFile => {
if (!cmdFile.endsWith(".js")) return;
client.loadCommand(cmdFile);
});
// Then we load events, which will include our message and ready event.
const evtFiles = await readdir("./events/");
client.logger.log(client, `Loading ${evtFiles.length} events:`);
evtFiles.forEach(file => {
try {
const eventName = file.split(".")[0];
const event = require(`./events/${file}`);
client.logger.log(client, `Load Event: ${eventName}...`);
client.on(eventName, event.bind(null, client));
delete require.cache[require.resolve(`./events/${file}`)];
} catch (error) {
client.logger.error(client, `Unable to load command ${file}:\n${error.stack}`);
}
});
// Generate a cache of client permissions
client.levelCache = {};
for (let i = 0; i < client.config.permLevels.length; i++) {
const thisLevel = client.config.permLevels[i];
client.levelCache[thisLevel.name] = thisLevel.level;
}
// GET and merge character databases
const charactersURL = "https://swgoh.gg/api/characters/?format=json";
const charactersOptions = { uri: charactersURL, json: true };
let characters;
const maxRetries = 3;
let retries = 0;
while (!characters && (retries < maxRetries)) {
try {
characters = await request(charactersOptions);
// Loop through our character array, find the matching characters in the swgoh.gg api
// and merge its data into ours.
if (characters) {
for (var i = 0; i < characters.length; i++) {
const character = characters[i];
if (character) {
const chData = getObjects(charactersJS, "name", character.name);
if (chData.length == 1) Object.assign(character, chData[0]);
else if (chData.length == 0) {
const newCharacter = {
"name": character.name,
"faction": [character.alignment].concat(character.role).concat(character.categories),
"chImage": `https:${character.image}`
};
client.logger.warn(client, `No JS results found for character: ${character.name}. ${JSON.stringify(newCharacter)}`);
}
else client.logger.warn(client, `Multiple results found for character: ${character.name}`);
} else {
client.logger.warn(client, `Found an empty character in the swgoh.gg characters API at index: ${i}`);
}
}
} else {
throw new Error("I wasn't able to get the list of characters from swgoh.gg's API!");
}
} catch (error) {
if (retries < maxRetries) {
retries++;
client.logger.warn(client, `Character Request Failure: try ${retries} of ${maxRetries}\n${error.stack}`);
continue;
} else {
client.logger.error(client, `Character Request Failure\n${error.stack}`);
throw new Error("I wasn't able to get the list of characters from swgoh.gg's API!");
}
}
}
// GET and merge ship databases
const shipsURL = "https://swgoh.gg/api/ships/?format=json";
const shipsOptions = { uri: shipsURL, json: true };
let ships;
retries = 0;
while (!ships && (retries < maxRetries)) {
try {
ships = await request(shipsOptions);
// Loop through our ships array, find the matching ships in the swgoh.gg api
// and merge its data into ours.
if (ships) {
for (var j = 0; j < ships.length; j++) {
const ship = ships[j];
if (ship) {
const sData = getObjects(shipsJS, "name", ship.name);
if (sData.length == 1) Object.assign(ship, sData[0]);
else if (sData.length == 0) {
const newShip = {
"name": ship.name,
"faction": [ship.alignment].concat(ship.role).concat(ship.categories),
"sImage": `https:${ship.image}`
};
client.logger.warn(client, `No JS results found for ship: ${ship.name}. ${JSON.stringify(newShip)}`);
}
else client.logger.warn(client, `Multiple results found for ship: ${ship.name}`);
} else {
client.logger.warn(client, `Found an empty ship in the swgoh.gg ships API at index: ${j}`);
}
}
} else {
throw new Error("I wasn't able to get the list of ships from swgoh.gg's API!");
}
} catch (error) {
if (retries < maxRetries) {
retries++;
client.logger.warn(client, `Ships Request Failure: try ${retries} of ${maxRetries}\n${error.stack}`);
continue;
} else {
client.logger.error(client, `Ships Request Failure\n${error}`);
throw new Error("I wasn't able to get the list of ships from swgoh.gg's API!");
}
}
}
client.swgohData.set("charactersData", characters);
client.swgohData.set("shipsData", ships);
client.login(client.config.token);
};
init();