aboutsummaryrefslogtreecommitdiff
path: root/174bg/discord-bot/main.js
diff options
context:
space:
mode:
authorAlex Pooley (@zuedev) <zuedev@gmail.com>2026-07-18 12:21:47 +0100
committerAlex Pooley (@zuedev) <zuedev@gmail.com>2026-07-18 12:21:47 +0100
commit75fe60c1d8014c2613f487799b2d4168493d2979 (patch)
treeac06abab20ce239f8c76d9d6f8bb5b698db52664 /174bg/discord-bot/main.js
parent91900b943def6ae9e7dab58ee4453e3874a21841 (diff)
downloadunnamed-group-75fe60c1d8014c2613f487799b2d4168493d2979.tar
unnamed-group-75fe60c1d8014c2613f487799b2d4168493d2979.tar.gz
unnamed-group-75fe60c1d8014c2613f487799b2d4168493d2979.tar.bz2
unnamed-group-75fe60c1d8014c2613f487799b2d4168493d2979.tar.xz
unnamed-group-75fe60c1d8014c2613f487799b2d4168493d2979.zip
build uex cache on start
Diffstat (limited to '174bg/discord-bot/main.js')
-rw-r--r--174bg/discord-bot/main.js51
1 files changed, 51 insertions, 0 deletions
diff --git a/174bg/discord-bot/main.js b/174bg/discord-bot/main.js
index fc4da76..02400d6 100644
--- a/174bg/discord-bot/main.js
+++ b/174bg/discord-bot/main.js
@@ -14,6 +14,8 @@ import {
import commands from "./commands/_index.js";
+import fs from "fs";
+
const client = new Client({
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMembers],
});
@@ -22,6 +24,8 @@ client.on(Events.ClientReady, async (readyClient) => {
console.log(`Logged in as ${readyClient.user.tag}!`);
await registerCommands();
+
+ await buildCache();
});
client.on(Events.InteractionCreate, async (interaction) => {
@@ -77,3 +81,50 @@ async function registerCommands() {
console.error(error);
}
}
+
+async function getUrl(url) {
+ const response = await fetch(url);
+ if (!response.ok) return null;
+ return await response.json();
+}
+
+async function buildCache() {
+ if (!fs.existsSync("./.cache")) fs.mkdirSync("./.cache");
+
+ const uex = {};
+
+ uex.categories = (await getUrl("https://api.uexcorp.uk/2.0/categories"))?.data;
+
+ for (const category of uex.categories) {
+ if (category.type === "item") {
+ if (!uex.items) uex.items = {};
+ if (!uex.items[category.section]) uex.items[category.section] = {};
+ uex.items[category.section][category.name] = (await getUrl(`https://api.uexcorp.uk/2.0/items?id_category=${category.id}`))?.data;
+ }
+ }
+
+ // is there a difference between the current cache and the new cache?
+ if (fs.existsSync("./.cache/uex.json")) {
+ const currentCache = JSON.parse(fs.readFileSync("./.cache/uex.json", "utf-8"));
+
+ if (JSON.stringify(currentCache) === JSON.stringify(uex)) {
+ console.log("No changes detected in UEX cache.");
+ return;
+ }
+
+ console.log("Changes detected in UEX cache. Updating cache.");
+
+ // backup the current cache YYMMDDHHMMSS
+ fs.writeFileSync(
+ `./.cache/uex.json.bak.${new Date().toISOString().replace(/[-:]/g, "").replace(/\..+/, "")}`,
+ JSON.stringify(currentCache, null, 2),
+ );
+ } else {
+ console.log("No existing UEX cache found. Creating new cache.");
+ }
+
+ fs.writeFileSync(
+ "./.cache/uex.json",
+ JSON.stringify(uex, null, 2),
+ );
+} \ No newline at end of file