Migration

This commit is contained in:
2026-05-14 21:33:18 +02:00
parent f56e6079e6
commit 4853da05a9
3 changed files with 25 additions and 1 deletions

19
src/db/init.ts Normal file
View File

@@ -0,0 +1,19 @@
import type { AppDatabase } from "./db.js";
export async function initializeSchema(db: AppDatabase): Promise<void> {
await db.exec(`
CREATE TABLE IF NOT EXISTS BotState (
key TEXT PRIMARY KEY,
value TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS TTSPreferences (
user_id TEXT PRIMARY KEY,
engine TEXT NOT NULL,
voice TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS WBWStories (
story_id INTEGER PRIMARY KEY AUTOINCREMENT,
story_text TEXT NOT NULL
);
`);
}

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 { initializeSchema } from "./db/init.js";
import type { BotStateRow } from "./db/schema.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";
@@ -32,6 +33,7 @@ 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);
await initializeSchema(db);
const savedEngine = await db.get<BotStateRow>( const savedEngine = await db.get<BotStateRow>(
"select value from BotState where key='announcement_engine'", "select value from BotState where key='announcement_engine'",
); );

View File

@@ -59,7 +59,10 @@ export const wordbyword: Module = ({ audio, commands, db, t, rootDir }) => {
currentWBW.indexOf(".") === -1 ? currentWBW : currentWBW.slice(currentWBW.lastIndexOf(".") + 2); currentWBW.indexOf(".") === -1 ? currentWBW : currentWBW.slice(currentWBW.lastIndexOf(".") + 2);
const voiceChannel = message.member?.voice.channel; const voiceChannel = message.member?.voice.channel;
if (voiceChannel) await audio.speak(voiceChannel, toSay); if (voiceChannel) await audio.speak(voiceChannel, toSay);
await db.run("update BotState set value=? where key='last_wbw'", [message.author.id]); await db.run("insert or replace into BotState (key, value) values (?, ?)", [
"last_wbw",
message.author.id,
]);
} }
}); });