Spaces:
Runtime error
Runtime error
Download storage.js from Brozy123/LineUp-Bot: direct link, hf CLI and curl.
- Browser
- Download file 6.33 kB
-
https://huggingface.co/spaces/Brozy123/LineUp-Bot/resolve/main/storage.js
- Command line
-
hf download hf://spaces/Brozy123/LineUp-Bot/storage.js
-
curl -L -o storage.js https://huggingface.co/spaces/Brozy123/LineUp-Bot/resolve/main/storage.js
6.33 kB
| const fs = require('fs'); | |
| const path = require('path'); | |
| console.log(">>> [MODULE] storage.js: Initializing..."); | |
| // --- File Paths --- | |
| // --- Persistence Configuration (Hugging Face Buckets) --- | |
| // The bucket should be mounted at /home/node/app/data (as seen in Space settings) | |
| const PERSISTENT_DIR = path.isAbsolute(process.env.DATA_PATH || "") | |
| ? process.env.DATA_PATH | |
| : path.join(__dirname, 'data'); | |
| console.log(`[STORAGE] Using persistent directory: ${PERSISTENT_DIR}`); | |
| if (!fs.existsSync(PERSISTENT_DIR)) { | |
| console.log(`[STORAGE] Creating persistent directory...`); | |
| fs.mkdirSync(PERSISTENT_DIR, { recursive: true }); | |
| } | |
| const GUILD_SETTINGS_FILE = path.join(PERSISTENT_DIR, "guild_settings.json"); | |
| const CONFIG_FILE = path.join(PERSISTENT_DIR, "lineup_access.json"); | |
| const PLAYER_STATS_FILE = path.join(PERSISTENT_DIR, "player_stats.json"); | |
| const STATE_FILE = path.join(PERSISTENT_DIR, "state.json"); | |
| const BROADCAST_FILE = path.join(PERSISTENT_DIR, "broadcast_history.json"); | |
| function loadJson(path, defaultValue = {}) { | |
| if (fs.existsSync(path)) { | |
| try { | |
| const data = fs.readFileSync(path, 'utf8'); | |
| return JSON.parse(data); | |
| } catch (e) { | |
| console.warn(`[STORAGE] Failed to load ${path}:`, e.message); | |
| } | |
| } | |
| return defaultValue; | |
| } | |
| function saveJson(path, data) { | |
| try { | |
| fs.writeFileSync(path, JSON.stringify(data, null, 2)); | |
| } catch (e) { | |
| console.error(`[STORAGE] Failed to save ${path}:`, e.message); | |
| } | |
| } | |
| class StorageManager { | |
| constructor() { | |
| this.data = { | |
| config: {}, // Guild settings | |
| roles: {}, // Lineup roles | |
| stats: {}, // Player stats | |
| active_polls: {}, // Active poll messages | |
| processed_messages: new Set(), // Processed message IDs | |
| settings: {}, // Global bot settings | |
| broadcasts: [], // Broadcast history | |
| scheduled_matches: [], // Scheduled league matches | |
| cleanup_queue: [], // Messages to be deleted | |
| onboarded: {}, // GuildId -> Set of UserIds who got the welcome DM | |
| dmall_sessions: {}, // SessionId -> Session Data | |
| muted_users: {} // UserId -> true (users who mute friendly DMs) | |
| }; | |
| this.loadAll(); | |
| } | |
| loadAll() { | |
| // Load Guild Settings | |
| const guildSettings = loadJson(GUILD_SETTINGS_FILE, {}); | |
| this.data.config = guildSettings; | |
| // Load Roles | |
| const roles = loadJson(CONFIG_FILE, {}); | |
| this.data.roles = Object.fromEntries( | |
| Object.entries(roles).map(([k, v]) => [k, new Set(v)]) | |
| ); | |
| // Load Stats | |
| const stats = loadJson(PLAYER_STATS_FILE, {}); | |
| this.data.stats = stats; | |
| // Load State | |
| const state = loadJson(STATE_FILE, {}); | |
| this.data.active_polls = state.polls || {}; | |
| this.data.processed_messages = new Set(state.processed || []); | |
| this.data.settings = state.settings || {}; | |
| // Load Broadcasts | |
| this.data.broadcasts = loadJson(BROADCAST_FILE, []); | |
| // Load Reminders & Cleanup | |
| this.data.scheduled_matches = state.scheduled_matches || []; | |
| this.data.cleanup_queue = state.cleanup_queue || []; | |
| this.data.onboarded = state.onboarded || {}; | |
| this.data.dmall_sessions = state.dmall_sessions || {}; | |
| this.data.muted_users = state.muted_users || {}; | |
| console.log('[STORAGE] Data loaded successfully'); | |
| } | |
| save(type) { | |
| switch (type) { | |
| case 'config': | |
| saveJson(GUILD_SETTINGS_FILE, this.data.config); | |
| break; | |
| case 'roles': | |
| saveJson(CONFIG_FILE, Object.fromEntries( | |
| Object.entries(this.data.roles).map(([k, v]) => [k, Array.from(v)]) | |
| )); | |
| break; | |
| case 'stats': | |
| saveJson(PLAYER_STATS_FILE, this.data.stats); | |
| break; | |
| case 'active_polls': | |
| this.saveState(); | |
| break; | |
| case 'settings': | |
| this.saveState(); | |
| break; | |
| case 'broadcasts': | |
| saveJson(BROADCAST_FILE, this.data.broadcasts); | |
| break; | |
| default: | |
| console.warn(`[STORAGE] Unknown save type: ${type}`); | |
| } | |
| } | |
| saveState() { | |
| saveJson(STATE_FILE, { | |
| polls: this.data.active_polls, | |
| processed: Array.from(this.data.processed_messages), | |
| settings: this.data.settings, | |
| scheduled_matches: this.data.scheduled_matches, | |
| cleanup_queue: this.data.cleanup_queue, | |
| onboarded: this.data.onboarded, | |
| dmall_sessions: this.data.dmall_sessions, | |
| muted_users: this.data.muted_users | |
| }); | |
| } | |
| saveAll() { | |
| this.save('config'); | |
| this.save('roles'); | |
| this.save('stats'); | |
| this.save('broadcasts'); | |
| this.saveState(); | |
| } | |
| getPrefix(guildId) { | |
| return this.data.config[guildId]?.prefix || '!'; | |
| } | |
| setPrefix(guildId, prefix) { | |
| if (!this.data.config[guildId]) this.data.config[guildId] = {}; | |
| this.data.config[guildId].prefix = prefix; | |
| this.save('config'); | |
| } | |
| getRecord(guildId) { | |
| if (!this.data.config[guildId]) this.data.config[guildId] = {}; | |
| if (!this.data.config[guildId].record) { | |
| this.data.config[guildId].record = { wins: 0, losses: 0, draws: 0 }; | |
| } | |
| return this.data.config[guildId].record; | |
| } | |
| addStat(userId, statType, amount = 1) { | |
| if (!this.data.stats[userId]) { | |
| this.data.stats[userId] = { | |
| matches: 0, | |
| wins: 0, | |
| losses: 0, | |
| draws: 0, | |
| goals: 0, | |
| assists: 0, | |
| mvps: 0 | |
| }; | |
| } | |
| this.data.stats[userId][statType] = (this.data.stats[userId][statType] || 0) + amount; | |
| this.save('stats'); | |
| } | |
| } | |
| const storage = new StorageManager(); | |
| storage.PERSISTENT_DIR = PERSISTENT_DIR; | |
| console.log(">>> [MODULE] storage.js: Fully initialized."); | |
| module.exports = storage; |