From 4853da05a97f00db98bd6d9b2960383ab1af61b6 Mon Sep 17 00:00:00 2001 From: Talon Date: Thu, 14 May 2026 21:33:18 +0200 Subject: [PATCH] Migration --- src/db/init.ts | 19 +++++++++++++++++++ src/index.ts | 2 ++ src/modules/wordbyword.ts | 5 ++++- 3 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 src/db/init.ts diff --git a/src/db/init.ts b/src/db/init.ts new file mode 100644 index 0000000..4dbcf58 --- /dev/null +++ b/src/db/init.ts @@ -0,0 +1,19 @@ +import type { AppDatabase } from "./db.js"; + +export async function initializeSchema(db: AppDatabase): Promise { + 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 + ); + `); +} diff --git a/src/index.ts b/src/index.ts index 0a52788..94ed7ed 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,6 +8,7 @@ process.on("uncaughtException", (err) => { console.error("Uncaught exception:", err); }); import { openDatabase } from "./db/db.js"; +import { initializeSchema } from "./db/init.js"; import type { BotStateRow } from "./db/schema.js"; import { loadStrings, makeT } from "./i18n/strings.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)); const db = await openDatabase(config.DB_FILE); +await initializeSchema(db); const savedEngine = await db.get( "select value from BotState where key='announcement_engine'", ); diff --git a/src/modules/wordbyword.ts b/src/modules/wordbyword.ts index d34e5f9..aa2b5d7 100644 --- a/src/modules/wordbyword.ts +++ b/src/modules/wordbyword.ts @@ -59,7 +59,10 @@ export const wordbyword: Module = ({ audio, commands, db, t, rootDir }) => { currentWBW.indexOf(".") === -1 ? currentWBW : currentWBW.slice(currentWBW.lastIndexOf(".") + 2); const voiceChannel = message.member?.voice.channel; 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, + ]); } });