Save announcer voice across restarts

This commit is contained in:
2026-05-14 21:26:10 +02:00
parent ffb229b513
commit f56e6079e6
2 changed files with 26 additions and 3 deletions

View File

@@ -8,6 +8,7 @@ process.on("uncaughtException", (err) => {
console.error("Uncaught exception:", err); console.error("Uncaught exception:", err);
}); });
import { openDatabase } from "./db/db.js"; import { openDatabase } from "./db/db.js";
import type { BotStateRow } from "./db/schema.js";
import { loadStrings, makeT } from "./i18n/strings.js"; import { loadStrings, makeT } from "./i18n/strings.js";
import { TTSRegistry } from "./tts/registry.js"; import { TTSRegistry } from "./tts/registry.js";
import { AudioService } from "./audio/AudioService.js"; import { AudioService } from "./audio/AudioService.js";
@@ -31,7 +32,16 @@ client.on(Events.Error, (err) => console.error("Discord client error:", err));
client.on(Events.Warn, (msg) => console.warn("Discord client warning:", msg)); client.on(Events.Warn, (msg) => console.warn("Discord client warning:", msg));
const db = await openDatabase(config.DB_FILE); const db = await openDatabase(config.DB_FILE);
const tts = new TTSRegistry(config.ANNOUNCEMENT_ENGINE, config.ANNOUNCEMENT_VOICE); const savedEngine = await db.get<BotStateRow>(
"select value from BotState where key='announcement_engine'",
);
const savedVoice = await db.get<BotStateRow>(
"select value from BotState where key='announcement_voice'",
);
const tts = new TTSRegistry(
savedEngine?.value ?? config.ANNOUNCEMENT_ENGINE,
savedVoice?.value ?? config.ANNOUNCEMENT_VOICE,
);
const audio = new AudioService({ voiceTmpPath: config.VOICE_TMP_PATH, tts }); const audio = new AudioService({ voiceTmpPath: config.VOICE_TMP_PATH, tts });
const commands = new CommandRegistry(config.PREFIX); const commands = new CommandRegistry(config.PREFIX);
const strings = loadStrings(config.STRING_SET); const strings = loadStrings(config.STRING_SET);

View File

@@ -4,10 +4,21 @@ import { respond } from "../audio/AudioService.js";
import { formatCandidates } from "../tts/BaseEngine.js"; import { formatCandidates } from "../tts/BaseEngine.js";
import type { Module } from "./types.js"; import type { Module } from "./types.js";
export const ttsSettings: Module = ({ audio, commands, tts, t, rootDir }) => { export const ttsSettings: Module = ({ audio, commands, tts, db, t, rootDir }) => {
const sysmsg = join(rootDir, "sysmsg.wav"); const sysmsg = join(rootDir, "sysmsg.wav");
commands.register("announcevoice", (args, message) => { const persistAnnouncement = async () => {
await db.run("insert or replace into BotState (key, value) values (?, ?)", [
"announcement_engine",
tts.announcement.shortName,
]);
await db.run("insert or replace into BotState (key, value) values (?, ?)", [
"announcement_voice",
tts.announcementVoice,
]);
};
commands.register("announcevoice", async (args, message) => {
const engineName = args[1]; const engineName = args[1];
if (!engineName || args.length < 3) { if (!engineName || args.length < 3) {
respond(audio, sysmsg, message, t("TOO_MANY_ARGUMENTS")); respond(audio, sysmsg, message, t("TOO_MANY_ARGUMENTS"));
@@ -23,6 +34,7 @@ export const ttsSettings: Module = ({ audio, commands, tts, t, rootDir }) => {
tts.announcement = engine; tts.announcement = engine;
if (res.kind === "exact" || res.kind === "fuzzy") { if (res.kind === "exact" || res.kind === "fuzzy") {
tts.announcementVoice = res.voice; tts.announcementVoice = res.voice;
await persistAnnouncement();
respond(audio, sysmsg, message, t("SYSTEM_VOICE_CHANGED", res.voice, engine.longName)); respond(audio, sysmsg, message, t("SYSTEM_VOICE_CHANGED", res.voice, engine.longName));
} else if (res.kind === "ambiguous") { } else if (res.kind === "ambiguous") {
respond( respond(
@@ -33,6 +45,7 @@ export const ttsSettings: Module = ({ audio, commands, tts, t, rootDir }) => {
); );
} else { } else {
tts.announcementVoice = engine.getDefaultVoice(); tts.announcementVoice = engine.getDefaultVoice();
await persistAnnouncement();
respond( respond(
audio, audio,
sysmsg, sysmsg,