aboutsummaryrefslogtreecommitdiff
path: root/174bg/discord-bot/source/controllers
diff options
context:
space:
mode:
authorAlex Pooley (@zuedev) <zuedev@gmail.com>2026-07-22 15:32:57 +0100
committerAlex Pooley (@zuedev) <zuedev@gmail.com>2026-07-22 15:32:57 +0100
commit72487019b3f255cece40a8073bbad8bc9968a3b7 (patch)
treeb6590e63039cdc8b1820bd345cdfe83f28ff839b /174bg/discord-bot/source/controllers
parentda0509c91376f56f133872eddd2e4282d76f215e (diff)
downloadunnamed-group-72487019b3f255cece40a8073bbad8bc9968a3b7.tar
unnamed-group-72487019b3f255cece40a8073bbad8bc9968a3b7.tar.gz
unnamed-group-72487019b3f255cece40a8073bbad8bc9968a3b7.tar.bz2
unnamed-group-72487019b3f255cece40a8073bbad8bc9968a3b7.tar.xz
unnamed-group-72487019b3f255cece40a8073bbad8bc9968a3b7.zip
major code refactor, add uex lookup
Diffstat (limited to '174bg/discord-bot/source/controllers')
-rw-r--r--174bg/discord-bot/source/controllers/http.js25
-rw-r--r--174bg/discord-bot/source/controllers/pocketbase.js40
-rw-r--r--174bg/discord-bot/source/controllers/uex.js29
3 files changed, 94 insertions, 0 deletions
diff --git a/174bg/discord-bot/source/controllers/http.js b/174bg/discord-bot/source/controllers/http.js
new file mode 100644
index 0000000..1472b18
--- /dev/null
+++ b/174bg/discord-bot/source/controllers/http.js
@@ -0,0 +1,25 @@
+/**
+ * Controller module for interacting with HTTP resources, mainly for fetching JSON data from external APIs.
+ */
+
+/**
+ * Fetches data from a specified URL and returns the response as JSON or text.
+ * @param {string} url - The URL to fetch data from.
+ * @returns {Promise<Object|string|null>} A Promise that resolves to the parsed JSON object, text, or null if the request fails.
+ */
+export async function get(url) {
+ const response = await fetch(url);
+
+ if (!response.ok) return null;
+
+ // is it json?
+ const contentType = response.headers.get("content-type");
+ if (contentType && contentType.includes("application/json")) {
+ return await response.json();
+ }
+
+ // fallback to text
+ return await response.text();
+};
+
+export default { get }; \ No newline at end of file
diff --git a/174bg/discord-bot/source/controllers/pocketbase.js b/174bg/discord-bot/source/controllers/pocketbase.js
new file mode 100644
index 0000000..4046391
--- /dev/null
+++ b/174bg/discord-bot/source/controllers/pocketbase.js
@@ -0,0 +1,40 @@
+/**
+ * Controller module for interacting with the PocketBase API.
+ */
+
+import PocketBase from "pocketbase";
+
+const { POCKETBASE_URL, POCKETBASE_SUPERUSER_EMAIL, POCKETBASE_SUPERUSER_PASSWORD } = process.env;
+
+/**
+ * Logs in to the PocketBase API using the superuser credentials.
+ * @returns {Promise<PocketBase>} A Promise that resolves to the authenticated PocketBase instance.
+ */
+export async function login() {
+ const pb = new PocketBase(POCKETBASE_URL);
+ await pb.collection("_superusers").authWithPassword(POCKETBASE_SUPERUSER_EMAIL, POCKETBASE_SUPERUSER_PASSWORD);
+ return pb;
+}
+
+/**
+ * Logs out of the PocketBase API by clearing the authentication store.
+ * @param {PocketBase} pb - The PocketBase instance to log out from.
+ * @returns {Promise<void>} A Promise that resolves when the logout is complete.
+ */
+export async function logout(pb) {
+ await pb.authStore.clear();
+}
+
+/**
+ * Retrieves the full list of records from a specified collection in the PocketBase API.
+ * @param {string} collectionName - The name of the collection to retrieve records from.
+ * @returns {Promise<RecordModel[]>} A Promise that resolves to an array of records from the specified collection.
+ */
+export async function getFullList(collectionName) {
+ const pb = await login();
+ const data = await pb.collection(collectionName).getFullList();
+ await logout(pb);
+ return data;
+}
+
+export default { login, logout, getFullList } \ No newline at end of file
diff --git a/174bg/discord-bot/source/controllers/uex.js b/174bg/discord-bot/source/controllers/uex.js
new file mode 100644
index 0000000..615b517
--- /dev/null
+++ b/174bg/discord-bot/source/controllers/uex.js
@@ -0,0 +1,29 @@
+import { get } from "./http.js";
+
+export async function categories() {
+ return await get("https://api.uexcorp.uk/2.0/categories");
+}
+
+export async function items(id_category) {
+ let items = [];
+
+ if (!id_category) {
+ const { data: categories } = await get("https://api.uexcorp.uk/2.0/categories");
+
+ for (const category of categories) {
+ const { data: categoryItems } = await get(`https://api.uexcorp.uk/2.0/items?id_category=${category.id}`);
+
+ if (!categoryItems) continue;
+
+ items = items.concat(categoryItems);
+ }
+ } else {
+ const { data } = await get(`https://api.uexcorp.uk/2.0/items?category=${id_category}`);
+
+ items = data;
+ }
+
+ return items;
+}
+
+export default { categories, items }; \ No newline at end of file