mrmBot-Matrix/utils/database/sqlite.js

158 lines
5.8 KiB
JavaScript
Raw Normal View History

const collections = require("../collections.js");
const logger = require("../logger.js");
const sqlite3 = require("better-sqlite3");
const connection = sqlite3(process.env.DB.replace("sqlite://", ""));
exports.setup = async () => {
let counts;
try {
counts = connection.prepare("SELECT * FROM counts").all();
} catch {
2021-08-11 21:54:43 +00:00
connection.prepare("CREATE TABLE counts ( command VARCHAR NOT NULL PRIMARY KEY, count integer NOT NULL )").run();
counts = [];
}
2021-08-11 15:00:35 +00:00
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) {
for (const command of collections.commands.keys()) {
connection.prepare("INSERT INTO counts (command, count) VALUES (?, ?)").run(command, 0);
}
} else {
const exists = [];
for (const command of collections.commands.keys()) {
const count = connection.prepare("SELECT * FROM counts WHERE command = ?").get(command);
if (!count) {
connection.prepare("INSERT INTO counts (command, count) VALUES (?, ?)").run(command, 0);
}
exists.push(command);
}
2021-08-14 21:15:21 +00:00
for (const { command } of counts) {
if (!exists.includes(command)) {
connection.prepare("DELETE FROM counts WHERE command = ?").run(command);
}
}
}
};
exports.stop = async () => {
connection.close();
};
exports.fixGuild = async (guild) => {
let guildDB;
try {
guildDB = connection.prepare("SELECT * FROM guilds WHERE guild_id = ?").get(guild.id);
} catch {
connection.prepare("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 )").run();
}
if (!guildDB) {
logger.log(`Registering guild database entry for guild ${guild.id}...`);
return await this.addGuild(guild);
}
};
exports.addCount = async (command) => {
connection.prepare("UPDATE counts SET count = count + 1 WHERE command = ?").run(command);
};
exports.getCounts = async () => {
const counts = connection.prepare("SELECT * FROM counts").all();
const countObject = {};
for (const { command, count } of counts) {
countObject[command] = count;
}
return countObject;
};
exports.disableCommand = async (guild, command) => {
const guildDB = await this.getGuild(guild);
connection.prepare("UPDATE guilds SET disabled_commands = ? WHERE guild_id = ?").run(JSON.stringify((guildDB.disabledCommands ? [...JSON.parse(guildDB.disabledCommands), command] : [command]).filter((v) => v !== undefined)), guild);
collections.disabledCmdCache.set(guild, guildDB.disabled_commands ? [...JSON.parse(guildDB.disabledCommands), command] : [command].filter((v) => v !== undefined));
};
exports.enableCommand = async (guild, command) => {
const guildDB = await this.getGuild(guild);
const newDisabled = guildDB.disabledCommands ? JSON.parse(guildDB.disabledCommands).filter(item => item !== command) : [];
connection.prepare("UPDATE guilds SET disabled_commands = ? WHERE guild_id = ?").run(JSON.stringify(newDisabled), guild);
collections.disabledCmdCache.set(guild, newDisabled);
};
exports.disableChannel = async (channel) => {
const guildDB = await this.getGuild(channel.guild.id);
connection.prepare("UPDATE guilds SET disabled = ? WHERE guild_id = ?").run(JSON.stringify([...JSON.parse(guildDB.disabled), channel.id]), channel.guild.id);
collections.disabledCache.set(channel.guild.id, [...JSON.parse(guildDB.disabled), channel.id]);
};
exports.enableChannel = async (channel) => {
const guildDB = await this.getGuild(channel.guild.id);
const newDisabled = JSON.parse(guildDB.disabled).filter(item => item !== channel.id);
connection.prepare("UPDATE guilds SET disabled = ? WHERE guild_id = ?").run(JSON.stringify(newDisabled), channel.guild.id);
collections.disabledCache.set(channel.guild.id, newDisabled);
};
exports.getTag = async (guild, tag) => {
const tagResult = connection.prepare("SELECT * FROM tags WHERE guild_id = ? AND name = ?").get(guild, tag);
return tagResult ? { content: tagResult.content, author: tagResult.author } : undefined;
};
2021-08-11 15:00:35 +00:00
exports.getTags = async (guild) => {
const tagArray = connection.prepare("SELECT * FROM tags WHERE guild_id = ?").all(guild);
const tags = {};
if (!tagArray) return [];
for (const tag of tagArray) {
tags[tag.name] = { content: tag.content, author: tag.author };
}
return tags;
2021-08-11 01:25:29 +00:00
};
exports.setTag = async (name, content, guild) => {
2021-08-11 15:00:35 +00:00
const tag = {
id: guild.id,
name: name,
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) => {
2021-08-11 15:00:35 +00:00
connection.prepare("DELETE FROM tags WHERE guild_id = ? AND name = ?").run(guild.id, name);
};
2021-08-11 15:00:35 +00:00
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);
};
2021-08-11 01:25:29 +00:00
exports.setPrefix = async (prefix, guild) => {
connection.prepare("UPDATE guilds SET prefix = ? WHERE guild_id = ?").run(prefix, guild.id);
collections.prefixCache.set(guild.id, prefix);
};
exports.addGuild = async (guild) => {
const query = await this.getGuild(guild);
if (query) return query;
const guildObject = {
id: guild.id,
prefix: process.env.PREFIX,
disabled: "[]",
disabledCommands: "[]"
};
connection.prepare("INSERT INTO guilds (guild_id, prefix, disabled, disabled_commands) VALUES (@id, @prefix, @disabled, @tagsDisabled)").run(guildObject);
return guildObject;
};
exports.getGuild = async (query) => {
try {
return connection.prepare("SELECT * FROM guilds WHERE guild_id = ?").get(query);
} catch {
return;
}
};