import { DatabaseSync } from "node:sqlite"; import { commands, messageCommands, userCommands } from "#utils/collections.js"; import logger from "#utils/logger.js"; import { getEnv } from "#utils/runtime.js"; import type { Count, DBGuild, Tag } from "#utils/types.js"; import type { DatabasePlugin } from "../database.ts"; const schema = ` CREATE TABLE guilds ( guild_id VARCHAR(30) NOT NULL PRIMARY KEY, prefix VARCHAR(15) NOT NULL, disabled text NOT NULL, disabled_commands text NOT NULL, tag_roles VARCHAR DEFAULT '[]' ); CREATE TABLE counts ( command VARCHAR NOT NULL PRIMARY KEY, count integer NOT NULL ); CREATE TABLE tags ( guild_id VARCHAR(30) NOT NULL, name text NOT NULL, content text NOT NULL, author VARCHAR(30) NOT NULL, UNIQUE(guild_id, name) ); CREATE TABLE settings ( id smallint PRIMARY KEY, broadcast VARCHAR, CHECK(id = 1) ); INSERT INTO settings (id) VALUES (1); `; const updates = [ "", // reserved "ALTER TABLE guilds ADD COLUMN accessed int", "ALTER TABLE guilds DROP COLUMN accessed", `CREATE TABLE settings ( id smallint PRIMARY KEY, broadcast VARCHAR, CHECK(id = 1) ); INSERT INTO settings (id) VALUES (1);`, "ALTER TABLE guilds ADD COLUMN tag_roles VARCHAR DEFAULT '[]'", `WITH cmds AS ( SELECT sum("count") AS amount FROM counts WHERE command IN ('qrcreate', 'qrread', 'qr') ) INSERT OR REPLACE INTO counts ("command", "count") VALUES ('qr', (SELECT amount FROM cmds)); WITH cmds AS ( SELECT sum("count") AS amount FROM counts WHERE command IN ( '9gag', 'avs4you', 'bandicam', 'deviantart', 'funky', 'hypercam', 'ifunny', 'kinemaster', 'memecenter', 'powerdirector', 'shutterstock', 'watermark' ) ) INSERT OR REPLACE INTO counts ("command", "count") VALUES ('watermark', (SELECT amount FROM cmds));`, ]; export default class SQLitePlugin implements DatabasePlugin { connection: DatabaseSync; constructor(connectString: string) { this.connection = new DatabaseSync(connectString.replace("sqlite://", "")); } async setup() { const existingCommands = (this.connection.prepare("SELECT command FROM counts").all() as { command: string }[]).map( (x) => x.command, ); const commandNames = [...commands.keys(), ...messageCommands.keys(), ...userCommands.keys()]; for (const command of commandNames) { if (!existingCommands.includes(command)) { this.connection.prepare("INSERT INTO counts (command, count) VALUES (?, ?)").run(command, 0); } } } async stop() { this.connection.close(); } async upgrade() { this.connection.exec("PRAGMA journal_mode = WAL;"); try { this.connection.exec("BEGIN TRANSACTION"); let version: number; const result = this.connection.prepare("PRAGMA user_version").get() as { user_version: number }; version = result?.user_version ?? 0; const latestVersion = updates.length - 1; if (version === 0) { logger.info("Initializing SQLite database..."); this.connection.exec(schema); } else if (version < latestVersion) { logger.info(`Migrating SQLite database at ${getEnv("DB")}, which is currently at version ${version}...`); while (version < latestVersion) { version++; logger.info(`Running version ${version} update script...`); this.connection.exec(updates[version]); } } else { if (version > latestVersion) { logger.warn( `SQLite database is at version ${version}, but this version of esmBot only supports up to version ${latestVersion}. Running an older version of esmBot on a newer database is not supported.`, ); } this.connection.exec("ROLLBACK"); return; } this.connection.exec(`PRAGMA user_version = ${latestVersion}`); this.connection.exec("COMMIT"); } catch (e) { logger.error(`SQLite migration failed: ${e}`); logger.error("Unable to start the bot, quitting now."); this.connection.exec("ROLLBACK"); return 1; } } async addCount(command: string) { this.connection.prepare("UPDATE counts SET count = count + 1 WHERE command = ?").run(command); } async getCounts(all?: boolean) { const counts = this.connection.prepare("SELECT * FROM counts").all() as unknown as Count[]; const commandNames = [...commands.keys(), ...messageCommands.keys(), ...userCommands.keys()]; const countMap = new Map( (all ? counts : counts.filter((val) => commandNames.includes(val.command))).map((val) => [ val.command, val.count, ]), ); return countMap; } async disableCommand(guild: string, command: string) { const guildDB = await this.getGuild(guild); this.connection .prepare("UPDATE guilds SET disabled_commands = ? WHERE guild_id = ?") .run( JSON.stringify( (guildDB.disabled_commands ? [...guildDB.disabled_commands, command] : [command]).filter((v) => !!v), ), guild, ); } async enableCommand(guild: string, command: string) { const guildDB = await this.getGuild(guild); const newDisabled = guildDB.disabled_commands ? guildDB.disabled_commands.filter((item) => item !== command) : []; this.connection .prepare("UPDATE guilds SET disabled_commands = ? WHERE guild_id = ?") .run(JSON.stringify(newDisabled), guild); } async disableChannel(channel: string, guild: string) { const guildDB = await this.getGuild(guild); this.connection .prepare("UPDATE guilds SET disabled = ? WHERE guild_id = ?") .run(JSON.stringify([...guildDB.disabled, channel]), guild); } async enableChannel(channel: string, guild: string) { const guildDB = await this.getGuild(guild); const newDisabled = guildDB.disabled.filter((item: string) => item !== channel); this.connection .prepare("UPDATE guilds SET disabled = ? WHERE guild_id = ?") .run(JSON.stringify(newDisabled), guild); } async getTag(guild: string, tag: string) { const tagResult = this.connection.prepare("SELECT * FROM tags WHERE guild_id = ? AND name = ?").get(guild, tag) as | Tag | undefined; return tagResult; } async getTags(guild: string) { const tagArray = this.connection .prepare("SELECT name, content, author FROM tags WHERE guild_id = ?") .all(guild) as unknown as Tag[]; const tags: Record = {}; for (const tag of tagArray) { tags[tag.name] = tag; } return tags; } async setTag(tag: Tag, guild: string) { const tagData = { guild_id: guild, name: tag.name, content: tag.content, author: tag.author, }; this.connection .prepare("INSERT INTO tags (guild_id, name, content, author) VALUES (:guild_id, :name, :content, :author)") .run(tagData); } async removeTag(name: string, guild: string) { this.connection.prepare("DELETE FROM tags WHERE guild_id = ? AND name = ?").run(guild, name); } async editTag(tag: Tag, guild: string) { this.connection .prepare("UPDATE tags SET content = ?, author = ? WHERE guild_id = ? AND name = ?") .run(tag.content, tag.author, guild, tag.name); } async addTagRole(guild: string, role: string) { const guildDB = await this.getGuild(guild); this.connection .prepare("UPDATE guilds SET tag_roles = ? WHERE guild_id = ?") .run(JSON.stringify([...guildDB.tag_roles, role]), guild); } async removeTagRole(guild: string, role: string) { const guildDB = await this.getGuild(guild); this.connection .prepare("UPDATE guilds SET tag_roles = ? WHERE guild_id = ?") .run(JSON.stringify(guildDB.tag_roles.filter((v) => v !== role)), guild); } async setBroadcast(msg?: string) { this.connection.prepare("UPDATE settings SET broadcast = ? WHERE id = 1").run(msg ?? null); } async getBroadcast() { const result = this.connection.prepare("SELECT broadcast FROM settings WHERE id = 1").get() as { broadcast: string | undefined; }; return result.broadcast; } async setPrefix(prefix: string, guild: string) { this.connection.prepare("UPDATE guilds SET prefix = ? WHERE guild_id = ?").run(prefix, guild); } async getGuild(query: string): Promise { // SQLite does not support arrays, so instead we convert them from strings let guild: | ({ disabled: string; disabled_commands: string; tag_roles: string; } & Omit) | undefined; try { this.connection.exec("BEGIN TRANSACTION"); guild = this.connection.prepare("SELECT * FROM guilds WHERE guild_id = ?").get(query) as { disabled: string; disabled_commands: string; tag_roles: string; } & Omit; if (!guild) { const guild_id = query; const prefix = getEnv("PREFIX") ?? "&"; guild = { guild_id, prefix, disabled: "[]", disabled_commands: "[]", tag_roles: "[]", }; this.connection .prepare( "INSERT INTO guilds (guild_id, prefix, disabled, disabled_commands, tag_roles) VALUES (:guild_id, :prefix, :disabled, :disabled_commands, :tag_roles)", ) .run(guild); } this.connection.exec("COMMIT"); } catch { this.connection.exec("ROLLBACK"); } if (guild) { guild.disabled = JSON.parse(guild.disabled); guild.disabled_commands = JSON.parse(guild.disabled_commands); guild.tag_roles = JSON.parse(guild.tag_roles); } return ( (guild as DBGuild | undefined) ?? { guild_id: query, prefix: getEnv("PREFIX") ?? "&", disabled: [], disabled_commands: [], tag_roles: [], } ); } }