1 | import { get, post } from './bttps';
|
2 | import { fallbackData } from './fallbackListData';
|
3 | import DiscordJSClientFallback from '../types/discord.js';
|
4 |
|
5 | let listData: any;
|
6 | const listAge = new Date();
|
7 | let extendedLogging = false;
|
8 | let useBotblockAPI = true;
|
9 |
|
10 |
|
11 |
|
12 | type apiKeysObject = any;
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | async function postToAllLists(apiKeys: apiKeysObject): Promise<void> {
|
19 |
|
20 | const currentDate = new Date();
|
21 | if (!listData || listAge < currentDate) {
|
22 |
|
23 |
|
24 | listAge.setDate(currentDate.getDate() + 1);
|
25 | try {
|
26 | const tmpListData = await get('https://botblock.org/api/lists');
|
27 |
|
28 | if (tmpListData) {
|
29 | listData = tmpListData;
|
30 | } else {
|
31 | throw new Error('Got empty list from botblock.');
|
32 | }
|
33 | } catch (e) {
|
34 | console.error(`BLAPI: ${e}`);
|
35 | console.error(
|
36 | "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.",
|
37 | );
|
38 | }
|
39 | }
|
40 | Object.entries(listData).forEach(([listname]) => {
|
41 | if (
|
42 | apiKeys[listname]
|
43 | && (listData[listname].api_post || listname === 'discordbots.org')
|
44 | ) {
|
45 |
|
46 | let list = listData[listname];
|
47 | if (listname === 'discordbots.org') {
|
48 | list = fallbackData[listname];
|
49 | }
|
50 | const apiPath = list.api_post.replace(':id', apiKeys.bot_id);
|
51 |
|
52 | const sendObj: any = {};
|
53 | sendObj[list.api_field] = apiKeys.server_count;
|
54 | if (apiKeys.shard_id && list.api_shard_id) {
|
55 | sendObj[list.api_shard_id] = apiKeys.shard_id;
|
56 | }
|
57 | if (apiKeys.shard_count && list.api_shard_count) {
|
58 | sendObj[list.api_shard_count] = apiKeys.shard_count;
|
59 | }
|
60 | if (apiKeys.shards && list.api_shards) {
|
61 | sendObj[list.api_shards] = apiKeys.shards;
|
62 | }
|
63 |
|
64 | post(apiPath, apiKeys[listname], sendObj, extendedLogging).catch((e) => console.error(`BLAPI: ${e}`));
|
65 | }
|
66 | });
|
67 | }
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 | async function handleInternal(
|
75 | client: DiscordJSClientFallback,
|
76 | apiKeys: apiKeysObject,
|
77 | repeatInterval: number,
|
78 | ): Promise<void> {
|
79 | setTimeout(
|
80 | handleInternal.bind(null, client, apiKeys, repeatInterval),
|
81 | 60000 * repeatInterval,
|
82 | );
|
83 | let unchanged;
|
84 |
|
85 | if (client.user) {
|
86 |
|
87 | apiKeys.bot_id = client.user.id;
|
88 |
|
89 |
|
90 | if (client.shard && client.shard.id === 0) {
|
91 | apiKeys.shard_count = client.shard.count;
|
92 |
|
93 |
|
94 | try {
|
95 | const _: Array<number> = await client.shard.broadcastEval(
|
96 | 'this.guilds.size ? client.guilds.size : client.guilds.cache.size',
|
97 | );
|
98 | const shardCounts = _.filter((count: number) => count !== 0);
|
99 | if (shardCounts.length !== client.shard.count) {
|
100 |
|
101 | return;
|
102 | }
|
103 | apiKeys.shards = shardCounts;
|
104 | apiKeys.server_count = apiKeys.shards.reduce(
|
105 | (prev: number, val: number) => prev + val,
|
106 | 0,
|
107 | );
|
108 |
|
109 |
|
110 | } catch (e) {
|
111 | console.error('BLAPI: Error while fetching shard server counts:', e);
|
112 | }
|
113 |
|
114 | } else if (!client.shard) {
|
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 | apiKeys.server_count = client.guilds instanceof Map ? client.guilds.size : client.guilds.cache.size;
|
144 | } else {
|
145 | unchanged = true;
|
146 | }
|
147 | if (!unchanged) {
|
148 | if (repeatInterval > 2 && useBotblockAPI) {
|
149 |
|
150 | post(
|
151 | 'https://botblock.org/api/count',
|
152 | 'no key needed for this',
|
153 | apiKeys,
|
154 | extendedLogging,
|
155 | ).catch((e) => console.error(`BLAPI: ${e}`));
|
156 |
|
157 |
|
158 | if (apiKeys['discordbots.org']) {
|
159 | const newApiKeys: apiKeysObject = {};
|
160 | newApiKeys.bot_id = apiKeys.bot_id;
|
161 | newApiKeys['discordbots.org'] = apiKeys['discordbots.org'];
|
162 | newApiKeys.server_count = apiKeys.server_count;
|
163 | if (apiKeys.shard_count) {
|
164 | newApiKeys.shard_count = apiKeys.shard_count;
|
165 | }
|
166 | if (apiKeys.shards) {
|
167 | newApiKeys.shards = apiKeys.shards;
|
168 | }
|
169 | postToAllLists(newApiKeys);
|
170 | }
|
171 | } else {
|
172 | postToAllLists(apiKeys);
|
173 | }
|
174 | }
|
175 | } else {
|
176 | console.error(
|
177 | `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.`,
|
178 | );
|
179 | }
|
180 | }
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 | export function handle(
|
189 | discordClient: DiscordJSClientFallback,
|
190 | apiKeys: apiKeysObject,
|
191 | repeatInterval: number,
|
192 | ): Promise<void> {
|
193 |
|
194 |
|
195 | if (!repeatInterval || repeatInterval < 1) repeatInterval = 30;
|
196 | return handleInternal(discordClient, apiKeys, repeatInterval);
|
197 | }
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 | export function manualPost(
|
212 | guildCount: number,
|
213 | botID: string,
|
214 | apiKeys: apiKeysObject,
|
215 | shardID: number,
|
216 | shardCount: number,
|
217 | shards: Array<number>,
|
218 | ): void {
|
219 |
|
220 | apiKeys.bot_id = botID;
|
221 | apiKeys.server_count = guildCount;
|
222 |
|
223 | if (shardID === 0 || (shardID && !shards)) {
|
224 |
|
225 | apiKeys.shard_id = shardID;
|
226 | apiKeys.shard_count = shardCount;
|
227 | if (shards) {
|
228 | if (shards.length !== shardCount) {
|
229 | console.error(
|
230 | `BLAPI: Shardcount (${shardCount}) does not equal the length of the shards array (${shards.length}).`,
|
231 | );
|
232 | return;
|
233 | }
|
234 | apiKeys.shards = shards;
|
235 | apiKeys.server_count = apiKeys.shards.reduce(
|
236 | (prev: number, val: number) => prev + val,
|
237 | 0,
|
238 | );
|
239 | }
|
240 |
|
241 | }
|
242 |
|
243 | if (useBotblockAPI) {
|
244 | post(
|
245 | 'https://botblock.org/api/count',
|
246 | 'no key needed for this',
|
247 | apiKeys,
|
248 | extendedLogging,
|
249 | ).catch((e) => console.error(`BLAPI: ${e}`));
|
250 | } else {
|
251 | postToAllLists(apiKeys);
|
252 | }
|
253 | }
|
254 |
|
255 | export function setLogging(setLog: boolean): void {
|
256 | extendedLogging = setLog;
|
257 | }
|
258 |
|
259 | export function setBotblock(useBotblock: boolean): void {
|
260 | useBotblockAPI = useBotblock;
|
261 | }
|