god dammit

This commit is contained in:
Essem 2022-12-12 11:36:08 -06:00
parent ed25116851
commit 14d5965caa
No known key found for this signature in database
GPG key ID: 7D497397CC3A2A8C
5 changed files with 18 additions and 20 deletions

View file

@ -7,7 +7,7 @@ class PrefixCommand extends Command {
const guild = await database.getGuild(this.guild.id); const guild = await database.getGuild(this.guild.id);
if (this.args.length !== 0) { if (this.args.length !== 0) {
if (!database) { if (!database) {
return "Setting a per-guild prefix is not possible on a stateless instance of esmBot!" return "Setting a per-guild prefix is not possible on a stateless instance of esmBot!";
} }
const owners = process.env.OWNER.split(","); const owners = process.env.OWNER.split(",");
if (!this.member.permissions.has("ADMINISTRATOR") && !owners.includes(this.member.id)) { if (!this.member.permissions.has("ADMINISTRATOR") && !owners.includes(this.member.id)) {

View file

@ -18,7 +18,7 @@ export default async (client, interaction) => {
if (cmd.dbRequired && !database) { if (cmd.dbRequired && !database) {
await interaction["createMessage"]({ content: "This command is unavailable on stateless instances of esmBot.", flags: 64 }); await interaction["createMessage"]({ content: "This command is unavailable on stateless instances of esmBot.", flags: 64 });
return; return;
}; }
const invoker = interaction.member ?? interaction.user; const invoker = interaction.member ?? interaction.user;

View file

@ -67,9 +67,9 @@ export default async (client, message) => {
if (cmd.dbRequired && !database) { if (cmd.dbRequired && !database) {
await client.rest.channels.createMessage(message.channelID, { await client.rest.channels.createMessage(message.channelID, {
content: "This command is unavailable on stateless instances of esmBot." content: "This command is unavailable on stateless instances of esmBot."
}) });
return; return;
}; }
// don't run if message is in a disabled channel // don't run if message is in a disabled channel
if (message.guildID && database) { if (message.guildID && database) {

View file

@ -1,5 +1,4 @@
import { prefixCache, disabledCmdCache, disabledCache, commands, messageCommands } from "../collections.js"; import { prefixCache, disabledCmdCache, disabledCache, commands, messageCommands } from "../collections.js";
import * as logger from "../logger.js";
import Postgres from "postgres"; import Postgres from "postgres";
const sql = Postgres(process.env.DB, { const sql = Postgres(process.env.DB, {
@ -48,12 +47,12 @@ export async function setup() {
if (!commandNames.includes(command)) { if (!commandNames.includes(command)) {
await sql`DELETE FROM counts WHERE command = ${command}`; await sql`DELETE FROM counts WHERE command = ${command}`;
} }
}; }
for (const command of commandNames) { for (const command of commandNames) {
if (!existingCommands.includes(command)) { if (!existingCommands.includes(command)) {
await sql`INSERT INTO counts ${sql({ command, count: 0 }, "command", "count")}`; await sql`INSERT INTO counts ${sql({ command, count: 0 }, "command", "count")}`;
} }
}; }
} }
export async function upgrade(logger) { export async function upgrade(logger) {
@ -66,10 +65,10 @@ export async function upgrade(logger) {
version = 0; version = 0;
} else { } else {
version = settingsrow[0].version; version = settingsrow[0].version;
}; }
const latestVersion = updates.length - 1; const latestVersion = updates.length - 1;
if (version === 0) { if (version === 0) {
logger.info(`Initializing PostgreSQL database...`); logger.info("Initializing PostgreSQL database...");
await sql.unsafe(schema); await sql.unsafe(schema);
} else if (version < latestVersion) { } else if (version < latestVersion) {
logger.info(`Migrating PostgreSQL database, which is currently at version ${version}...`); logger.info(`Migrating PostgreSQL database, which is currently at version ${version}...`);
@ -99,7 +98,7 @@ export async function getGuild(query) {
if (guild == undefined) { if (guild == undefined) {
guild = { guild_id: query, prefix: process.env.PREFIX, disabled: [], disabled_commands: [] }; guild = { guild_id: query, prefix: process.env.PREFIX, disabled: [], disabled_commands: [] };
await sql`INSERT INTO guilds ${sql(guild)}`; await sql`INSERT INTO guilds ${sql(guild)}`;
}; }
}); });
return guild; return guild;
} }

View file

@ -1,5 +1,4 @@
import { commands, messageCommands } from "../collections.js"; import { commands, messageCommands, disabledCache, disabledCmdCache, prefixCache } from "../collections.js";
import * as logger from "../logger.js";
import sqlite3 from "better-sqlite3"; import sqlite3 from "better-sqlite3";
const connection = sqlite3(process.env.DB.replace("sqlite://", "")); const connection = sqlite3(process.env.DB.replace("sqlite://", ""));
@ -49,12 +48,12 @@ export async function setup() {
if (!commandNames.includes(command)) { if (!commandNames.includes(command)) {
connection.prepare("DELETE FROM counts WHERE command = ?").run(command); connection.prepare("DELETE FROM counts WHERE command = ?").run(command);
} }
}; }
for (const command of commandNames) { for (const command of commandNames) {
if (!existingCommands.includes(command)) { if (!existingCommands.includes(command)) {
connection.prepare("INSERT INTO counts (command, count) VALUES (?, ?)").run(command, 0); connection.prepare("INSERT INTO counts (command, count) VALUES (?, ?)").run(command, 0);
} }
}; }
} }
export async function stop() { export async function stop() {
@ -67,7 +66,7 @@ export async function upgrade(logger) {
let version = connection.pragma("user_version", { simple: true }); let version = connection.pragma("user_version", { simple: true });
const latestVersion = updates.length - 1; const latestVersion = updates.length - 1;
if (version == 0) { if (version == 0) {
logger.info(`Initializing SQLite database...`); logger.info("Initializing SQLite database...");
connection.exec(schema); connection.exec(schema);
} else if (version < latestVersion) { } else if (version < latestVersion) {
logger.info(`Migrating SQLite database at ${process.env.DB}, which is currently at version ${version}...`); logger.info(`Migrating SQLite database at ${process.env.DB}, which is currently at version ${version}...`);
@ -107,27 +106,27 @@ export async function getCounts() {
export async function disableCommand(guild, command) { export async function disableCommand(guild, command) {
const guildDB = await this.getGuild(guild); 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); 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);
collections.disabledCmdCache.set(guild, guildDB.disabled_commands ? [...JSON.parse(guildDB.disabledCommands), command] : [command].filter((v) => !!v)); disabledCmdCache.set(guild, guildDB.disabled_commands ? [...JSON.parse(guildDB.disabledCommands), command] : [command].filter((v) => !!v));
} }
export async function enableCommand(guild, command) { export async function enableCommand(guild, command) {
const guildDB = await this.getGuild(guild); const guildDB = await this.getGuild(guild);
const newDisabled = guildDB.disabledCommands ? JSON.parse(guildDB.disabledCommands).filter(item => item !== command) : []; 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); connection.prepare("UPDATE guilds SET disabled_commands = ? WHERE guild_id = ?").run(JSON.stringify(newDisabled), guild);
collections.disabledCmdCache.set(guild, newDisabled); disabledCmdCache.set(guild, newDisabled);
} }
export async function disableChannel(channel) { export async function disableChannel(channel) {
const guildDB = await this.getGuild(channel.guildID); 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); connection.prepare("UPDATE guilds SET disabled = ? WHERE guild_id = ?").run(JSON.stringify([...JSON.parse(guildDB.disabled), channel.id]), channel.guildID);
collections.disabledCache.set(channel.guildID, [...JSON.parse(guildDB.disabled), channel.id]); disabledCache.set(channel.guildID, [...JSON.parse(guildDB.disabled), channel.id]);
} }
export async function enableChannel(channel) { export async function enableChannel(channel) {
const guildDB = await this.getGuild(channel.guildID); const guildDB = await this.getGuild(channel.guildID);
const newDisabled = JSON.parse(guildDB.disabled).filter(item => item !== channel.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.guildID); connection.prepare("UPDATE guilds SET disabled = ? WHERE guild_id = ?").run(JSON.stringify(newDisabled), channel.guildID);
collections.disabledCache.set(channel.guildID, newDisabled); disabledCache.set(channel.guildID, newDisabled);
} }
export async function getTag(guild, tag) { export async function getTag(guild, tag) {
@ -174,7 +173,7 @@ export async function getBroadcast() {
export async function setPrefix(prefix, guild) { export async function setPrefix(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);
collections.prefixCache.set(guild.id, prefix); prefixCache.set(guild.id, prefix);
} }
export async function getGuild(query) { export async function getGuild(query) {