mirror of
https://github.com/ZHANGTIANYAO1/teamspeak-music-bot.git
synced 2026-12-02 19:02:50 +08:00
feat: persist volume, play mode and audio quality across restarts
Runtime playback settings were kept only in memory (AudioPlayer.volume, PlayQueue.mode, each provider's quality field), so restarting the bot reset them to defaults and users had to re-tune volume and quality every time (#125). Persist and restore them via the repo's existing storage: - Volume and play mode are per-bot, stored on new bot_instances columns (volume, play_mode) with a schema migration; restored when the instance is (re)built, written by cmdVol / cmdMode which every entry point (chat command, WebUI, REST) funnels through. Volume and mode are written independently so a transient !fm/!artist mode switch never overwrites the user's saved !mode. - Per-provider audio quality is global (shared providers), stored in a new config.json `audioQuality` block; applied to the providers at startup and re-snapshotted on POST /api/music/quality. Queue, current song, progress and FM/artist sessions stay ephemeral. Adds tests for config sanitize/round-trip, DB player-settings + migration, cmdVol/cmdMode persistence + construction-time restore, and quality persistence through the REST endpoint. Documents the behavior in the README. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
051171b019
commit
fbd94c424b
11 files changed
+618
-12
No files matched your search
+52
-7
@@ -39,6 +39,15 @@ import type { SpotifyOAuth } from "../music/spotify/spotify-oauth.js";
|
||||
/** Reply sent when a non-admin invokes an admin-only chat command. */
|
||||
export const COMMAND_DENIED_MESSAGE = "⛔ 需要管理员权限(该命令仅限管理员服务器组)";
|
||||
|
||||
/** Maps the persisted / command-line play-mode string to the PlayMode enum.
|
||||
* Shared by the !mode command and the restart-restore path (#125). */
|
||||
const PLAY_MODE_BY_VALUE: Record<string, PlayMode> = {
|
||||
seq: PlayMode.Sequential,
|
||||
loop: PlayMode.Loop,
|
||||
random: PlayMode.Random,
|
||||
rloop: PlayMode.RandomLoop,
|
||||
};
|
||||
|
||||
/** Fallback message when Spotify audio can't be served (backend unavailable
|
||||
* OR a per-track playTrack failure against a dead/failed sidecar). */
|
||||
const SPOTIFY_UNAVAILABLE_MESSAGE =
|
||||
@@ -188,6 +197,19 @@ export class BotInstance extends EventEmitter {
|
||||
this.player = new AudioPlayer(this.logger);
|
||||
this.queue = new PlayQueue();
|
||||
|
||||
// Restore persisted per-bot player settings (#125): volume + play mode
|
||||
// survive restarts. getPlayerSettings returns validated values (the in-memory
|
||||
// defaults when the row/column is absent), so this is a harmless no-op for a
|
||||
// brand-new bot and reproduces the saved state for an existing one.
|
||||
try {
|
||||
const settings = this.database.getPlayerSettings(this.id);
|
||||
this.player.setVolume(settings.volume);
|
||||
const restoredMode = PLAY_MODE_BY_VALUE[settings.playMode];
|
||||
if (restoredMode) this.queue.setMode(restoredMode);
|
||||
} catch (err) {
|
||||
this.logger.warn({ err }, "Failed to restore player settings — using defaults");
|
||||
}
|
||||
|
||||
// Structural typing (like localProvider.sweepUnreferenced): only the real
|
||||
// JellyfinProvider exposes createPlaybackReporter, so the netease fallback
|
||||
// provider simply leaves reporting off.
|
||||
@@ -1136,10 +1158,36 @@ export class BotInstance extends EventEmitter {
|
||||
const vol = parseInt(cmd.args, 10);
|
||||
if (isNaN(vol) || vol < 0 || vol > 100) return "Usage: !vol <0-100>";
|
||||
this.player.setVolume(vol);
|
||||
// Persist so the volume survives a restart (#125). Both the chat !vol command
|
||||
// and the WebUI/REST volume endpoint funnel through here, so one write covers
|
||||
// every entry point. Only volume is written — play mode is saved independently.
|
||||
this.persistVolume();
|
||||
this.emit("stateChange");
|
||||
return `Volume set to ${vol}%`;
|
||||
}
|
||||
|
||||
/** Persist the current volume (#125). Best-effort: a DB error must never break
|
||||
* the volume change itself. */
|
||||
private persistVolume(): void {
|
||||
try {
|
||||
this.database.saveVolume(this.id, this.player.getVolume());
|
||||
} catch (err) {
|
||||
this.logger.warn({ err }, "Failed to persist volume");
|
||||
}
|
||||
}
|
||||
|
||||
/** Persist the current play mode (#125). Best-effort, mirrors persistVolume.
|
||||
* Called ONLY from the explicit !mode command — NOT from FM/artist mode, whose
|
||||
* Random/Loop switch is a transient side effect that must not overwrite the
|
||||
* user's saved preference. */
|
||||
private persistPlayMode(): void {
|
||||
try {
|
||||
this.database.savePlayMode(this.id, this.queue.getMode());
|
||||
} catch (err) {
|
||||
this.logger.warn({ err }, "Failed to persist play mode");
|
||||
}
|
||||
}
|
||||
|
||||
private cmdNow(): string {
|
||||
const song = this.queue.current();
|
||||
if (!song) return "Nothing is playing";
|
||||
@@ -1205,15 +1253,12 @@ export class BotInstance extends EventEmitter {
|
||||
}
|
||||
|
||||
private cmdMode(cmd: ParsedCommand): string {
|
||||
const modeMap: Record<string, PlayMode> = {
|
||||
seq: PlayMode.Sequential,
|
||||
loop: PlayMode.Loop,
|
||||
random: PlayMode.Random,
|
||||
rloop: PlayMode.RandomLoop,
|
||||
};
|
||||
const mode = modeMap[cmd.args];
|
||||
const mode = PLAY_MODE_BY_VALUE[cmd.args];
|
||||
if (mode === undefined) return "Usage: !mode <seq|loop|random|rloop>";
|
||||
this.queue.setMode(mode);
|
||||
// Persist so the play mode survives a restart (#125). The chat !mode command
|
||||
// and the WebUI/REST mode endpoint both funnel through here.
|
||||
this.persistPlayMode();
|
||||
this.emit("stateChange");
|
||||
return `Play mode set to: ${cmd.args}`;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user