Persistenz: state.json ins Sounds-Volume verschoben (Migration vom alten Pfad). totalPlays bleibt über Deployments erhalten
This commit is contained in:
parent
f6a5be444e
commit
bf9d6d60e3
1 changed files with 21 additions and 6 deletions
|
|
@ -44,25 +44,40 @@ if (!DISCORD_TOKEN) {
|
||||||
|
|
||||||
fs.mkdirSync(SOUNDS_DIR, { recursive: true });
|
fs.mkdirSync(SOUNDS_DIR, { recursive: true });
|
||||||
|
|
||||||
// Persistente Lautstärke pro Guild speichern
|
// Persistente Lautstärke und Play-Zähler speichern
|
||||||
type PersistedState = { volumes: Record<string, number>; plays: Record<string, number>; totalPlays: number };
|
type PersistedState = { volumes: Record<string, number>; plays: Record<string, number>; totalPlays: number };
|
||||||
const STATE_FILE = path.join(path.resolve(SOUNDS_DIR, '..'), 'state.json');
|
// Neuer, persistenter Speicherort direkt im Sounds-Volume
|
||||||
|
const STATE_FILE_NEW = path.join(SOUNDS_DIR, 'state.json');
|
||||||
|
// Alter Speicherort (eine Ebene über SOUNDS_DIR). Wird für Migration gelesen, falls vorhanden.
|
||||||
|
const STATE_FILE_OLD = path.join(path.resolve(SOUNDS_DIR, '..'), 'state.json');
|
||||||
|
|
||||||
function readPersistedState(): PersistedState {
|
function readPersistedState(): PersistedState {
|
||||||
try {
|
try {
|
||||||
if (fs.existsSync(STATE_FILE)) {
|
// 1) Bevorzugt neuen Speicherort lesen
|
||||||
const raw = fs.readFileSync(STATE_FILE, 'utf8');
|
if (fs.existsSync(STATE_FILE_NEW)) {
|
||||||
|
const raw = fs.readFileSync(STATE_FILE_NEW, 'utf8');
|
||||||
const parsed = JSON.parse(raw);
|
const parsed = JSON.parse(raw);
|
||||||
return { volumes: parsed.volumes ?? {}, plays: parsed.plays ?? {}, totalPlays: parsed.totalPlays ?? 0 } as PersistedState;
|
return { volumes: parsed.volumes ?? {}, plays: parsed.plays ?? {}, totalPlays: parsed.totalPlays ?? 0 } as PersistedState;
|
||||||
}
|
}
|
||||||
|
// 2) Fallback: alten Speicherort lesen und sofort nach NEW migrieren
|
||||||
|
if (fs.existsSync(STATE_FILE_OLD)) {
|
||||||
|
const raw = fs.readFileSync(STATE_FILE_OLD, 'utf8');
|
||||||
|
const parsed = JSON.parse(raw);
|
||||||
|
const migrated: PersistedState = { volumes: parsed.volumes ?? {}, plays: parsed.plays ?? {}, totalPlays: parsed.totalPlays ?? 0 };
|
||||||
|
try {
|
||||||
|
fs.mkdirSync(path.dirname(STATE_FILE_NEW), { recursive: true });
|
||||||
|
fs.writeFileSync(STATE_FILE_NEW, JSON.stringify(migrated, null, 2), 'utf8');
|
||||||
|
} catch {}
|
||||||
|
return migrated;
|
||||||
|
}
|
||||||
} catch {}
|
} catch {}
|
||||||
return { volumes: {}, plays: {}, totalPlays: 0 };
|
return { volumes: {}, plays: {}, totalPlays: 0 };
|
||||||
}
|
}
|
||||||
|
|
||||||
function writePersistedState(state: PersistedState): void {
|
function writePersistedState(state: PersistedState): void {
|
||||||
try {
|
try {
|
||||||
fs.mkdirSync(path.dirname(STATE_FILE), { recursive: true });
|
fs.mkdirSync(path.dirname(STATE_FILE_NEW), { recursive: true });
|
||||||
fs.writeFileSync(STATE_FILE, JSON.stringify(state, null, 2), 'utf8');
|
fs.writeFileSync(STATE_FILE_NEW, JSON.stringify(state, null, 2), 'utf8');
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.warn('Persisted state konnte nicht geschrieben werden:', e);
|
console.warn('Persisted state konnte nicht geschrieben werden:', e);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue