Implement update checker

Co-authored-by: Aisuruneko <aisuru@nekos.tech>
This commit is contained in:
Sophie Marie 2023-01-25 16:10:45 -06:00
parent 38bc50d8a1
commit 631e4987d6
No known key found for this signature in database
GPG Key ID: 0C9FB786E7770775
4 changed files with 64 additions and 21 deletions

View File

@ -1,7 +1,13 @@
{
"prefix": "n!",
"build": "1.4dev",
"build": "1.4",
"footerTxt": "Anitrox, made with <3 by IDeletedSystem64 | 2018-2023",
"imageApi": "",
"updater": {
"enabled": true,
"frequency": "360",
"gitRepo": "IDeletedSystem64/anitrox"
},
"sandbox": {
"enabled": false,
"id": "0",

View File

@ -44,15 +44,19 @@ module.exports = {
console.log(' ___ _ __ ');
console.log(' / | ____ (_) /__________ _ __');
console.log(' / /| | / __ \\/ / __/ ___/ __ \\| |/_/');
console.log(' / ___ |/ / / / / /_/ / / /_/ /> < ');
console.log('/_/ |_/_/ /_/_/\\__/_/ \\____/_/|_| ');
console.log('');
console.log(`${config.release}, ${config.build}`);
console.log('Bot online. | Anitrox by IDeletedSystem64 | ALL MY CODE KEEPS BLOWING UP!');
console.log(' / ___ |/ / / / / /_/ / / /_/ /> w < ');
console.log(`/_/ |_/_/ /_/_/\\__/_/ \\____/_/|_| ${config.build}`);
console.log('Ready!| Anitrox by IDeletedSystem64 | Also check out Novetus!');
if (config.updater.enabled === true) {
await client.updater.checkUpdates(true); // This is probably the wrong way to do it
setInterval(async () => {
await client.updater.checkUpdates(true);
}, parseInt(config.updater.frequency * 60000)); // This is set to 6 hours (360 minutes) by default.
}
// Statuses
setInterval(async () => {
// Picks a status from the config file
const index = Math.floor(Math.random() * config.statuses.length);
const index = Math.floor(Math.random() * config.statuses.length); // Picks a status from the config file
await client.user?.setActivity(config.statuses[index]);
}, 20000);
}

View File

@ -1,20 +1,50 @@
/* eslint-disable brace-style */ // Tell eslint to stfu :)
const fetch = require('node-fetch');
const config = require('../config.json');
module.exports = () => {
const repo = `https://api.github.com/repos/${config.gitRepo}/releases/latest`;
fetch(repo).then(async (response) => {
response = await response.json();
const latest = response.name;
const currentVer = config.build;
class updateChecker {
constructor () {
this.status = null;
this.failReason = null;
}
let status = 0; // 0 = Up-to-date, 1 = New update.
checkUpdates = (notify) => {
const repo = `https://api.github.com/repos/${config.updater.gitRepo}/releases/latest`;
const getUpdates = () => {
return fetch(repo)
.then(async (response) => {
if (response.status !== 200) {
this.status = 2; // Error
this.failReason = response.statusText;
} else {
response = await response.json();
if (!response) { this.status = 2; } // Something went wrong while checking for updates :(
const version = {
latest: response.name,
current: config.build
};
if (currentVer === latest) { status = 0; } // Anitrox is up to date
else if (currentVer < latest) { status = 1; } // Anitrox is not up to date
else if (currentVer > latest) { status = 0; } // Anitrox is ahead, Treat this as it being up to date.
if (version.current === version.latest) { this.status = 0; return version; } // Up-to-date!
else if (version.current < version.latest) { this.status = 1; return version; } // Not up to date.
else this.status = 2; // Something went wrong while checking for updates :(
if (status === 1) console.log(`\n✨ It must be your lucky day! Anitrox ${latest} is now available! Download it from github.com/${config.gitRepo}/releases!`); // Log to console about the new release!
});
};
return response;
};
})
.catch(err => { console.error(err); });
};
return getUpdates().then(response => {
if (notify === true) {
if (this.status === 1) console.log(`\n✨ It must be your lucky day! Anitrox ${response.latest} is now available! Download it from github.com/${config.updater.gitRepo}/releases!`); // Log to console about the new release!
else if (this.status === 2) console.error(`\nSomething went wrong while checking for updates... :( | ${this.failReason}`); // This can probably be done more properly, But quite honestly I'm tired of working at this. ^system64
else if (this.status === 0) {} // Up-to-date
else console.error(`Unknown Update Status!! ${this.status}`);
} else return this.status;
});
};
getStatus = () => { return this.status; };
}
module.exports = function () { return new updateChecker(); };

View File

@ -47,6 +47,9 @@ client.generateErrorMessage = (errorMsg, avatarURL) => ({
]
}]
});
// Error message generator.
client.updater = require('./functions/updateCheck.js')(); // da update checker (real)
client.login(process.env.TOKEN);
// Login to Discord!