fix(server): remove pre-declaration use of 'state' when computing default volume

This commit is contained in:
vibe-bot 2025-08-08 01:43:39 +02:00
parent 9a97a9d7bb
commit 8b0b7c75cd

View file

@ -230,7 +230,6 @@ app.post('/api/play', async (req: Request, res: Response) => {
volume?: number; // 0..1 volume?: number; // 0..1
}; };
if (!soundName || !guildId || !channelId) return res.status(400).json({ error: 'soundName, guildId, channelId erforderlich' }); if (!soundName || !guildId || !channelId) return res.status(400).json({ error: 'soundName, guildId, channelId erforderlich' });
const safeVolume = typeof volume === 'number' && Number.isFinite(volume) ? Math.max(0, Math.min(1, volume)) : state?.currentVolume ?? 1;
const filePath = path.join(SOUNDS_DIR, `${soundName}.mp3`); const filePath = path.join(SOUNDS_DIR, `${soundName}.mp3`);
if (!fs.existsSync(filePath)) return res.status(404).json({ error: 'Sound nicht gefunden' }); if (!fs.existsSync(filePath)) return res.status(404).json({ error: 'Sound nicht gefunden' });
@ -319,15 +318,19 @@ app.post('/api/play', async (req: Request, res: Response) => {
} }
console.log(`${new Date().toISOString()} | createAudioResource: ${filePath}`); console.log(`${new Date().toISOString()} | createAudioResource: ${filePath}`);
// Volume bestimmen: bevorzugt Request-Volume, sonst bisheriger State-Wert, sonst 1
const volumeToUse = typeof volume === 'number' && Number.isFinite(volume)
? Math.max(0, Math.min(1, volume))
: (state.currentVolume ?? 1);
const resource = createAudioResource(filePath, { inlineVolume: true }); const resource = createAudioResource(filePath, { inlineVolume: true });
if (resource.volume) { if (resource.volume) {
resource.volume.setVolume(safeVolume); resource.volume.setVolume(volumeToUse);
console.log(`${new Date().toISOString()} | setVolume(${safeVolume}) for ${soundName}`); console.log(`${new Date().toISOString()} | setVolume(${volumeToUse}) for ${soundName}`);
} }
state.player.stop(); state.player.stop();
state.player.play(resource); state.player.play(resource);
state.currentResource = resource; state.currentResource = resource;
state.currentVolume = safeVolume; state.currentVolume = volumeToUse;
console.log(`${new Date().toISOString()} | player.play() called for ${soundName}`); console.log(`${new Date().toISOString()} | player.play() called for ${soundName}`);
return res.json({ ok: true }); return res.json({ ok: true });
} catch (err: any) { } catch (err: any) {