1 | "use strict";
|
2 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3 | function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
4 | return new (P || (P = Promise))(function (resolve, reject) {
|
5 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
6 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
7 | function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
8 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
9 | });
|
10 | };
|
11 | var __generator = (this && this.__generator) || function (thisArg, body) {
|
12 | var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
13 | return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
14 | function verb(n) { return function (v) { return step([n, v]); }; }
|
15 | function step(op) {
|
16 | if (f) throw new TypeError("Generator is already executing.");
|
17 | while (_) try {
|
18 | if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
19 | if (y = 0, t) op = [op[0] & 2, t.value];
|
20 | switch (op[0]) {
|
21 | case 0: case 1: t = op; break;
|
22 | case 4: _.label++; return { value: op[1], done: false };
|
23 | case 5: _.label++; y = op[1]; op = [0]; continue;
|
24 | case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
25 | default:
|
26 | if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
27 | if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
28 | if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
29 | if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
30 | if (t[2]) _.ops.pop();
|
31 | _.trys.pop(); continue;
|
32 | }
|
33 | op = body.call(thisArg, _);
|
34 | } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
35 | if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
36 | }
|
37 | };
|
38 | Object.defineProperty(exports, "__esModule", { value: true });
|
39 | var bttps_1 = require("./bttps");
|
40 | var fallbackListData_1 = require("./fallbackListData");
|
41 | var listData;
|
42 | var listAge = new Date();
|
43 | var extendedLogging = false;
|
44 | var useBotblockAPI = true;
|
45 |
|
46 |
|
47 |
|
48 |
|
49 | function postToAllLists(apiKeys) {
|
50 | return __awaiter(this, void 0, void 0, function () {
|
51 | var currentDate, tmpListData, e_1;
|
52 | return __generator(this, function (_a) {
|
53 | switch (_a.label) {
|
54 | case 0:
|
55 | currentDate = new Date();
|
56 | if (!(!listData || listAge < currentDate)) return [3 , 4];
|
57 |
|
58 |
|
59 | listAge.setDate(currentDate.getDate() + 1);
|
60 | _a.label = 1;
|
61 | case 1:
|
62 | _a.trys.push([1, 3, , 4]);
|
63 | return [4 , bttps_1.get('https://botblock.org/api/lists')];
|
64 | case 2:
|
65 | tmpListData = _a.sent();
|
66 |
|
67 | if (tmpListData) {
|
68 | listData = tmpListData;
|
69 | }
|
70 | else {
|
71 | throw new Error('Got empty list from botblock.');
|
72 | }
|
73 | return [3 , 4];
|
74 | case 3:
|
75 | e_1 = _a.sent();
|
76 | console.error("BLAPI: " + e_1);
|
77 | 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.");
|
78 | return [3 , 4];
|
79 | case 4:
|
80 | Object.entries(listData).forEach(function (_a) {
|
81 | var listname = _a[0];
|
82 | if (apiKeys[listname]
|
83 | && (listData[listname].api_post || listname === 'discordbots.org')) {
|
84 |
|
85 | var list = listData[listname];
|
86 | if (listname === 'discordbots.org') {
|
87 | list = fallbackListData_1.fallbackData[listname];
|
88 | }
|
89 | var apiPath = list.api_post.replace(':id', apiKeys.bot_id);
|
90 |
|
91 | var sendObj = {};
|
92 | sendObj[list.api_field] = apiKeys.server_count;
|
93 | if (apiKeys.shard_id && list.api_shard_id) {
|
94 | sendObj[list.api_shard_id] = apiKeys.shard_id;
|
95 | }
|
96 | if (apiKeys.shard_count && list.api_shard_count) {
|
97 | sendObj[list.api_shard_count] = apiKeys.shard_count;
|
98 | }
|
99 | if (apiKeys.shards && list.api_shards) {
|
100 | sendObj[list.api_shards] = apiKeys.shards;
|
101 | }
|
102 | bttps_1.post(apiPath, apiKeys[listname], sendObj, extendedLogging).catch(function (e) { return console.error("BLAPI: " + e); });
|
103 | }
|
104 | });
|
105 | return [2 ];
|
106 | }
|
107 | });
|
108 | });
|
109 | }
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 | function handleInternal(client, apiKeys, repeatInterval) {
|
116 | return __awaiter(this, void 0, void 0, function () {
|
117 | var unchanged, _, shardCounts, e_2, newApiKeys;
|
118 | return __generator(this, function (_a) {
|
119 | switch (_a.label) {
|
120 | case 0:
|
121 | setTimeout( handleInternal.bind(null, client, apiKeys, repeatInterval), 60000 * repeatInterval);
|
122 | if (!client.user) return [3 , 7];
|
123 |
|
124 | apiKeys.bot_id = client.user.id;
|
125 | if (!(client.shard && client.shard.id === 0)) return [3 , 5];
|
126 | apiKeys.shard_count = client.shard.count;
|
127 | _a.label = 1;
|
128 | case 1:
|
129 | _a.trys.push([1, 3, , 4]);
|
130 | return [4 , client.shard.broadcastEval('this.guilds.size ? client.guilds.size : client.guilds.cache.size')];
|
131 | case 2:
|
132 | _ = _a.sent();
|
133 | shardCounts = _.filter(function (count) { return count !== 0; });
|
134 | if (shardCounts.length !== client.shard.count) {
|
135 |
|
136 | return [2 ];
|
137 | }
|
138 | apiKeys.shards = shardCounts;
|
139 | apiKeys.server_count = apiKeys.shards.reduce(function (prev, val) { return prev + val; }, 0);
|
140 | return [3 , 4];
|
141 | case 3:
|
142 | e_2 = _a.sent();
|
143 | console.error('BLAPI: Error while fetching shard server counts:', e_2);
|
144 | return [3 , 4];
|
145 | case 4: return [3 , 6];
|
146 | case 5:
|
147 | if (!client.shard) {
|
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 | apiKeys.server_count = client.guilds instanceof Map ? client.guilds.size : client.guilds.cache.size;
|
177 | }
|
178 | else {
|
179 | unchanged = true;
|
180 | }
|
181 | _a.label = 6;
|
182 | case 6:
|
183 | if (!unchanged) {
|
184 | if (repeatInterval > 2 && useBotblockAPI) {
|
185 |
|
186 | bttps_1.post('https://botblock.org/api/count', 'no key needed for this', apiKeys, extendedLogging).catch(function (e) { return console.error("BLAPI: " + e); });
|
187 |
|
188 | if (apiKeys['discordbots.org']) {
|
189 | newApiKeys = {};
|
190 | newApiKeys.bot_id = apiKeys.bot_id;
|
191 | newApiKeys['discordbots.org'] = apiKeys['discordbots.org'];
|
192 | newApiKeys.server_count = apiKeys.server_count;
|
193 | if (apiKeys.shard_count) {
|
194 | newApiKeys.shard_count = apiKeys.shard_count;
|
195 | }
|
196 | if (apiKeys.shards) {
|
197 | newApiKeys.shards = apiKeys.shards;
|
198 | }
|
199 | postToAllLists(newApiKeys);
|
200 | }
|
201 | }
|
202 | else {
|
203 | postToAllLists(apiKeys);
|
204 | }
|
205 | }
|
206 | return [3 , 8];
|
207 | case 7:
|
208 | 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.");
|
209 | _a.label = 8;
|
210 | case 8: return [2 ];
|
211 | }
|
212 | });
|
213 | });
|
214 | }
|
215 |
|
216 |
|
217 |
|
218 |
|
219 |
|
220 |
|
221 | function handle(discordClient, apiKeys, repeatInterval) {
|
222 |
|
223 |
|
224 | if (!repeatInterval || repeatInterval < 1)
|
225 | repeatInterval = 30;
|
226 | return handleInternal(discordClient, apiKeys, repeatInterval);
|
227 | }
|
228 | exports.handle = handle;
|
229 |
|
230 |
|
231 |
|
232 |
|
233 |
|
234 |
|
235 |
|
236 |
|
237 |
|
238 |
|
239 |
|
240 |
|
241 | function manualPost(guildCount, botID, apiKeys, shardID, shardCount, shards) {
|
242 |
|
243 | apiKeys.bot_id = botID;
|
244 | apiKeys.server_count = guildCount;
|
245 |
|
246 | if (shardID === 0 || (shardID && !shards)) {
|
247 |
|
248 | apiKeys.shard_id = shardID;
|
249 | apiKeys.shard_count = shardCount;
|
250 | if (shards) {
|
251 | if (shards.length !== shardCount) {
|
252 | console.error("BLAPI: Shardcount (" + shardCount + ") does not equal the length of the shards array (" + shards.length + ").");
|
253 | return;
|
254 | }
|
255 | apiKeys.shards = shards;
|
256 | apiKeys.server_count = apiKeys.shards.reduce(function (prev, val) { return prev + val; }, 0);
|
257 | }
|
258 |
|
259 | }
|
260 |
|
261 | if (useBotblockAPI) {
|
262 | bttps_1.post('https://botblock.org/api/count', 'no key needed for this', apiKeys, extendedLogging).catch(function (e) { return console.error("BLAPI: " + e); });
|
263 | }
|
264 | else {
|
265 | postToAllLists(apiKeys);
|
266 | }
|
267 | }
|
268 | exports.manualPost = manualPost;
|
269 | function setLogging(setLog) {
|
270 | extendedLogging = setLog;
|
271 | }
|
272 | exports.setLogging = setLogging;
|
273 | function setBotblock(useBotblock) {
|
274 | useBotblockAPI = useBotblock;
|
275 | }
|
276 | exports.setBotblock = setBotblock;
|