diff options
| author | Alex Pooley (@zuedev) <zuedev@gmail.com> | 2026-07-18 13:35:21 +0100 |
|---|---|---|
| committer | Alex Pooley (@zuedev) <zuedev@gmail.com> | 2026-07-18 13:35:21 +0100 |
| commit | 33fc86d95c7339525d46d0707b45d303a664f49f (patch) | |
| tree | b1d543a71f1cc700db327bd4e105dfb145388bc7 /174bg/discord-bot/main.js | |
| parent | 75fe60c1d8014c2613f487799b2d4168493d2979 (diff) | |
| download | unnamed-group-33fc86d95c7339525d46d0707b45d303a664f49f.tar unnamed-group-33fc86d95c7339525d46d0707b45d303a664f49f.tar.gz unnamed-group-33fc86d95c7339525d46d0707b45d303a664f49f.tar.bz2 unnamed-group-33fc86d95c7339525d46d0707b45d303a664f49f.tar.xz unnamed-group-33fc86d95c7339525d46d0707b45d303a664f49f.zip | |
populate uex_items
Diffstat (limited to '174bg/discord-bot/main.js')
| -rw-r--r-- | 174bg/discord-bot/main.js | 36 |
1 files changed, 23 insertions, 13 deletions
diff --git a/174bg/discord-bot/main.js b/174bg/discord-bot/main.js index 02400d6..8a83bd9 100644 --- a/174bg/discord-bot/main.js +++ b/174bg/discord-bot/main.js @@ -1,3 +1,4 @@ +import fs from "fs"; import { Client, Events, @@ -11,11 +12,9 @@ import { ButtonBuilder, ButtonStyle, } from "discord.js"; - +import getUrl from "./utilities/getUrl.js"; import commands from "./commands/_index.js"; -import fs from "fs"; - const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMembers], }); @@ -82,12 +81,6 @@ async function registerCommands() { } } -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"); @@ -96,19 +89,32 @@ async function buildCache() { uex.categories = (await getUrl("https://api.uexcorp.uk/2.0/categories"))?.data; for (const category of uex.categories) { + uex.items = uex.items || []; 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; + uex.items = uex.items.concat((await getUrl(`https://api.uexcorp.uk/2.0/items?id_category=${category.id}`))?.data || []); } } + uex.space_stations = (await getUrl("https://api.uexcorp.uk/2.0/space_stations"))?.data; + uex.cities = (await getUrl("https://api.uexcorp.uk/2.0/cities"))?.data; + uex.outposts = (await getUrl("https://api.uexcorp.uk/2.0/outposts"))?.data; + uex.points_of_interest = (await getUrl("https://api.uexcorp.uk/2.0/poi"))?.data; + uex.terminals = (await getUrl("https://api.uexcorp.uk/2.0/terminals"))?.data; + uex.vehicles = (await getUrl("https://api.uexcorp.uk/2.0/vehicles"))?.data; + uex.vehicles_pledge_prices = (await getUrl("https://api.uexcorp.uk/2.0/vehicles_prices"))?.data; + uex.vehicles_buy_prices = (await getUrl("https://api.uexcorp.uk/2.0/vehicles_purchases_prices"))?.data; + uex.vehicles_rent_prices = (await getUrl("https://api.uexcorp.uk/2.0/vehicles_rentals_prices"))?.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."); + console.log("If you want to force a cache update, delete the .cache/uex.json file and restart the bot."); + for (const [key, value] of Object.entries(uex)) { + console.log(`Found ${value.length} ${key.replace(/_/g, " ")}.`); + } return; } @@ -123,8 +129,12 @@ async function buildCache() { console.log("No existing UEX cache found. Creating new cache."); } - fs.writeFileSync( + await fs.writeFileSync( "./.cache/uex.json", JSON.stringify(uex, null, 2), ); + + for (const [key, value] of Object.entries(uex)) { + console.log(`Cached ${value.length} ${key.replace(/_/g, " ")}.`); + } }
\ No newline at end of file |
