Skip to content

Commit

Permalink
Improve checking for updates
Browse files Browse the repository at this point in the history
  • Loading branch information
GoudronViande24 committed Jul 29, 2022
1 parent d1841ec commit 42fd55e
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 21 deletions.
110 changes: 96 additions & 14 deletions core/commands/checkupdates.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,73 @@ import Artibot from "../../index.js";
* @param {string[]} args
* @param {Artibot} artibot
*/
export default async function execute(message, args, { config, localizer, version, checkForUpdates, modules }) {
export default async function execute(message, args, { config, localizer, version, checkForUpdates, checkForPackageUpdates, modules }) {
// Check if config is valid
if (!config.checkForUpdates) {
message.reply(localizer._("Checking for updates is disabled in config!"));
return;
};

const latest = await checkForUpdates();
// Check if an argument is passed
if (args.length) {
const moduleId = args[0].toLowerCase();

if (moduleId == "artibot") {
const latest = await checkForPackageUpdates();

let content;

if (!latest) {
content = "**Artibot:** " + localizer._("Impossible to get latest version!");
} else if (version == latest) {
content = "**Artibot:** " + localizer.__("Already up to date (v[[0]]).", { placeholders: [version] });
} else {
content = "**Artibot:** " + localizer.__("An update is available: v[[0]] --> v[[1]].", { placeholders: [version, latest] });
}

return await message.reply({ content });
}

const module = modules.get(moduleId);

if (!module) return await message.reply(localizer.__("Module with ID `[[0]]` not found.", { placeholders: [moduleId] }));

const { name, repo, packageName } = module;
version = module.version;
let content = name + ": " + localizer._("Impossible to get latest version!") + "\n";

if (packageName) {
const latest = await checkForPackageUpdates(packageName);

if (latest) {
if (version == latest) {
content = name + ": " + localizer.__("Already up to date (v[[0]]).", { placeholders: [version] });
} else {
content = name + ": " + localizer.__("An update is available: v[[0]] --> v[[1]].", { placeholders: [version, latest] });
}

return await message.reply({ content });
}
}

if (repo) {
const latest = await checkForUpdates(repo);

if (latest) {
if (version == latest) {
content = name + ": " + localizer.__("Already up to date (v[[0]]).", { placeholders: [version] });
} else {
content = name + ": " + localizer.__("An update is available: v[[0]] --> v[[1]].", { placeholders: [version, latest] });
}

return await message.reply({ content });
}
}

return await message.reply({ content });
}

const latest = await checkForPackageUpdates();

let reply;

Expand All @@ -29,19 +88,42 @@ export default async function execute(message, args, { config, localizer, versio
}

for (const [, module] of modules) {
if (!module.repo) continue;
const { name, version, repo } = module;
const latest = await checkForUpdates(repo);

if (!latest) {
reply += name + ": " + localizer._("Impossible to get latest version!") + "\n";
} else if (version == latest) {
reply += name + ": " + localizer.__("Already up to date (v[[0]]).", { placeholders: [version] }) + "\n";
} else {
reply += name + ": " + localizer.__("An update is available: v[[0]] --> v[[1]].", {
placeholders: [version, latest]
}) + "\n";
const { name, version, repo, packageName } = module;

if (packageName) {
const latest = await checkForPackageUpdates(packageName);

if (latest) {
if (version == latest) {
reply += name + ": " + localizer.__("Already up to date (v[[0]]).", { placeholders: [version] }) + "\n";
} else {
reply += name + ": " + localizer.__("An update is available: v[[0]] --> v[[1]].", {
placeholders: [version, latest]
}) + "\n";
}

continue;
}
}

if (repo) {
const latest = await checkForUpdates(repo);

if (latest) {
if (version == latest) {
reply += name + ": " + localizer.__("Already up to date (v[[0]]).", { placeholders: [version] }) + "\n";
} else {
reply += name + ": " + localizer.__("An update is available: v[[0]] --> v[[1]].", {
placeholders: [version, latest]
}) + "\n";
}

continue;
}
}

// If no way to find the latest version
reply += name + ": " + localizer._("Impossible to get latest version!") + "\n";
}

message.reply(reply.trim());
Expand Down
2 changes: 2 additions & 0 deletions core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export default function coreModule(artibot) {
name: "Artibot Core",
id: "core",
repo: "Artivain/artibot",
packageName: "artibot",
version,
langs: ["en", "fr"],
parts: [
Expand Down Expand Up @@ -61,6 +62,7 @@ export default function coreModule(artibot) {
id: "checkupdates",
name: "checkupdates",
description: localizer._("Check updates for the bot"),
usage: localizer._("[module id]"),
ownerOnly: true,
mainFunction: checkupdatesCommand
}),
Expand Down
29 changes: 27 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,14 +232,37 @@ export class Artibot {
headers: {
"User-Agent": "Artibot/" + this.version
},
validateStatus: () => { return true }
validateStatus: () => true
});

if (request.status != 200) return false;

const { data } = request;
return data.name.replace("v", "");
}

/**
* Get latest release version of a NPM package
* @param {String} [packageName="artibot"] - Package name on NPM
* @returns {Promise<string|false>} Version number, or false if package not found or an error happens
* @method
*/
checkForPackageUpdates = async (packageName = "artibot") => {
const request = await axios({
method: "GET",
url: `https://api.npms.io/v2/package/${packageName}`,
responseType: "json",
headers: {
"User-Agent": "Artibot/" + this.version
},
validateStatus: () => true
});

if (request.status != 200) return false;

const { data } = request;
return data.collected.metadata.version;
}
}

/** @ignore */
Expand All @@ -263,8 +286,9 @@ export class Module {
* @param {ModulePartResolvable[]} config.parts - List of parts of the module
* @param {IntentsResolvable[]} [config.intents] - List of required intents
* @param {string} [config.repo] - GitHub repository of the module (ex.: "Artivain/artibot")
* @param {String} [config.packageName] - Package name of the module on NPM (ex.: "artibot")
*/
constructor({ name, id, version, langs = "any", parts, intents = [], repo }) {
constructor({ name, id, version, langs = "any", parts, intents = [], repo, packageName }) {
if (!name || !id || !version || !langs || !parts) throw new Error("Missing module informations!");
this.name = name;
this.id = id;
Expand All @@ -273,6 +297,7 @@ export class Module {
this.parts = parts;
this.additionalIntents = intents;
this.repo = repo;
this.packageName = packageName;
}
}

Expand Down
8 changes: 8 additions & 0 deletions locales.json
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,14 @@

"Error when registering module: ": {
"fr": "Une erreur est survenue en chargeant un module: "
},

"[module id]": {
"fr": "[id du module]"
},

"Module with ID `[[0]]` not found.": {
"fr": "Il n'y a pas de module avec le ID `[[0]]` installé."
}

}
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "artibot",
"version": "4.0.0",
"version": "4.1.0",
"description": "Modern, fast and modular open-source Discord bot",
"main": "index.js",
"scripts": {
"start": "node index.js",
"test": "nodemon testing.js",
"test": "nodemon testing.js --ignore data/",
"upgrade": "ncu -u && npm install && npm update",
"build-docs": "jsdoc -c ./jsdoc.json"
},
Expand Down
4 changes: 3 additions & 1 deletion testing.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,6 @@ artibot.registerModule(
})
);

artibot.login({ token });
artibot.login({ token });

console.log(await artibot.checkForPackageUpdates());

0 comments on commit 42fd55e

Please sign in to comment.