forked from LakeYS/Discord-Trivia-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshard.js
138 lines (113 loc) · 4.26 KB
/
shard.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
const Discord = require("discord.js");
const { Client } = Discord;
const snekfetch = require("snekfetch");
var Config = require("./lib/config.js")(process.argv[2]).config;
global.client = new Client({
retryLimit: 3,
messageCacheMaxSize: 50
});
global.Trivia = require("./triviabot.js");
if(Config["fallback-mode"] && Config["debug-mode"]) {
require("./lib/failover_client.js")(Config);
}
else if(Config["debug-mode"]) {
require("./lib/failover_server.js");
}
if(Config["debug-log"]) {
global.client.on("debug", (info) => {
console.log("DEBUG [" + global.client.shard.id + "]: " + info);
});
}
// # Post to Bot Listings # //
function postBotStats() {
// The following sites only need the total shard count, so we'll only post using the last shard.
// TODO: Fix this for when shards spawn out of order
if(global.client.shard.id === global.client.shard.count-1) {
global.client.shard.fetchClientValues("guilds.size")
.then((countArray) => {
var guildCountVal = countArray.reduce((prev, val) => prev + val, 0);
var id = global.client.user.id;
if(guildCountVal > 1) {
console.log("===== Posting guild count of\x1b[1m " + guildCountVal + "\x1b[0m =====");
}
var listings = {
// If 'data' not specified, assume it is this: { server_count: guildCount }
"botsfordiscord.com": {
url: `https://botsfordiscord.com/api/bot/${id}/`
},
"botlist.space": {
url: `https://botlist.space/api/bots/${id}/`
},
"discordbots.group": {
url: `https://discordbots.group/api/bot/${id}`,
data: { count: guildCountVal }
},
"discord.bots.gg": {
url: `https://discord.bots.gg/api/v1/bots/${id}/stats`,
data: { guildCount: guildCountVal }
},
"discordbots.org": {
url: `https://discordbots.org/api/bots/${id}/stats`
},
"discordbot.world": {
url: `https://discordbot.world/api/bot/${id}/stats`
}
};
for(var site in listings) {
if(Config[`${site}-token`] && Config[`${site}-token`] !== "optionaltokenhere") {
var data = listings[site].data || { server_count: guildCountVal };
snekfetch.post(listings[site].url)
.set("Authorization", Config[`${site}-token`])
.send(data)
.catch((err) => {
console.log(`Error occurred while posting to ${err.request.connection.servername} on shard ${global.client.shard.id}:\n${err}`);
if(typeof err.text !== "undefined") {
console.log("Response included with the error: " + err.text);
}
})
.then((res) => {
if(typeof res !== "undefined") {
console.log(`Posted to site ${res.request.connection.servername}, received response: ${res.text}`);
}
});
}
}
});
}
}
// # Custom Package Loading # //
if(typeof Config["additional-packages"] !== "undefined") {
Config["additional-packages"].forEach((key) => {
require(key)(global.Trivia);
});
}
// # Discord Client Login # //
global.client.login(global.client.token);
process.title = `Trivia - Shard ${global.client.shard.id} (Initializing)`;
global.client.on("ready", () => {
console.log("Shard " + global.client.shard.id + " connected to\x1b[1m " + global.client.guilds.size + " \x1b[0mserver" + (global.client.guilds.size===1?"":"s") + ".");
process.title = `Trivia - Shard ${global.client.shard.id}`;
if(global.client.user.avatar === null) {
console.log("Set profile image to profile.png");
global.client.user.setAvatar("./profile.png");
}
global.client.user.setPresence({ game: { name: "Trivia! Type '" + Config.prefix + "help' to get started.", type: 0 } });
postBotStats();
});
global.client.on("disconnect", () => {
console.log("Discord client disconnected.");
});
global.client.on("error", (err) => {
console.log("Discord client error: " + err.message);
global.Trivia.exportGame();
process.exit();
});
global.client.on("message", (msg) => {
var str = msg.toString().toUpperCase();
if(msg.channel.type === "text" || msg.channel.type === "dm") {
global.Trivia.parse(str, msg);
}
});
global.client.on("messageReactionAdd", (reaction, user) => {
global.Trivia.reactionAdd(reaction, user);
});