Move tags to separate table, part 2

This commit is contained in:
Essem 2021-08-11 10:00:35 -05:00
parent a75ceb41f2
commit e8094c78ec
No known key found for this signature in database
GPG key ID: 7D497397CC3A2A8C

View file

@ -1,5 +1,4 @@
const collections = require("../collections.js"); const collections = require("../collections.js");
const misc = require("../misc.js");
const logger = require("../logger.js"); const logger = require("../logger.js");
const sqlite3 = require("better-sqlite3"); const sqlite3 = require("better-sqlite3");
@ -14,6 +13,12 @@ exports.setup = async () => {
counts = []; counts = [];
} }
try {
connection.prepare("SELECT * FROM tags").all();
} catch {
connection.prepare("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) )").run();
}
if (!counts) { if (!counts) {
for (const command of collections.commands.keys()) { for (const command of collections.commands.keys()) {
connection.prepare("INSERT INTO counts (command, count) VALUES (?, ?)").run(command, 0); connection.prepare("INSERT INTO counts (command, count) VALUES (?, ?)").run(command, 0);
@ -45,7 +50,7 @@ exports.fixGuild = async (guild) => {
try { try {
guildDB = connection.prepare("SELECT * FROM guilds WHERE guild_id = ?").get(guild.id); guildDB = connection.prepare("SELECT * FROM guilds WHERE guild_id = ?").get(guild.id);
} catch { } catch {
connection.prepare("CREATE TABLE guilds ( guild_id VARCHAR(30) NOT NULL, tags text NOT NULL, prefix VARCHAR(15) NOT NULL, disabled text NOT NULL, tags_disabled integer NOT NULL DEFAULT 0 CHECK(tags_disabled IN (0,1)) )").run(); connection.prepare("CREATE TABLE guilds ( guild_id VARCHAR(30) NOT NULL, prefix VARCHAR(15) NOT NULL, disabled text NOT NULL, tags_disabled integer NOT NULL DEFAULT 0 CHECK(tags_disabled IN (0,1)) )").run();
} }
if (!guildDB) { if (!guildDB) {
logger.log(`Registering guild database entry for guild ${guild.id}...`); logger.log(`Registering guild database entry for guild ${guild.id}...`);
@ -85,26 +90,33 @@ exports.toggleTags = async (guild) => {
connection.prepare("UPDATE guilds SET tags_disabled = ? WHERE guild_id = ?").run(guildDB.tags_disabled, guild.id); connection.prepare("UPDATE guilds SET tags_disabled = ? WHERE guild_id = ?").run(guildDB.tags_disabled, guild.id);
}; };
exports.getTags = async (name, content, guild) => { exports.getTags = async (guild) => {
const guildDB = await this.getGuild(guild.id); const tagArray = connection.prepare("SELECT * FROM tags WHERE guild_id = ?").all(guild);
return JSON.parse(guildDB.tags); const tags = {};
if (!tagArray) return [];
for (const tag of tagArray) {
tags[tag.name] = { content: tag.content, author: tag.author };
}
return tags;
}; };
exports.setTag = async (name, content, guild) => { exports.setTag = async (name, content, guild) => {
const guildDB = await this.getGuild(guild.id); const tag = {
const tags = JSON.parse(guildDB.tags); id: guild.id,
tags[name] = content; name: name,
connection.prepare("UPDATE guilds SET tags = ? WHERE guild_id = ?").run(JSON.stringify(tags), guild.id); content: content.content,
author: content.author
};
connection.prepare("INSERT INTO tags (guild_id, name, content, author) VALUES (@id, @name, @content, @author)").run(tag);
}; };
exports.removeTag = async (name, guild) => { exports.removeTag = async (name, guild) => {
const guildDB = await this.getGuild(guild.id); connection.prepare("DELETE FROM tags WHERE guild_id = ? AND name = ?").run(guild.id, name);
const tags = JSON.parse(guildDB.tags);
delete tags[name];
connection.prepare("UPDATE guilds SET tags = ? WHERE guild_id = ?").run(JSON.stringify(tags), guild.id);
}; };
exports.editTag = this.setTag; exports.editTag = async (name, content, guild) => {
connection.prepare("UPDATE tags SET content = ?, author = ? WHERE guild_id = ? AND name = ?").run(content.content, content.author, guild.id, name);
};
exports.setPrefix = async (prefix, guild) => { exports.setPrefix = async (prefix, guild) => {
connection.prepare("UPDATE guilds SET prefix = ? WHERE guild_id = ?").run(prefix, guild.id); connection.prepare("UPDATE guilds SET prefix = ? WHERE guild_id = ?").run(prefix, guild.id);
@ -116,12 +128,11 @@ exports.addGuild = async (guild) => {
if (query) return query; if (query) return query;
const guildObject = { const guildObject = {
id: guild.id, id: guild.id,
tags: JSON.stringify(misc.tagDefaults),
prefix: process.env.PREFIX, prefix: process.env.PREFIX,
disabled: "[]", disabled: "[]",
tagsDisabled: 0 tagsDisabled: 0
}; };
connection.prepare("INSERT INTO guilds (guild_id, tags, prefix, disabled, tags_disabled) VALUES (@id, @tags, @prefix, @disabled, @tagsDisabled)").run(guildObject); connection.prepare("INSERT INTO guilds (guild_id, prefix, disabled, tags_disabled) VALUES (@id, @prefix, @disabled, @tagsDisabled)").run(guildObject);
return guildObject; return guildObject;
}; };