Make broadcasts persistent, fix Postgres database upgrades, fix image slash commands with text not working in DMs

This commit is contained in:
Essem 2022-10-25 12:37:55 -05:00
parent 089b750838
commit 5d679dbb7a
No known key found for this signature in database
GPG key ID: 7D497397CC3A2A8C
8 changed files with 37 additions and 9 deletions

View file

@ -20,6 +20,8 @@ export async function getTag() {}
export async function setTag() {}
export async function removeTag() {}
export async function editTag() {}
export async function setBroadcast() {}
export async function getBroadcast() {}
export async function setPrefix() {}
export async function addGuild(guild) {
return {

View file

@ -7,7 +7,8 @@ const sql = Postgres(process.env.DB);
const psqlUpdates = [
"", // 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 guilds DROP COLUMN accessed",
"ALTER TABLE settings ADD COLUMN broadcast text"
];
export async function setup() {
@ -56,7 +57,7 @@ export async function upgrade(logger) {
while (version < (psqlUpdates.length - 1)) {
version++;
logger.warn(`Running version ${version} update script (${psqlUpdates[version]})...`);
await db`${psqlUpdates[version]}`;
await db.unsafe(psqlUpdates[version]);
}
});
const ver = psqlUpdates.length - 1;
@ -104,6 +105,15 @@ 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}`;

View file

@ -7,7 +7,8 @@ const connection = sqlite3(process.env.DB.replace("sqlite://", ""));
const sqliteUpdates = [
"", // reserved
"ALTER TABLE guilds ADD COLUMN accessed int",
"ALTER TABLE guilds DROP COLUMN accessed"
"ALTER TABLE guilds DROP COLUMN accessed",
"CREATE TABLE IF NOT EXISTS settings ( id smallint PRIMARY KEY, broadcast VARCHAR, CHECK(id = 1) );\nINSERT INTO settings (id) VALUES (1) ON CONFLICT (id) DO NOTHING;"
];
export async function setup() {
@ -151,6 +152,15 @@ 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);
collections.prefixCache.set(guild.id, prefix);