1 | const { join } = require('path');
|
2 | const bttps = require(join(__dirname, 'bttps.js'));
|
3 | const fallbackListData = require('./fallbackListData.json');
|
4 |
|
5 | let listData;
|
6 | const listAge = new Date();
|
7 | let extendedLogging = false;
|
8 | let useBotblockAPI = true;
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | const postToAllLists = async apiKeys => {
|
14 |
|
15 | const currentDate = new Date();
|
16 | if (!listData || listAge < currentDate) {
|
17 | listAge.setDate(currentDate.getDate() + 1);
|
18 | listData = await bttps.get('https://botblock.org/api/lists').catch(e => console.error(`BLAPI: ${e}`));
|
19 | if (!listData) {
|
20 | console.error("BLAPI : Something went wrong when contacting BotBlock for the API of the lists, so we're using an older preset. Some lists might not be available because of this.");
|
21 | listData = fallbackListData;
|
22 | }
|
23 | }
|
24 | for (const listname in listData) {
|
25 | if (apiKeys[listname] && (listData[listname]['api_post'] || listname === 'discordbots.org')) {
|
26 | let list = listData[listname];
|
27 | if (listname === 'discordbots.org') {
|
28 | list = fallbackListData[listname];
|
29 | }
|
30 | const url = `https://${listname}`;
|
31 | const apiPath = list.api_post.replace(url, '').replace(':id', apiKeys.bot_id);
|
32 |
|
33 | let sendObjString = `{ "${list.api_field}": ${apiKeys.server_count}`;
|
34 | if (apiKeys.shard_id && list.api_shard_id) {
|
35 | sendObjString += `, "${list.api_shard_id}": ${apiKeys.shard_id}`;
|
36 | }
|
37 | if (apiKeys.shard_count && list.api_shard_count) {
|
38 | sendObjString += `, "${list.api_shard_count}": ${apiKeys.shard_count}`;
|
39 | }
|
40 | if (apiKeys.shards && list.api_shards) {
|
41 | sendObjString += `, "${list.api_shards}": ${apiKeys.shards}`;
|
42 | }
|
43 | sendObjString += ' }';
|
44 | const sendObj = JSON.parse(sendObjString);
|
45 | bttps.post(listname, apiPath, apiKeys[listname], sendObj, extendedLogging).catch(e => console.error(`BLAPI: ${e}`));
|
46 | }
|
47 | }
|
48 | };
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 | const handleInternal = async (client, apiKeys, repeatInterval) => {
|
56 | setTimeout(handleInternal.bind(null, client, apiKeys, repeatInterval), 60000 * repeatInterval);
|
57 | if (client.user) {
|
58 |
|
59 | apiKeys.bot_id = client.user.id;
|
60 |
|
61 | if (client.shard && client.shard.id === 0) {
|
62 | apiKeys.shard_count = client.shard.count;
|
63 |
|
64 |
|
65 | const shardCounts = await client.shard.broadcastEval('this.guilds.size').catch(e => console.error('BLAPI: Error while fetching shard server counts:', e));
|
66 | if (shardCounts.length !== client.shard.count) {
|
67 |
|
68 | return;
|
69 | }
|
70 |
|
71 | apiKeys.shards = shardCounts;
|
72 | apiKeys.server_count = apiKeys.shards.reduce((prev, val) => prev + val, 0);
|
73 |
|
74 |
|
75 | } else if (client.ws && client.ws.shards) {
|
76 | apiKeys.shard_count = client.ws.shards.size;
|
77 |
|
78 | const shardCounts = [];
|
79 | client.ws.shards.forEach(shard => {
|
80 | let count = 0;
|
81 | client.guilds.forEach(g => {
|
82 | if (g.shardID === shard.id) count++;
|
83 | });
|
84 | shardCounts.push(count);
|
85 | });
|
86 | if (shardCounts.length !== client.ws.shards.size) {
|
87 |
|
88 | console.log("BLAPI: Not all shards are up yet, so we're skipping this run.");
|
89 | return;
|
90 | }
|
91 | apiKeys.shards = shardCounts;
|
92 | apiKeys.server_count = apiKeys.shards.reduce((prev, val) => prev + val, 0);
|
93 | } else {
|
94 | apiKeys.server_count = client.guilds.size;
|
95 | }
|
96 |
|
97 | if (repeatInterval > 2 && useBotblockAPI) {
|
98 | bttps.post('botblock.org', '/api/count', 'no key needed for this', apiKeys, extendedLogging).catch(e => console.error(`BLAPI: ${e}`));
|
99 |
|
100 |
|
101 | if (apiKeys['discordbots.org']) {
|
102 | const newApiKeys = {};
|
103 |
|
104 | newApiKeys.bot_id = apiKeys.bot_id;
|
105 | newApiKeys['discordbots.org'] = apiKeys['discordbots.org'];
|
106 | newApiKeys.server_count = apiKeys.server_count;
|
107 | if (apiKeys.shard_count) {
|
108 | newApiKeys.shard_count = apiKeys.shard_count;
|
109 | }
|
110 | if (apiKeys.shards) {
|
111 | newApiKeys.shards = apiKeys.shards;
|
112 | }
|
113 |
|
114 | postToAllLists(newApiKeys);
|
115 | }
|
116 | } else {
|
117 | postToAllLists(apiKeys);
|
118 | }
|
119 | } else {
|
120 | console.error(`BLAPI : Discord client seems to not be connected yet, so we're skipping this run of the post. We will try again in ${repeatInterval} minutes.`);
|
121 | }
|
122 | };
|
123 |
|
124 | module.exports = {
|
125 | |
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 | handle: (discordClient, apiKeys, repeatInterval) => {
|
132 |
|
133 | if (!repeatInterval || repeatInterval < 1) repeatInterval = 30;
|
134 | handleInternal(discordClient, apiKeys, repeatInterval);
|
135 | },
|
136 | |
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 | manualPost: (guildCount, botID, apiKeys, shardID, shardCount, shards) => {
|
146 |
|
147 | apiKeys.bot_id = botID;
|
148 | apiKeys.server_count = guildCount;
|
149 |
|
150 | if (shardID === 0 || (shardID && !shards)) {
|
151 | apiKeys.shard_id = shardID;
|
152 | apiKeys.shard_count = shardCount;
|
153 | if (shards) {
|
154 | if (shards.length !== shardCount) {
|
155 | console.error(`BLAPI: Shardcount (${shardCount}) does not equal the length of the shards array (${shards.length}).`);
|
156 | return;
|
157 | }
|
158 | apiKeys.shards = shards;
|
159 | apiKeys.server_count = apiKeys.shards.reduce((prev, val) => prev + val, 0);
|
160 | }
|
161 |
|
162 | }
|
163 | if (useBotblockAPI) {
|
164 | bttps.post('botblock.org', '/api/count', 'no key needed for this', apiKeys, extendedLogging).catch(e => console.error(`BLAPI: ${e}`));
|
165 | } else {
|
166 | postToAllLists(apiKeys);
|
167 | }
|
168 | },
|
169 | setLogging: setLogging => {
|
170 | extendedLogging = setLogging;
|
171 | },
|
172 | setBotblock: useBotblock => {
|
173 | useBotblockAPI = useBotblock;
|
174 | }
|
175 | };
|