hachem.wtf
aboutsummaryrefslogtreecommitdiff
path: root/bot/app.js
diff options
context:
space:
mode:
Diffstat (limited to 'bot/app.js')
-rw-r--r--bot/app.js63
1 files changed, 9 insertions, 54 deletions
diff --git a/bot/app.js b/bot/app.js
index 3fc9134..59219eb 100644
--- a/bot/app.js
+++ b/bot/app.js
@@ -5,26 +5,22 @@ const
GatewayIntentBits
} = require("discord.js")
-const axios = require("axios")
-const fs = require("fs-extra")
-const path = require("path")
+const { LoadConfig, LoadApi, LoadCache, SaveCache } = require("../lib/config")
+const { GetModVersions, GetLatestVersion, GetCompatibleVersion } = require("../lib/modrinth")
-const config = require("../config/config.json")
-const api = require("../config/api.json")
+const config = LoadConfig()
+const api = LoadApi()
const
{
channel_id,
check_interval_minutes,
minecraft_version,
- fabric_loader_version,
- loader_meta_version,
modrinth_mods
} = config
const { token } = api
-const cacheFile = path.join(__dirname, "../cache/mod_versions.json")
const client = new Client
({
intents:
@@ -34,47 +30,6 @@ const client = new Client
]
})
-const LoadCache = async () =>
-{
- try
- {
- if (await fs.pathExists(cacheFile))
- return JSON.parse(await fs.readFile(cacheFile, "utf8"))
- }
- catch (err)
- {
- console.error("[Cache] Failed to load:", err)
- }
- return {}
-}
-
-const SaveCache = async cache =>
-{
- try
- {
- await fs.ensureDir(path.dirname(cacheFile))
- await fs.writeFile(cacheFile, JSON.stringify(cache, null, 2))
- }
- catch (err)
- {
- console.error("[Cache] Failed to save:", err)
- }
-}
-
-const GetModVersions = async slug =>
-{
- try
- {
- const response = await axios.get(`https://api.modrinth.com/v2/project/${slug}/version`)
- return response.data
- }
- catch (err)
- {
- console.error(`[Modrinth] Failed for ${slug}:`, err.message)
- return null
- }
-}
-
const CheckMods = async () =>
{
const cache = await LoadCache()
@@ -90,8 +45,8 @@ const CheckMods = async () =>
if (!versions)
continue
- const latest = versions.sort((a, b) => new Date(b.date_published) - new Date(a.date_published))[0]
- const compatible = versions.find(v => v.game_versions.includes(minecraft_version)) || null
+ const latest = GetLatestVersion(versions)
+ const compatible = GetCompatibleVersion(versions, minecraft_version)
const cached = cache[slug] ||
{
@@ -102,7 +57,7 @@ const CheckMods = async () =>
newCache[slug] =
{
compatible: compatible ? compatible.id : null,
- latest: latest.id
+ latest: latest ? latest.id : null
}
if (!compatible)
@@ -117,7 +72,7 @@ const CheckMods = async () =>
continue
}
- if (latest.id !== cached.latest)
+ if (latest && latest.id !== cached.latest)
stillCompatibleUpdate.push(slug)
}
@@ -204,4 +159,4 @@ client.once(Events.ClientReady, async c =>
}, check_interval_minutes * 60 * 1000)
})
-client.login(token) \ No newline at end of file
+client.login(token)