From a4e70605384c4575f6c45e3609468c369f151a2d Mon Sep 17 00:00:00 2001 From: Dominic Ferrando Date: Sat, 15 Aug 2026 16:37:35 -0400 Subject: [PATCH] Setup elysia eden in bot --- apps/bot/package.json | 5 ++++- apps/bot/src/index.ts | 4 +++- apps/bot/src/util.ts | 38 ++++++++++++++++++++++++++++++++++++++ bun.lock | 4 ++-- 4 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 apps/bot/src/util.ts diff --git a/apps/bot/package.json b/apps/bot/package.json index d37ff3d..affa188 100644 --- a/apps/bot/package.json +++ b/apps/bot/package.json @@ -3,9 +3,12 @@ "module": "index.ts", "type": "module", "private": true, - "devDependencies": {}, + "devDependencies": { + "@blade-and-brawn/api": "workspace:*" + }, "peerDependencies": {}, "dependencies": { + "@elysia/eden": "^1.4.10", "discord.js": "^14.27.0" }, "scripts": { diff --git a/apps/bot/src/index.ts b/apps/bot/src/index.ts index 2f5c055..d258c63 100644 --- a/apps/bot/src/index.ts +++ b/apps/bot/src/index.ts @@ -1,7 +1,9 @@ import { Client, Events, GatewayIntentBits } from 'discord.js'; import { CommandService } from './services/cmd/service'; -const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMembers, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent] }); +const client = new Client({ + intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMembers, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent] +}); // SERVICES // ----------------------- diff --git a/apps/bot/src/util.ts b/apps/bot/src/util.ts new file mode 100644 index 0000000..08ed4c9 --- /dev/null +++ b/apps/bot/src/util.ts @@ -0,0 +1,38 @@ +import cluster from "node:cluster"; +import { pino } from "pino"; +import { treaty } from '@elysia/eden'; +import { type API } from "@blade-and-brawn/api"; + +export const log = pino({ + level: Bun.env.LOG_LEVEL ?? "info", + transport: Bun.env.NODE_ENV != "production" + ? { target: "pino-pretty" } + : undefined, +}); + +export const env = { + PUBLIC_API_URL: requireEnv("PUBLIC_API_URL"), + BOT_TOKEN: requireEnv("BOT_TOKEN"), +}; + +export const api = treaty(env.PUBLIC_API_URL ?? ""); + +function requireEnv(key: string): string { + const val = Bun.env[key]; + if (!val) { + if (cluster.worker?.id === 1) + log.error({ name: key }, "Missing required environment variable"); + throw new Error(`Missing required environment variable: ${key}`) + }; + return val; +} + +function optionEnv(key: string, fallback: string = ""): string { + const val = Bun.env[key]; + if (!val) { + if (cluster.worker?.id === 1) + log.warn({ name: key }, `Missing optional environmental variable, falling back to "${fallback}"`) + return fallback + }; + return val; +} diff --git a/bun.lock b/bun.lock index bf3c452..03ccbb6 100644 --- a/bun.lock +++ b/bun.lock @@ -37,10 +37,11 @@ "apps/bot": { "name": "@blade-and-brawn/bot", "dependencies": { + "@elysia/eden": "^1.4.10", "discord.js": "^14.27.0", }, "devDependencies": { - "@types/bun": "^1.3.14", + "@blade-and-brawn/api": "workspace:*", }, }, "apps/portal": { @@ -59,7 +60,6 @@ "@blade-and-brawn/api": "workspace:*", "@sveltejs/kit": "^2.70.2", "@sveltejs/vite-plugin-svelte": "^6.2.4", - "@types/bun": "^1.3.14", "elysia": "^1.4.29", "svelte": "^5.56.9", "svelte-adapter-bun": "^1.0.1",