Spaces:
Runtime error
Runtime error
Download lineup_persistence.js from Brozy123/LineUp-Bot: direct link, hf CLI and curl.
- Browser
- Download file 5.32 kB
-
https://huggingface.co/spaces/Brozy123/LineUp-Bot/resolve/main/lineup_persistence.js
- Command line
-
hf download hf://spaces/Brozy123/LineUp-Bot/lineup_persistence.js
-
curl -L -o lineup_persistence.js https://huggingface.co/spaces/Brozy123/LineUp-Bot/resolve/main/lineup_persistence.js
5.32 kB
| const fs = require('fs'); | |
| const path = require('path'); | |
| const storage = require('./storage'); | |
| /** | |
| * lineup_persistence.js | |
| * Handles persistent storage for lineup state across bot restarts. | |
| * Fixes the issue where lineups reset when the space restarts. | |
| */ | |
| const LINEUP_DATA_FILE = path.join(storage.PERSISTENT_DIR, 'lineup_persistence.json'); | |
| let guildLineups = new Map(); | |
| let guildSelectedFormationA = new Map(); | |
| let guildSelectedFormationB = new Map(); | |
| // Shared formation selection map for text-based lineup (!lineup) | |
| let guildSelectedFormation = new Map(); | |
| let lineupOwners = new Map(); | |
| let activeLineupMessages = new Map(); | |
| let managerBoards = new Map(); | |
| let guildCustomFormations = new Map(); | |
| let guildCustomFormationNames = new Map(); | |
| let guildPingType = new Map(); | |
| let guildFriendlyPingType = new Map(); | |
| let guildScrimMode = new Map(); // Track if lineup is in scrim mode | |
| let guildLineupsB = new Map(); // Team B lineup for scrim mode | |
| function save() { | |
| try { | |
| const data = { | |
| guildLineups: {}, | |
| guildSelectedFormationA: Object.fromEntries(guildSelectedFormationA), | |
| guildSelectedFormationB: Object.fromEntries(guildSelectedFormationB), | |
| // Persist the shared text-based formation map as well | |
| guildSelectedFormation: Object.fromEntries(guildSelectedFormation), | |
| lineupOwners: Object.fromEntries(lineupOwners), | |
| activeLineupMessages: Object.fromEntries(activeLineupMessages), | |
| managerBoards: Object.fromEntries(managerBoards), | |
| guildCustomFormations: Object.fromEntries(guildCustomFormations), | |
| guildCustomFormationNames: Object.fromEntries(guildCustomFormationNames), | |
| guildPingType: Object.fromEntries(guildPingType), | |
| guildFriendlyPingType: Object.fromEntries(guildFriendlyPingType), | |
| guildScrimMode: Object.fromEntries(guildScrimMode), | |
| guildLineupsB: {} | |
| }; | |
| for (const [guildId, lineupMap] of guildLineups) { | |
| data.guildLineups[guildId] = Object.fromEntries(lineupMap); | |
| } | |
| for (const [guildId, lineupMap] of guildLineupsB) { | |
| data.guildLineupsB[guildId] = Object.fromEntries(lineupMap); | |
| } | |
| fs.writeFileSync(LINEUP_DATA_FILE, JSON.stringify(data, null, 2)); | |
| // console.log('[LINEUP PERSISTENCE] State saved successfully.'); | |
| } catch (e) { | |
| console.error('[LINEUP PERSISTENCE] Save failed:', e.message); | |
| } | |
| } | |
| function load() { | |
| if (!fs.existsSync(LINEUP_DATA_FILE)) { | |
| console.log('[LINEUP PERSISTENCE] No existing persistence file found.'); | |
| return; | |
| } | |
| try { | |
| const raw = fs.readFileSync(LINEUP_DATA_FILE, 'utf8'); | |
| const data = JSON.parse(raw); | |
| if (data.guildLineups) { | |
| for (const [guildId, lineupObj] of Object.entries(data.guildLineups)) { | |
| guildLineups.set(guildId, new Map(Object.entries(lineupObj))); | |
| } | |
| } | |
| // Load shared text-based formation state if available | |
| if (data.guildSelectedFormation) { | |
| for (const [k, v] of Object.entries(data.guildSelectedFormation)) guildSelectedFormation.set(k, v); | |
| } | |
| if (data.guildSelectedFormationA) { | |
| for (const [k, v] of Object.entries(data.guildSelectedFormationA)) guildSelectedFormationA.set(k, v); | |
| } | |
| if (data.guildSelectedFormationB) { | |
| for (const [k, v] of Object.entries(data.guildSelectedFormationB)) guildSelectedFormationB.set(k, v); | |
| } | |
| if (data.lineupOwners) { | |
| for (const [k, v] of Object.entries(data.lineupOwners)) lineupOwners.set(k, v); | |
| } | |
| if (data.activeLineupMessages) { | |
| for (const [k, v] of Object.entries(data.activeLineupMessages)) activeLineupMessages.set(k, v); | |
| } | |
| if (data.managerBoards) { | |
| for (const [k, v] of Object.entries(data.managerBoards)) managerBoards.set(k, v); | |
| } | |
| if (data.guildCustomFormations) { | |
| for (const [k, v] of Object.entries(data.guildCustomFormations)) guildCustomFormations.set(k, v); | |
| } | |
| if (data.guildCustomFormationNames) { | |
| for (const [k, v] of Object.entries(data.guildCustomFormationNames)) guildCustomFormationNames.set(k, v); | |
| } | |
| if (data.guildPingType) { | |
| for (const [k, v] of Object.entries(data.guildPingType)) guildPingType.set(k, v); | |
| } | |
| if (data.guildFriendlyPingType) { | |
| for (const [k, v] of Object.entries(data.guildFriendlyPingType)) guildFriendlyPingType.set(k, v); | |
| } | |
| if (data.guildScrimMode) { | |
| for (const [k, v] of Object.entries(data.guildScrimMode)) guildScrimMode.set(k, v === "true" || v === true); | |
| } | |
| if (data.guildLineupsB) { | |
| for (const [guildId, lineupObj] of Object.entries(data.guildLineupsB)) { | |
| guildLineupsB.set(guildId, new Map(Object.entries(lineupObj))); | |
| } | |
| } | |
| console.log('[LINEUP PERSISTENCE] Data loaded successfully.'); | |
| } catch (e) { | |
| console.error('[LINEUP PERSISTENCE] Load failed:', e.message); | |
| } | |
| } | |
| // Initial load | |
| load(); | |
| module.exports = { | |
| guildLineups, guildLineupsB, guildSelectedFormationA, guildSelectedFormationB, guildSelectedFormation, lineupOwners, activeLineupMessages, managerBoards, guildCustomFormations, guildCustomFormationNames, guildPingType, guildFriendlyPingType, guildScrimMode, save | |
| }; |