Merge pull request #2 from IrPgFKS0/co-work

Nitrado API Retry and other Docker quality of life changes
This commit is contained in:
SowinskiBraeden authored and GitHub committed 2023-09-02 23:02:00 -07:00
commit bb347c2812
3 files changed
+28 -8

No files matched your search

+1
View File
@@ -12,3 +12,4 @@ npm-debug.log
#env #env
.env .env
.DS_Store
+7 -7
View File
@@ -104,11 +104,11 @@ class DayzRBot extends Client {
error(Text) { this.logger.error(Text); } error(Text) { this.logger.error(Text); }
async getDateEST(time) { async getDateEST(time) {
let t = new Date(); // Get current date (PST) let timeArray = time.split(' ')[0].split(':')
let e = new Date(t.getTime() + 180*60*1000); // Convert to EST to ensure the date is correct for the applied EST time let t = new Date(); // Get current date & time (UTC)
let n = new Date(`${e.getFullYear()}-${e.getMonth()<10?'0':''}${e.getMonth()+1}-${e.getDate()<10?'0':''}${e.getDate()}T${time.split(' ')[0]}`) // Apply given time to EST date let f = new Date(t.getTime() - 4 * 3600000) // Convert UTC into EST time to roll back the day as necessary
let f = new Date(n.getTime() - 180*60*1000) // Convert back to PST f.setUTCHours(timeArray[0], timeArray[1], timeArray[2]); // Apply the supplied EST time to the converted date (EST is the timezone produced from the Nitrado logs).
return f; return new Date(f.getTime() + 4 * 3600000); // Add EST time offset to return timestamp in UTC
} }
async readLogs(guildId) { async readLogs(guildId) {
@@ -246,12 +246,12 @@ class DayzRBot extends Client {
this.databaseConnected = true; this.databaseConnected = true;
} catch (err) { } catch (err) {
databaselogs.attempts++; databaselogs.attempts++;
this.error(`Failed to connect to mongodb: attempt ${databaselogs.attempts}`); this.error(`Failed to connect to mongodb (mongodb://${mongoURI.split('@')[1]}/${dbo}): attempt ${databaselogs.attempts} - Error: ${err}`);
failed = true; failed = true;
} }
// write JSON string to a file // write JSON string to a file
await fs.writeFileSync(dbLogDir, JSON.stringify(databaselogs)); fs.writeFileSync(dbLogDir, JSON.stringify(databaselogs));
if (failed) process.exit(-1); if (failed) process.exit(-1);
} }
+20 -1
View File
@@ -3,10 +3,13 @@ const concat = require('concat-stream');
const { Readable } = require('stream'); const { Readable } = require('stream');
const FormData = require('form-data'); const FormData = require('form-data');
const fs = require('fs'); const fs = require('fs');
const maxRetries = 5;
const retryDelay = 5000; // 5 second
module.exports = { module.exports = {
DownloadNitradoFile: async (client, filename, outputDir) => { DownloadNitradoFile: async (client, filename, outputDir) => {
for (let retries = 0; retries <= maxRetries; retries++) {
try {
const res = await fetch(`https://api.nitrado.net/services/${client.config.Nitrado.ServerID}/gameservers/file_server/download?file=${filename}`, { const res = await fetch(`https://api.nitrado.net/services/${client.config.Nitrado.ServerID}/gameservers/file_server/download?file=${filename}`, {
headers: { headers: {
"Authorization": client.config.Nitrado.Auth "Authorization": client.config.Nitrado.Auth
@@ -24,10 +27,19 @@ module.exports = {
const { body } = await fetch(res.data.token.url); const { body } = await fetch(res.data.token.url);
await finished(Readable.fromWeb(body).pipe(stream)); await finished(Readable.fromWeb(body).pipe(stream));
return 0; return 0;
} catch (error) {
if (retries === maxRetries) {
throw new Error(`Failed to fetch data after ${maxRetries} retries`);
}
}
await new Promise(resolve => setTimeout(resolve, retryDelay)); // Delay before retrying
}
}, },
HandlePlayerBan: async (client, gamertag, ban) => { HandlePlayerBan: async (client, gamertag, ban) => {
// get current bans // get current bans
for (let retries = 0; retries <= maxRetries; retries++) {
try {
const res = await fetch(`https://api.nitrado.net/services/${client.config.Nitrado.ServerID}/gameservers`, { const res = await fetch(`https://api.nitrado.net/services/${client.config.Nitrado.ServerID}/gameservers`, {
headers: { headers: {
"Authorization": client.config.Nitrado.Auth "Authorization": client.config.Nitrado.Auth
@@ -59,6 +71,13 @@ module.exports = {
} }
sendList(); sendList();
})); }));
} catch (error) {
if (retries === maxRetries) {
throw new Error(`Failed to fetch data after ${maxRetries} retries`);
}
}
await new Promise(resolve => setTimeout(resolve, retryDelay)); // Delay before retrying
}
}, },
/* /*