forked from syntaxbullet/AuroraBot-discord
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[];
|
colorRoles: string[];
|
||||||
welcomeChannelId?: string;
|
welcomeChannelId?: string;
|
||||||
welcomeMessage?: string;
|
welcomeMessage?: string;
|
||||||
|
terminal?: {
|
||||||
|
channelId: string;
|
||||||
|
messageId: string;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initial default config state
|
// Initial default config state
|
||||||
@@ -114,7 +118,11 @@ const configSchema = z.object({
|
|||||||
visitorRole: z.string(),
|
visitorRole: z.string(),
|
||||||
colorRoles: z.array(z.string()).default([]),
|
colorRoles: z.array(z.string()).default([]),
|
||||||
welcomeChannelId: z.string().optional(),
|
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() {
|
export function reloadConfig() {
|
||||||
|
|||||||
@@ -3,42 +3,16 @@ import { AuroraClient } from "@/lib/BotClient";
|
|||||||
import { DrizzleClient } from "@/lib/DrizzleClient";
|
import { DrizzleClient } from "@/lib/DrizzleClient";
|
||||||
import { users, transactions, lootdrops } from "@/db/schema";
|
import { users, transactions, lootdrops } from "@/db/schema";
|
||||||
import { desc } from "drizzle-orm";
|
import { desc } from "drizzle-orm";
|
||||||
import { readFileSync, writeFileSync, existsSync } from "fs";
|
import { config, saveConfig } from "@/lib/config";
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
export const terminalService = {
|
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) => {
|
init: async (channel: TextChannel) => {
|
||||||
// limit to one terminal for now
|
// limit to one terminal for now
|
||||||
if (terminalService.data) {
|
if (config.terminal) {
|
||||||
try {
|
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) {
|
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();
|
if (oldMsg) await oldMsg.delete();
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
@@ -47,25 +21,30 @@ export const terminalService = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const msg = await channel.send({ content: "🔄 Initializing Aurora Observatory..." });
|
const msg = await channel.send({ content: "🔄 Initializing Aurora Observatory..." });
|
||||||
terminalService.saveData({
|
|
||||||
|
config.terminal = {
|
||||||
channelId: channel.id,
|
channelId: channel.id,
|
||||||
messageId: msg.id
|
messageId: msg.id
|
||||||
});
|
};
|
||||||
|
saveConfig(config);
|
||||||
|
|
||||||
await terminalService.update();
|
await terminalService.update();
|
||||||
},
|
},
|
||||||
|
|
||||||
update: async () => {
|
update: async () => {
|
||||||
if (!terminalService.data) {
|
if (!config.terminal) return;
|
||||||
terminalService.loadData();
|
|
||||||
}
|
|
||||||
if (!terminalService.data) return;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const channel = await AuroraClient.channels.fetch(terminalService.data.channelId) as TextChannel;
|
const channel = await AuroraClient.channels.fetch(config.terminal.channelId).catch(() => null) as TextChannel;
|
||||||
if (!channel) return;
|
if (!channel) {
|
||||||
const message = await channel.messages.fetch(terminalService.data.messageId);
|
console.warn("Terminal channel not found");
|
||||||
if (!message) return;
|
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();
|
const containers = await terminalService.buildMessage();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user