initial update
This commit is contained in:
parent
3272429cf6
commit
db9b70bf66
280 changed files with 11772 additions and 11966 deletions
|
@ -1,187 +1,187 @@
|
|||
import { prefixCache, disabledCmdCache, disabledCache, commands, messageCommands } from "../collections.js";
|
||||
|
||||
import Postgres from "postgres";
|
||||
const sql = Postgres(process.env.DB, {
|
||||
onnotice: () => {}
|
||||
});
|
||||
|
||||
const settingsSchema = `
|
||||
CREATE TABLE IF NOT EXISTS settings (
|
||||
id smallint PRIMARY KEY,
|
||||
version integer NOT NULL, CHECK(id = 1)
|
||||
);
|
||||
`;
|
||||
|
||||
const schema = `
|
||||
ALTER TABLE settings ADD COLUMN broadcast text;
|
||||
CREATE TABLE guilds (
|
||||
guild_id VARCHAR(30) NOT NULL PRIMARY KEY,
|
||||
prefix VARCHAR(15) NOT NULL,
|
||||
disabled text ARRAY NOT NULL,
|
||||
disabled_commands text ARRAY NOT NULL
|
||||
);
|
||||
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)
|
||||
);
|
||||
`;
|
||||
|
||||
const updates = [
|
||||
"", // reserved
|
||||
"CREATE TABLE IF NOT EXISTS settings ( id smallint PRIMARY KEY, version integer NOT NULL, CHECK(id = 1) );\nALTER TABLE guilds ADD COLUMN accessed timestamp;",
|
||||
"ALTER TABLE guilds DROP COLUMN accessed",
|
||||
"ALTER TABLE settings ADD COLUMN IF NOT EXISTS broadcast text"
|
||||
];
|
||||
|
||||
export async function setup() {
|
||||
const existingCommands = (await sql`SELECT command FROM counts`).map(x => x.command);
|
||||
const commandNames = [...commands.keys(), ...messageCommands.keys()];
|
||||
for (const command of existingCommands) {
|
||||
if (!commandNames.includes(command)) {
|
||||
await sql`DELETE FROM counts WHERE command = ${command}`;
|
||||
}
|
||||
}
|
||||
for (const command of commandNames) {
|
||||
if (!existingCommands.includes(command)) {
|
||||
await sql`INSERT INTO counts ${sql({ command, count: 0 }, "command", "count")}`;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export async function upgrade(logger) {
|
||||
try {
|
||||
await sql.begin(async (sql) => {
|
||||
await sql.unsafe(settingsSchema);
|
||||
let version;
|
||||
const settingsrow = (await sql`SELECT version FROM settings WHERE id = 1`);
|
||||
if (settingsrow.length == 0) {
|
||||
version = 0;
|
||||
} else {
|
||||
version = settingsrow[0].version;
|
||||
}
|
||||
const latestVersion = updates.length - 1;
|
||||
if (version === 0) {
|
||||
logger.info("Initializing PostgreSQL database...");
|
||||
await sql.unsafe(schema);
|
||||
} else if (version < latestVersion) {
|
||||
logger.info(`Migrating PostgreSQL database, which is currently at version ${version}...`);
|
||||
while (version < latestVersion) {
|
||||
version++;
|
||||
logger.info(`Running version ${version} update script...`);
|
||||
await sql.unsafe(updates[version]);
|
||||
}
|
||||
} else if (version > latestVersion) {
|
||||
throw new Error(`PostgreSQL database is at version ${version}, but this version of the bot only supports up to version ${latestVersion}.`);
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
await sql`INSERT INTO settings ${sql({ id: 1, version: latestVersion })} ON CONFLICT (id) DO UPDATE SET version = ${latestVersion}`;
|
||||
});
|
||||
} catch (e) {
|
||||
logger.error(`PostgreSQL migration failed: ${e}`);
|
||||
logger.error("Unable to start the bot, quitting now.");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
export async function getGuild(query) {
|
||||
let guild;
|
||||
await sql.begin(async (sql) => {
|
||||
guild = (await sql`SELECT * FROM guilds WHERE guild_id = ${query}`)[0];
|
||||
if (guild == undefined) {
|
||||
guild = { guild_id: query, prefix: process.env.PREFIX, disabled: [], disabled_commands: [] };
|
||||
await sql`INSERT INTO guilds ${sql(guild)}`;
|
||||
}
|
||||
});
|
||||
return guild;
|
||||
}
|
||||
|
||||
export async function setPrefix(prefix, guild) {
|
||||
await sql`UPDATE guilds SET prefix = ${prefix} WHERE guild_id = ${guild.id}`;
|
||||
prefixCache.set(guild.id, prefix);
|
||||
}
|
||||
|
||||
export async function getTag(guild, tag) {
|
||||
const tagResult = await sql`SELECT * FROM tags WHERE guild_id = ${guild} AND name = ${tag}`;
|
||||
return tagResult[0] ? { content: tagResult[0].content, author: tagResult[0].author } : undefined;
|
||||
}
|
||||
|
||||
export async function getTags(guild) {
|
||||
const tagArray = await sql`SELECT * FROM tags WHERE guild_id = ${guild}`;
|
||||
const tags = {};
|
||||
for (const tag of tagArray) {
|
||||
tags[tag.name] = { content: tag.content, author: tag.author };
|
||||
}
|
||||
return tags;
|
||||
}
|
||||
|
||||
export async function setTag(name, content, guild) {
|
||||
await sql`INSERT INTO tags ${sql({ guild_id: guild.id, name, content: content.content, author: content.author }, "guild_id", "name", "content", "author")}`;
|
||||
}
|
||||
|
||||
export async function editTag(name, content, guild) {
|
||||
await sql`UPDATE tags SET content = ${content.content}, author = ${content.author} WHERE guild_id = ${guild.id} AND name = ${name}`;
|
||||
}
|
||||
|
||||
export async function removeTag(name, guild) {
|
||||
await sql`DELETE FROM tags WHERE guild_id = ${guild.id} AND name = ${name}`;
|
||||
}
|
||||
|
||||
export async function setBroadcast(msg) {
|
||||
await sql`UPDATE settings SET broadcast = ${msg} WHERE id = 1`;
|
||||
}
|
||||
|
||||
export async function getBroadcast() {
|
||||
const result = await sql`SELECT broadcast FROM settings WHERE id = 1`;
|
||||
return result[0].broadcast;
|
||||
}
|
||||
|
||||
export async function disableCommand(guild, command) {
|
||||
const guildDB = await this.getGuild(guild);
|
||||
await sql`UPDATE guilds SET disabled_commands = ${(guildDB.disabled_commands ? [...guildDB.disabled_commands, command] : [command]).filter((v) => !!v)} WHERE guild_id = ${guild}`;
|
||||
disabledCmdCache.set(guild, guildDB.disabled_commands ? [...guildDB.disabled_commands, command] : [command].filter((v) => !!v));
|
||||
}
|
||||
|
||||
export async function enableCommand(guild, command) {
|
||||
const guildDB = await this.getGuild(guild);
|
||||
const newDisabled = guildDB.disabled_commands ? guildDB.disabled_commands.filter(item => item !== command) : [];
|
||||
await sql`UPDATE guilds SET disabled_commands = ${newDisabled} WHERE guild_id = ${guild}`;
|
||||
disabledCmdCache.set(guild, newDisabled);
|
||||
}
|
||||
|
||||
export async function disableChannel(channel) {
|
||||
const guildDB = await this.getGuild(channel.guildID);
|
||||
await sql`UPDATE guilds SET disabled_commands = ${[...guildDB.disabled, channel.id]} WHERE guild_id = ${channel.guildID}`;
|
||||
disabledCache.set(channel.guildID, [...guildDB.disabled, channel.id]);
|
||||
}
|
||||
|
||||
export async function enableChannel(channel) {
|
||||
const guildDB = await this.getGuild(channel.guildID);
|
||||
const newDisabled = guildDB.disabled.filter(item => item !== channel.id);
|
||||
await sql`UPDATE guilds SET disabled_commands = ${newDisabled} WHERE guild_id = ${channel.guildID}`;
|
||||
disabledCache.set(channel.guildID, newDisabled);
|
||||
}
|
||||
|
||||
export async function getCounts() {
|
||||
const counts = await sql`SELECT * FROM counts`;
|
||||
const countObject = {};
|
||||
for (const { command, count } of counts) {
|
||||
countObject[command] = count;
|
||||
}
|
||||
return countObject;
|
||||
}
|
||||
|
||||
export async function addCount(command) {
|
||||
await sql`INSERT INTO counts ${sql({ command, count: 1 }, "command", "count")} ON CONFLICT (command) DO UPDATE SET count = counts.count + 1 WHERE counts.command = ${command}`;
|
||||
}
|
||||
|
||||
export async function stop() {
|
||||
await sql.end();
|
||||
}
|
||||
import { prefixCache, disabledCmdCache, disabledCache, commands, messageCommands } from "../collections.js";
|
||||
|
||||
import Postgres from "postgres";
|
||||
const sql = Postgres(process.env.DB, {
|
||||
onnotice: () => {}
|
||||
});
|
||||
|
||||
const settingsSchema = `
|
||||
CREATE TABLE IF NOT EXISTS settings (
|
||||
id smallint PRIMARY KEY,
|
||||
version integer NOT NULL, CHECK(id = 1)
|
||||
);
|
||||
`;
|
||||
|
||||
const schema = `
|
||||
ALTER TABLE settings ADD COLUMN broadcast text;
|
||||
CREATE TABLE guilds (
|
||||
guild_id VARCHAR(30) NOT NULL PRIMARY KEY,
|
||||
prefix VARCHAR(15) NOT NULL,
|
||||
disabled text ARRAY NOT NULL,
|
||||
disabled_commands text ARRAY NOT NULL
|
||||
);
|
||||
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)
|
||||
);
|
||||
`;
|
||||
|
||||
const updates = [
|
||||
"", // reserved
|
||||
"CREATE TABLE IF NOT EXISTS settings ( id smallint PRIMARY KEY, version integer NOT NULL, CHECK(id = 1) );\nALTER TABLE guilds ADD COLUMN accessed timestamp;",
|
||||
"ALTER TABLE guilds DROP COLUMN accessed",
|
||||
"ALTER TABLE settings ADD COLUMN IF NOT EXISTS broadcast text"
|
||||
];
|
||||
|
||||
export async function setup() {
|
||||
const existingCommands = (await sql`SELECT command FROM counts`).map(x => x.command);
|
||||
const commandNames = [...commands.keys(), ...messageCommands.keys()];
|
||||
for (const command of existingCommands) {
|
||||
if (!commandNames.includes(command)) {
|
||||
await sql`DELETE FROM counts WHERE command = ${command}`;
|
||||
}
|
||||
}
|
||||
for (const command of commandNames) {
|
||||
if (!existingCommands.includes(command)) {
|
||||
await sql`INSERT INTO counts ${sql({ command, count: 0 }, "command", "count")}`;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export async function upgrade(logger) {
|
||||
try {
|
||||
await sql.begin(async (sql) => {
|
||||
await sql.unsafe(settingsSchema);
|
||||
let version;
|
||||
const settingsrow = (await sql`SELECT version FROM settings WHERE id = 1`);
|
||||
if (settingsrow.length == 0) {
|
||||
version = 0;
|
||||
} else {
|
||||
version = settingsrow[0].version;
|
||||
}
|
||||
const latestVersion = updates.length - 1;
|
||||
if (version === 0) {
|
||||
logger.info("Initializing PostgreSQL database...");
|
||||
await sql.unsafe(schema);
|
||||
} else if (version < latestVersion) {
|
||||
logger.info(`Migrating PostgreSQL database, which is currently at version ${version}...`);
|
||||
while (version < latestVersion) {
|
||||
version++;
|
||||
logger.info(`Running version ${version} update script...`);
|
||||
await sql.unsafe(updates[version]);
|
||||
}
|
||||
} else if (version > latestVersion) {
|
||||
throw new Error(`PostgreSQL database is at version ${version}, but this version of the bot only supports up to version ${latestVersion}.`);
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
await sql`INSERT INTO settings ${sql({ id: 1, version: latestVersion })} ON CONFLICT (id) DO UPDATE SET version = ${latestVersion}`;
|
||||
});
|
||||
} catch (e) {
|
||||
logger.error(`PostgreSQL migration failed: ${e}`);
|
||||
logger.error("Unable to start the bot, quitting now.");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
export async function getGuild(query) {
|
||||
let guild;
|
||||
await sql.begin(async (sql) => {
|
||||
guild = (await sql`SELECT * FROM guilds WHERE guild_id = ${query}`)[0];
|
||||
if (guild == undefined) {
|
||||
guild = { guild_id: query, prefix: process.env.PREFIX, disabled: [], disabled_commands: [] };
|
||||
await sql`INSERT INTO guilds ${sql(guild)}`;
|
||||
}
|
||||
});
|
||||
return guild;
|
||||
}
|
||||
|
||||
export async function setPrefix(prefix, guild) {
|
||||
await sql`UPDATE guilds SET prefix = ${prefix} WHERE guild_id = ${guild.id}`;
|
||||
prefixCache.set(guild.id, prefix);
|
||||
}
|
||||
|
||||
export async function getTag(guild, tag) {
|
||||
const tagResult = await sql`SELECT * FROM tags WHERE guild_id = ${guild} AND name = ${tag}`;
|
||||
return tagResult[0] ? { content: tagResult[0].content, author: tagResult[0].author } : undefined;
|
||||
}
|
||||
|
||||
export async function getTags(guild) {
|
||||
const tagArray = await sql`SELECT * FROM tags WHERE guild_id = ${guild}`;
|
||||
const tags = {};
|
||||
for (const tag of tagArray) {
|
||||
tags[tag.name] = { content: tag.content, author: tag.author };
|
||||
}
|
||||
return tags;
|
||||
}
|
||||
|
||||
export async function setTag(name, content, guild) {
|
||||
await sql`INSERT INTO tags ${sql({ guild_id: guild.id, name, content: content.content, author: content.author }, "guild_id", "name", "content", "author")}`;
|
||||
}
|
||||
|
||||
export async function editTag(name, content, guild) {
|
||||
await sql`UPDATE tags SET content = ${content.content}, author = ${content.author} WHERE guild_id = ${guild.id} AND name = ${name}`;
|
||||
}
|
||||
|
||||
export async function removeTag(name, guild) {
|
||||
await sql`DELETE FROM tags WHERE guild_id = ${guild.id} AND name = ${name}`;
|
||||
}
|
||||
|
||||
export async function setBroadcast(msg) {
|
||||
await sql`UPDATE settings SET broadcast = ${msg} WHERE id = 1`;
|
||||
}
|
||||
|
||||
export async function getBroadcast() {
|
||||
const result = await sql`SELECT broadcast FROM settings WHERE id = 1`;
|
||||
return result[0].broadcast;
|
||||
}
|
||||
|
||||
export async function disableCommand(guild, command) {
|
||||
const guildDB = await this.getGuild(guild);
|
||||
await sql`UPDATE guilds SET disabled_commands = ${(guildDB.disabled_commands ? [...guildDB.disabled_commands, command] : [command]).filter((v) => !!v)} WHERE guild_id = ${guild}`;
|
||||
disabledCmdCache.set(guild, guildDB.disabled_commands ? [...guildDB.disabled_commands, command] : [command].filter((v) => !!v));
|
||||
}
|
||||
|
||||
export async function enableCommand(guild, command) {
|
||||
const guildDB = await this.getGuild(guild);
|
||||
const newDisabled = guildDB.disabled_commands ? guildDB.disabled_commands.filter(item => item !== command) : [];
|
||||
await sql`UPDATE guilds SET disabled_commands = ${newDisabled} WHERE guild_id = ${guild}`;
|
||||
disabledCmdCache.set(guild, newDisabled);
|
||||
}
|
||||
|
||||
export async function disableChannel(channel) {
|
||||
const guildDB = await this.getGuild(channel.guildID);
|
||||
await sql`UPDATE guilds SET disabled_commands = ${[...guildDB.disabled, channel.id]} WHERE guild_id = ${channel.guildID}`;
|
||||
disabledCache.set(channel.guildID, [...guildDB.disabled, channel.id]);
|
||||
}
|
||||
|
||||
export async function enableChannel(channel) {
|
||||
const guildDB = await this.getGuild(channel.guildID);
|
||||
const newDisabled = guildDB.disabled.filter(item => item !== channel.id);
|
||||
await sql`UPDATE guilds SET disabled_commands = ${newDisabled} WHERE guild_id = ${channel.guildID}`;
|
||||
disabledCache.set(channel.guildID, newDisabled);
|
||||
}
|
||||
|
||||
export async function getCounts() {
|
||||
const counts = await sql`SELECT * FROM counts`;
|
||||
const countObject = {};
|
||||
for (const { command, count } of counts) {
|
||||
countObject[command] = count;
|
||||
}
|
||||
return countObject;
|
||||
}
|
||||
|
||||
export async function addCount(command) {
|
||||
await sql`INSERT INTO counts ${sql({ command, count: 1 }, "command", "count")} ON CONFLICT (command) DO UPDATE SET count = counts.count + 1 WHERE counts.command = ${command}`;
|
||||
}
|
||||
|
||||
export async function stop() {
|
||||
await sql.end();
|
||||
}
|
||||
|
|
|
@ -1,194 +1,194 @@
|
|||
import { commands, messageCommands, disabledCache, disabledCmdCache, prefixCache } from "../collections.js";
|
||||
|
||||
import sqlite3 from "better-sqlite3";
|
||||
const connection = sqlite3(process.env.DB.replace("sqlite://", ""));
|
||||
|
||||
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
|
||||
);
|
||||
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);`,
|
||||
];
|
||||
|
||||
export async function setup() {
|
||||
const existingCommands = connection.prepare("SELECT command FROM counts").all().map(x => x.command);
|
||||
const commandNames = [...commands.keys(), ...messageCommands.keys()];
|
||||
for (const command of existingCommands) {
|
||||
if (!commandNames.includes(command)) {
|
||||
connection.prepare("DELETE FROM counts WHERE command = ?").run(command);
|
||||
}
|
||||
}
|
||||
for (const command of commandNames) {
|
||||
if (!existingCommands.includes(command)) {
|
||||
connection.prepare("INSERT INTO counts (command, count) VALUES (?, ?)").run(command, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export async function stop() {
|
||||
connection.close();
|
||||
}
|
||||
|
||||
export async function upgrade(logger) {
|
||||
connection.exec("BEGIN TRANSACTION");
|
||||
try {
|
||||
let version = connection.pragma("user_version", { simple: true });
|
||||
const latestVersion = updates.length - 1;
|
||||
if (version == 0) {
|
||||
logger.info("Initializing SQLite database...");
|
||||
connection.exec(schema);
|
||||
} else if (version < latestVersion) {
|
||||
logger.info(`Migrating SQLite database at ${process.env.DB}, which is currently at version ${version}...`);
|
||||
while (version < latestVersion) {
|
||||
version++;
|
||||
logger.info(`Running version ${version} update script...`);
|
||||
connection.exec(updates[version]);
|
||||
}
|
||||
} else if (version > latestVersion) {
|
||||
throw new Error(`SQLite database is at version ${version}, but this version of the bot only supports up to version ${latestVersion}.`);
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
connection.pragma(`user_version = ${latestVersion}`); // prepared statements don't seem to work here
|
||||
} catch (e) {
|
||||
logger.error(`SQLite migration failed: ${e}`);
|
||||
connection.exec("ROLLBACK");
|
||||
logger.error("Unable to start the bot, quitting now.");
|
||||
return 1;
|
||||
}
|
||||
connection.exec("COMMIT");
|
||||
}
|
||||
|
||||
export async function addCount(command) {
|
||||
connection.prepare("UPDATE counts SET count = count + 1 WHERE command = ?").run(command);
|
||||
}
|
||||
|
||||
export async function getCounts() {
|
||||
const counts = connection.prepare("SELECT * FROM counts").all();
|
||||
const countObject = {};
|
||||
for (const { command, count } of counts) {
|
||||
countObject[command] = count;
|
||||
}
|
||||
return countObject;
|
||||
}
|
||||
|
||||
export async function disableCommand(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)), guild);
|
||||
disabledCmdCache.set(guild, guildDB.disabled_commands ? [...JSON.parse(guildDB.disabledCommands), command] : [command].filter((v) => !!v));
|
||||
}
|
||||
|
||||
export async function enableCommand(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);
|
||||
disabledCmdCache.set(guild, newDisabled);
|
||||
}
|
||||
|
||||
export async function disableChannel(channel) {
|
||||
const guildDB = await this.getGuild(channel.guildID);
|
||||
connection.prepare("UPDATE guilds SET disabled = ? WHERE guild_id = ?").run(JSON.stringify([...JSON.parse(guildDB.disabled), channel.id]), channel.guildID);
|
||||
disabledCache.set(channel.guildID, [...JSON.parse(guildDB.disabled), channel.id]);
|
||||
}
|
||||
|
||||
export async function enableChannel(channel) {
|
||||
const guildDB = await this.getGuild(channel.guildID);
|
||||
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.guildID);
|
||||
disabledCache.set(channel.guildID, newDisabled);
|
||||
}
|
||||
|
||||
export async function getTag(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;
|
||||
}
|
||||
|
||||
export async function getTags(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;
|
||||
}
|
||||
|
||||
export async function setTag(name, content, guild) {
|
||||
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);
|
||||
}
|
||||
|
||||
export async function removeTag(name, guild) {
|
||||
connection.prepare("DELETE FROM tags WHERE guild_id = ? AND name = ?").run(guild.id, name);
|
||||
}
|
||||
|
||||
export async function editTag(name, content, guild) {
|
||||
connection.prepare("UPDATE tags SET content = ?, author = ? WHERE guild_id = ? AND name = ?").run(content.content, content.author, guild.id, name);
|
||||
}
|
||||
|
||||
export async function setBroadcast(msg) {
|
||||
connection.prepare("UPDATE settings SET broadcast = ? WHERE id = 1").run(msg);
|
||||
}
|
||||
|
||||
export async function getBroadcast() {
|
||||
const result = connection.prepare("SELECT broadcast FROM settings WHERE id = 1").all();
|
||||
return result[0].broadcast;
|
||||
}
|
||||
|
||||
export async function setPrefix(prefix, guild) {
|
||||
connection.prepare("UPDATE guilds SET prefix = ? WHERE guild_id = ?").run(prefix, guild.id);
|
||||
prefixCache.set(guild.id, prefix);
|
||||
}
|
||||
|
||||
export async function getGuild(query) {
|
||||
let guild;
|
||||
connection.transaction(() => {
|
||||
guild = connection.prepare("SELECT * FROM guilds WHERE guild_id = ?").get(query);
|
||||
if (!guild) {
|
||||
guild = {
|
||||
id: query,
|
||||
prefix: process.env.PREFIX,
|
||||
disabled: "[]",
|
||||
disabledCommands: "[]"
|
||||
};
|
||||
connection.prepare("INSERT INTO guilds (guild_id, prefix, disabled, disabled_commands) VALUES (@id, @prefix, @disabled, @disabledCommands)").run(guild);
|
||||
}
|
||||
})();
|
||||
return guild;
|
||||
}
|
||||
import { commands, messageCommands, disabledCache, disabledCmdCache, prefixCache } from "../collections.js";
|
||||
|
||||
import sqlite3 from "better-sqlite3";
|
||||
const connection = sqlite3(process.env.DB.replace("sqlite://", ""));
|
||||
|
||||
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
|
||||
);
|
||||
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);`,
|
||||
];
|
||||
|
||||
export async function setup() {
|
||||
const existingCommands = connection.prepare("SELECT command FROM counts").all().map(x => x.command);
|
||||
const commandNames = [...commands.keys(), ...messageCommands.keys()];
|
||||
for (const command of existingCommands) {
|
||||
if (!commandNames.includes(command)) {
|
||||
connection.prepare("DELETE FROM counts WHERE command = ?").run(command);
|
||||
}
|
||||
}
|
||||
for (const command of commandNames) {
|
||||
if (!existingCommands.includes(command)) {
|
||||
connection.prepare("INSERT INTO counts (command, count) VALUES (?, ?)").run(command, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export async function stop() {
|
||||
connection.close();
|
||||
}
|
||||
|
||||
export async function upgrade(logger) {
|
||||
connection.exec("BEGIN TRANSACTION");
|
||||
try {
|
||||
let version = connection.pragma("user_version", { simple: true });
|
||||
const latestVersion = updates.length - 1;
|
||||
if (version == 0) {
|
||||
logger.info("Initializing SQLite database...");
|
||||
connection.exec(schema);
|
||||
} else if (version < latestVersion) {
|
||||
logger.info(`Migrating SQLite database at ${process.env.DB}, which is currently at version ${version}...`);
|
||||
while (version < latestVersion) {
|
||||
version++;
|
||||
logger.info(`Running version ${version} update script...`);
|
||||
connection.exec(updates[version]);
|
||||
}
|
||||
} else if (version > latestVersion) {
|
||||
throw new Error(`SQLite database is at version ${version}, but this version of the bot only supports up to version ${latestVersion}.`);
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
connection.pragma(`user_version = ${latestVersion}`); // prepared statements don't seem to work here
|
||||
} catch (e) {
|
||||
logger.error(`SQLite migration failed: ${e}`);
|
||||
connection.exec("ROLLBACK");
|
||||
logger.error("Unable to start the bot, quitting now.");
|
||||
return 1;
|
||||
}
|
||||
connection.exec("COMMIT");
|
||||
}
|
||||
|
||||
export async function addCount(command) {
|
||||
connection.prepare("UPDATE counts SET count = count + 1 WHERE command = ?").run(command);
|
||||
}
|
||||
|
||||
export async function getCounts() {
|
||||
const counts = connection.prepare("SELECT * FROM counts").all();
|
||||
const countObject = {};
|
||||
for (const { command, count } of counts) {
|
||||
countObject[command] = count;
|
||||
}
|
||||
return countObject;
|
||||
}
|
||||
|
||||
export async function disableCommand(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)), guild);
|
||||
disabledCmdCache.set(guild, guildDB.disabled_commands ? [...JSON.parse(guildDB.disabledCommands), command] : [command].filter((v) => !!v));
|
||||
}
|
||||
|
||||
export async function enableCommand(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);
|
||||
disabledCmdCache.set(guild, newDisabled);
|
||||
}
|
||||
|
||||
export async function disableChannel(channel) {
|
||||
const guildDB = await this.getGuild(channel.guildID);
|
||||
connection.prepare("UPDATE guilds SET disabled = ? WHERE guild_id = ?").run(JSON.stringify([...JSON.parse(guildDB.disabled), channel.id]), channel.guildID);
|
||||
disabledCache.set(channel.guildID, [...JSON.parse(guildDB.disabled), channel.id]);
|
||||
}
|
||||
|
||||
export async function enableChannel(channel) {
|
||||
const guildDB = await this.getGuild(channel.guildID);
|
||||
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.guildID);
|
||||
disabledCache.set(channel.guildID, newDisabled);
|
||||
}
|
||||
|
||||
export async function getTag(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;
|
||||
}
|
||||
|
||||
export async function getTags(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;
|
||||
}
|
||||
|
||||
export async function setTag(name, content, guild) {
|
||||
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);
|
||||
}
|
||||
|
||||
export async function removeTag(name, guild) {
|
||||
connection.prepare("DELETE FROM tags WHERE guild_id = ? AND name = ?").run(guild.id, name);
|
||||
}
|
||||
|
||||
export async function editTag(name, content, guild) {
|
||||
connection.prepare("UPDATE tags SET content = ?, author = ? WHERE guild_id = ? AND name = ?").run(content.content, content.author, guild.id, name);
|
||||
}
|
||||
|
||||
export async function setBroadcast(msg) {
|
||||
connection.prepare("UPDATE settings SET broadcast = ? WHERE id = 1").run(msg);
|
||||
}
|
||||
|
||||
export async function getBroadcast() {
|
||||
const result = connection.prepare("SELECT broadcast FROM settings WHERE id = 1").all();
|
||||
return result[0].broadcast;
|
||||
}
|
||||
|
||||
export async function setPrefix(prefix, guild) {
|
||||
connection.prepare("UPDATE guilds SET prefix = ? WHERE guild_id = ?").run(prefix, guild.id);
|
||||
prefixCache.set(guild.id, prefix);
|
||||
}
|
||||
|
||||
export async function getGuild(query) {
|
||||
let guild;
|
||||
connection.transaction(() => {
|
||||
guild = connection.prepare("SELECT * FROM guilds WHERE guild_id = ?").get(query);
|
||||
if (!guild) {
|
||||
guild = {
|
||||
id: query,
|
||||
prefix: process.env.PREFIX,
|
||||
disabled: "[]",
|
||||
disabledCommands: "[]"
|
||||
};
|
||||
connection.prepare("INSERT INTO guilds (guild_id, prefix, disabled, disabled_commands) VALUES (@id, @prefix, @disabled, @disabledCommands)").run(guild);
|
||||
}
|
||||
})();
|
||||
return guild;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue