From 50e62e246fef7975866c244e08ead7c681d50349 Mon Sep 17 00:00:00 2001 From: hachem Date: Tue, 9 Dec 2025 21:55:02 +0100 Subject: [add]: prompt+banner+refactor --- lib/modrinth.js | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 lib/modrinth.js (limited to 'lib/modrinth.js') diff --git a/lib/modrinth.js b/lib/modrinth.js new file mode 100644 index 0000000..cf0f697 --- /dev/null +++ b/lib/modrinth.js @@ -0,0 +1,69 @@ +const axios = require("axios") + +const MODRINTH_API = "https://api.modrinth.com/v2" + +const GetModVersions = async slug => +{ + try + { + const response = await axios.get(`${MODRINTH_API}/project/${slug}/version`) + return response.data + } + catch (err) + { + console.error(`[Modrinth] Failed for ${slug}:`, err.message) + return null + } +} + +const GetLatestVersion = versions => +{ + if (!versions || !versions.length) + return null + return versions.sort((a, b) => new Date(b.date_published) - new Date(a.date_published))[0] +} + +const GetCompatibleVersion = (versions, minecraftVersion, loader = null) => +{ + if (!versions || !versions.length) + return null + + const compatible = versions.filter(v => + { + const hasVersion = v.game_versions.includes(minecraftVersion) + const hasLoader = loader ? v.loaders.includes(loader) : true + return hasVersion && hasLoader + }) + + if (!compatible.length) + return null + + return compatible.sort((a, b) => new Date(b.date_published) - new Date(a.date_published))[0] +} + +const DownloadFile = async (url, destPath) => +{ + const fs = require("fs-extra") + + try + { + const response = await axios.get(url, { responseType: "arraybuffer" }) + await fs.writeFile(destPath, response.data) + return true + } + catch (err) + { + console.error(`[Download] Failed:`, err.message) + return false + } +} + +module.exports = +{ + MODRINTH_API, + GetModVersions, + GetLatestVersion, + GetCompatibleVersion, + DownloadFile +} + -- cgit v1.3