Skip to content

Commit 018ce3b

Browse files
committed
fix(#182): retry posting after 10 seconds when first attempt fails
1 parent 68045fb commit 018ce3b

File tree

1 file changed

+50
-10
lines changed

1 file changed

+50
-10
lines changed

src/main.ts

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,22 @@ export enum LogLevel {
3333
}
3434

3535
type LogOptions = {
36+
/**
37+
* @deprecated Use logLevel instead.
38+
*
39+
* If you want an equivalent to `extended: true`, use `logLevel: LogLevel.All`
40+
*/
3641
extended?: boolean;
42+
43+
/**
44+
* The log level to use
45+
* @default LogLevel.WarnAndErrorOnly
46+
*/
3747
logLevel?: LogLevel;
48+
49+
/**
50+
* Your custom logger to be used instead of the default console logger
51+
*/
3852
logger?: UserLogger;
3953
};
4054

@@ -64,6 +78,7 @@ const logger = {
6478
if (userLogger) {
6579
userLogger.info(createBlapiMessage(msg));
6680
} else {
81+
// eslint-disable-next-line no-console
6782
console.info(createBlapiMessage(msg));
6883
}
6984
},
@@ -74,6 +89,7 @@ const logger = {
7489
if (userLogger) {
7590
userLogger.warn(createBlapiMessage(msg));
7691
} else {
92+
// eslint-disable-next-line no-console
7793
console.warn(createBlapiMessage(msg));
7894
}
7995
},
@@ -84,6 +100,7 @@ const logger = {
84100
if (userLogger) {
85101
userLogger.error(createBlapiMessage(msg));
86102
} else {
103+
// eslint-disable-next-line no-console
87104
console.error(createBlapiMessage(msg));
88105
}
89106
},
@@ -204,6 +221,19 @@ async function postToAllLists(
204221
return Promise.all(posts);
205222
}
206223

224+
async function runHandleInternalInSeconds(
225+
client: Client,
226+
apiKeys: apiKeysObject,
227+
repeatInterval: number,
228+
seconds: number,
229+
): Promise<void> {
230+
setTimeout(
231+
/* eslint-disable-next-line no-use-before-define */
232+
() => handleInternal(client, apiKeys, repeatInterval),
233+
1000 * seconds,
234+
);
235+
}
236+
207237
/**
208238
* @param client Discord.js client
209239
* @param apiKeys A JSON object formatted like: {"botlist name":"API Keys for that list", etc.}
@@ -213,12 +243,11 @@ async function handleInternal(
213243
client: Client,
214244
apiKeys: apiKeysObject,
215245
repeatInterval: number,
246+
isFirstRun = false,
216247
): Promise<void> {
217-
setTimeout(
218-
/* eslint-disable-next-line @typescript-eslint/no-misused-promises */
219-
handleInternal.bind(null, client, apiKeys, repeatInterval),
220-
60000 * repeatInterval,
221-
); // call this function again in the next interval
248+
// call this function again in the next interval
249+
runHandleInternalInSeconds(client, apiKeys, repeatInterval, 60 * repeatInterval);
250+
222251
if (client.user) {
223252
const client_id = client.user.id;
224253
let unchanged;
@@ -259,8 +288,14 @@ async function handleInternal(
259288
);
260289

261290
if (shards.length !== client.ws.shards.size) {
262-
// If not all shards are up yet, we skip this run of handleInternal
263-
logger.info("Not all shards are up yet, so we're skipping this run.");
291+
// If not all shards are up yet, we skip this run of handleInternal and try again later
292+
if (isFirstRun) {
293+
const secondsToWait = 10;
294+
logger.info(`Not all shards are up yet, so we're trying again in ${secondsToWait} seconds.`);
295+
runHandleInternalInSeconds(client, apiKeys, repeatInterval, secondsToWait);
296+
return;
297+
}
298+
logger.error('Not all shards are up yet, but this is not the first time we\'re trying so we will wait for the entire interval.');
264299
return;
265300
}
266301
server_count = shards.reduce(
@@ -314,9 +349,13 @@ async function handleInternal(
314349
}
315350
}
316351
} else {
317-
logger.error(
318-
`Discord client seems to not be connected yet, so we're skipping this run of the post. We will try again in ${repeatInterval} minutes.`,
319-
);
352+
if (isFirstRun) {
353+
const secondsToWait = 10;
354+
logger.info(`Discord client seems to not be connected yet, so we're trying again in ${secondsToWait} seconds.`);
355+
runHandleInternalInSeconds(client, apiKeys, repeatInterval, secondsToWait);
356+
return;
357+
}
358+
logger.error('Discord client seems to not be connected yet, but this is not the first time we\'re trying so we will wait for the entire interval.');
320359
}
321360
}
322361

@@ -335,6 +374,7 @@ export function handle(
335374
discordClient,
336375
apiKeys,
337376
!repeatInterval || repeatInterval < 1 ? 30 : repeatInterval,
377+
true,
338378
);
339379
}
340380

0 commit comments

Comments
 (0)