forked from FightRPG-DiscordBotRPG/DiscordBotRPG-Discord
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.js
More file actions
196 lines (163 loc) · 6.43 KB
/
Copy pathbot.js
File metadata and controls
196 lines (163 loc) · 6.43 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
const Discord = require("discord.js");
const conf = require("./conf/conf");
const ModuleHandler = require("./src/Modules/ModuleHandler");
const DBL = require("dblapi.js");
const DiscordServers = require("./src/Database/DiscordServers");
const Globals = require("./src/Globals");
const conn = require("./conf/mysql");
const Axios = require("axios").default;
const Utils = require("./src/Utils");
var bot = new Discord.Client();
process.on('unhandledRejection', err => {
let errorDate = new Date();
console.log(errorDate.toUTCString());
console.log(err);
});
console.log("Shard Starting ...");
let timeStart = Date.now();
async function startBot() {
try {
await bot.login(conf.discordbotkey);
removedInactiveUsers();
} catch (error) {
let errorDate = new Date();
console.log("Error when connecting Shard. Restarting shard in 30 seconds...");
console.log(errorDate.toUTCString());
console.log(error);
setTimeout(startBot, 30000);
}
}
async function removedInactiveUsers() {
let now = Date.now();
let inactiveUsers = 0;
for (let idUser in Globals.connectedUsers) {
let user = Globals.connectedUsers[idUser];
let diff = now - user.lastCommandUsed;
// 30 minutes inactive before removing some data from globals
if (diff / 60000 > Globals.inactiveTimeBeforeDisconnect) {
delete Globals.connectedUsers[user.id];
inactiveUsers++;
}
}
console.log(`Removed: ${inactiveUsers} inactive users. Memory consumption: ${await getMemory()} MB`);
//createDummyUsers();
setTimeout(removedInactiveUsers, Globals.inactiveTimeBeforeDisconnect * 60000);
}
async function createDummyUsers() {
let nbUsersToCreate = Math.round(Math.random() * 10000);
console.log(`Creating ${nbUsersToCreate} fake users...\nMemory before: ${await getMemory()} MB`);
let totalRequests = [];
const User = require("./src/Users/User");
const t = Date.now();
for (let i = 0; i < nbUsersToCreate; i++) {
let zerofilled = ('0000000' + Math.floor(Math.random() * 10000000)).slice(-7);
let u = new User(zerofilled, "Test User#1111", "");
u.token = ""; //When testing use your token don't forget to remove it
u.initAxios();
totalRequests.push(u.getAxios().get("/game/character/info"));
Globals.connectedUsers[zerofilled] = u;
}
console.log(`Memory after creation: ${await getMemory()} MB`);
console.time("Requesting");
await Promise.all(totalRequests);
console.log(`Avg time per requests: ${Math.round((Date.now() - t) / totalRequests.length)}ms; Number of requests per minute: ${1000/Math.round((Date.now() - t) / totalRequests.length)}`);
console.log(`Memory after requests: ${await getMemory()} MB`);
console.timeEnd("Requesting");
}
async function getMemory() {
let totalMemory = await bot.shard.broadcastEval("process.memoryUsage().heapUsed");
let totalMemoryMB = 0;
for (let c of totalMemory) {
totalMemoryMB += Math.round(c / 1048576);
}
return totalMemoryMB;
}
bot.on("ready", async () => {
console.log("Shard Connected");
bot.user.setPresence({
activity: {
name: "On " + await Utils.getTotalNumberOfGuilds(bot.shard) + " servers!"
}
});
//console.log(`${bot.guilds.cache.size}\n${bot.shard.ids}\n${bot.shard.count}`);
if (conf.env === "prod") {
const dbl = new DBL(conf.topggkey, bot);
setInterval(async () => {
console.log("Shards: " + bot.shard.ids);
console.log("Shard: " + bot.shard.ids[0] + " => Sending stats to https://discordbots.org/ ...");
await dbl.postStats(bot.guilds.cache.size, bot.shard.ids[0], bot.shard.count);
console.log("Data sent");
}, 1800000);
}
DiscordServers.serversStats(bot.guilds);
console.log("Shard Loaded");
});
let moduleHandler = new ModuleHandler();
// Key Don't open
startBot();
bot.on('message', async (message) => {
try {
await moduleHandler.run(message);
} catch (err) {
let msgError = "";
if (err.constructor == Discord.DiscordAPIError) {
msgError = "It seems to have an api error, you should check if the bot have all permissions it needs\n";
} else {
msgError = "Oops something went wrong, report the issue here (https://github.com/FightRPG-DiscordBotRPG/FightRPG-Discord-BugTracker/issues)\n";
}
let errorsLines = err.stack.split("\n");
let nameAndLine = errorsLines[1].split(" ");
nameAndLine = nameAndLine[nameAndLine.length - 1].split("\\");
nameAndLine = nameAndLine[nameAndLine.length - 1].split(")")[0];
msgError += "```js\n" + errorsLines[0] + "\nat " + nameAndLine + "\n```";
let errorDate = new Date();
console.log(errorDate.toUTCString());
console.log(err);
message.channel.send(msgError).catch((e) => message.author.send(msgError).catch((e) => null));
}
});
bot.on('guildCreate', async (guild) => {
bot.user.setPresence({
activity: {
name: "On " + await Utils.getTotalNumberOfGuilds(bot.shard) + " servers!",
},
});
DiscordServers.newGuild(guild);
});
bot.on('guildDelete', async (guild) => {
bot.user.setPresence({
activity: {
name: "On " + await Utils.getTotalNumberOfGuilds(bot.shard) + " servers!",
},
});
});
bot.on("userUpdate", async (oldUser, newUser) => {
if (oldUser.tag != newUser.tag) {
let axios;
if (Globals.connectedUsers[newUser.id]) {
axios = Globals.connectedUsers[newUser.id].getAxios();
} else {
let res = await conn.query("SELECT token FROM users WHERE idUser = ?;", [newUser.id]);
if (res[0]) {
axios = Axios.create({
headers: {
'Authorization': "Bearer " + res[0].token
}
})
}
}
if (axios != null) {
let data = await axios.post("/game/character/update", {
username: newUser.tag
});
if (data.data.error != null) {
console.log("Axios Existing.. hd5d6589d");
console.log(data.data);
}
} else {
// console.log("Axios not existing.. c89d6f5c2");
// console.log(oldUser.tag + " vs " + newUser.tag);
// console.log(axios);
}
}
});