From cddd8cdf5744f00aad8413f1c5d0c461b68ee91a Mon Sep 17 00:00:00 2001 From: syntaxbullet Date: Wed, 24 Dec 2025 19:57:00 +0100 Subject: [PATCH] refactor: move terminal message and channel ID persistence from a dedicated file to the main application configuration. --- src/lib/config.ts | 10 +++- src/modules/terminal/terminal.service.ts | 59 ++++++++---------------- 2 files changed, 28 insertions(+), 41 deletions(-) diff --git a/src/lib/config.ts b/src/lib/config.ts index 7d8508e..c6b28b8 100644 --- a/src/lib/config.ts +++ b/src/lib/config.ts @@ -51,6 +51,10 @@ export interface GameConfigType { colorRoles: string[]; welcomeChannelId?: string; welcomeMessage?: string; + terminal?: { + channelId: string; + messageId: string; + }; } // Initial default config state @@ -114,7 +118,11 @@ const configSchema = z.object({ visitorRole: z.string(), colorRoles: z.array(z.string()).default([]), welcomeChannelId: z.string().optional(), - welcomeMessage: z.string().optional() + welcomeMessage: z.string().optional(), + terminal: z.object({ + channelId: z.string(), + messageId: z.string() + }).optional() }); export function reloadConfig() { diff --git a/src/modules/terminal/terminal.service.ts b/src/modules/terminal/terminal.service.ts index 01a499a..79a314f 100644 --- a/src/modules/terminal/terminal.service.ts +++ b/src/modules/terminal/terminal.service.ts @@ -3,42 +3,16 @@ import { AuroraClient } from "@/lib/BotClient"; import { DrizzleClient } from "@/lib/DrizzleClient"; import { users, transactions, lootdrops } from "@/db/schema"; import { desc } from "drizzle-orm"; -import { readFileSync, writeFileSync, existsSync } from "fs"; -import { join } from "path"; - -// Simple persistence for the terminal message ID -const TERMINAL_DATA_PATH = join(process.cwd(), 'terminal-data.json'); - -interface TerminalData { - channelId: string; - messageId: string; -} +import { config, saveConfig } from "@/lib/config"; export const terminalService = { - data: null as TerminalData | null, - - loadData: () => { - if (existsSync(TERMINAL_DATA_PATH)) { - try { - terminalService.data = JSON.parse(readFileSync(TERMINAL_DATA_PATH, 'utf-8')); - } catch (e) { - console.error("Failed to load terminal data", e); - } - } - }, - - saveData: (data: TerminalData) => { - terminalService.data = data; - writeFileSync(TERMINAL_DATA_PATH, JSON.stringify(data, null, 2)); - }, - init: async (channel: TextChannel) => { // limit to one terminal for now - if (terminalService.data) { + if (config.terminal) { try { - const oldChannel = await AuroraClient.channels.fetch(terminalService.data.channelId) as TextChannel; + const oldChannel = await AuroraClient.channels.fetch(config.terminal.channelId) as TextChannel; if (oldChannel) { - const oldMsg = await oldChannel.messages.fetch(terminalService.data.messageId); + const oldMsg = await oldChannel.messages.fetch(config.terminal.messageId); if (oldMsg) await oldMsg.delete(); } } catch (e) { @@ -47,25 +21,30 @@ export const terminalService = { } const msg = await channel.send({ content: "🔄 Initializing Aurora Observatory..." }); - terminalService.saveData({ + + config.terminal = { channelId: channel.id, messageId: msg.id - }); + }; + saveConfig(config); await terminalService.update(); }, update: async () => { - if (!terminalService.data) { - terminalService.loadData(); - } - if (!terminalService.data) return; + if (!config.terminal) return; try { - const channel = await AuroraClient.channels.fetch(terminalService.data.channelId) as TextChannel; - if (!channel) return; - const message = await channel.messages.fetch(terminalService.data.messageId); - if (!message) return; + const channel = await AuroraClient.channels.fetch(config.terminal.channelId).catch(() => null) as TextChannel; + if (!channel) { + console.warn("Terminal channel not found"); + return; + } + const message = await channel.messages.fetch(config.terminal.messageId).catch(() => null); + if (!message) { + console.warn("Terminal message not found"); + return; + } const containers = await terminalService.buildMessage();