refactor: move terminal message and channel ID persistence from a dedicated file to the main application configuration.
This commit is contained in:
@@ -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() {
|
||||
|
||||
@@ -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();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user