Assorted db changes (#333)

* postgres: use transaction-scoped sql for upgrade

* database: combine fixGuild and addGuild into getGuild

* postgres, sqlite: simplify upgrade()

* allow running commands in DMs without prefix

this functionality was broken in
16095c0256 but is now fixed

* allow running esmBot without a database

Before this change, the only way to run esmBot without a database is to use the
dummy database driver which is broken but fails silently. THis can lead to a
confusing user experience. For instance, using `&command disable` with the
dummy database driver will tell you that the command has been disabled even
though it has not been.

This change adds support for running esmBot with no database driver by leaving
the DB= config option empty, and explicitly telling the user that some
functionality is now unavailable rather than failing silently like the dummy
driver.

* remove dummy database driver
This commit is contained in:
samhza 2022-12-12 12:15:10 -05:00 committed by GitHub
parent 345b525188
commit ed25116851
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 130 additions and 177 deletions

View file

@ -1,9 +0,0 @@
import db from "../utils/database.js";
import { log } from "../utils/logger.js";
// run when the bot is added to a guild
export default async (client, guild) => {
log(`[GUILD JOIN] ${guild.name} (${guild.id}) added the bot.`);
const guildDB = await db.getGuild(guild.id);
if (!guildDB) await db.addGuild(guild.id);
};

View file

@ -15,13 +15,19 @@ export default async (client, interaction) => {
cmd = messageCommands.get(command);
if (!cmd) return;
}
if (cmd.dbRequired && !database) {
await interaction["createMessage"]({ content: "This command is unavailable on stateless instances of esmBot.", flags: 64 });
return;
};
const invoker = interaction.member ?? interaction.user;
// actually run the command
logger.log("log", `${invoker.username} (${invoker.id}) ran application command ${command}`);
try {
await database.addCount(command);
if (database) {
await database.addCount(command);
}
// eslint-disable-next-line no-unused-vars
const commandClass = new cmd(client, { type: "application", interaction });
const result = await commandClass.run();

View file

@ -31,15 +31,12 @@ export default async (client, message) => {
const mentionResult = message.content.match(mentionRegex);
if (mentionResult) {
text = message.content.substring(mentionResult[0].length).trim();
} else if (message.guildID) {
} else if (message.guildID && database) {
const cachedPrefix = prefixCache.get(message.guildID);
if (cachedPrefix && message.content.startsWith(cachedPrefix)) {
text = message.content.substring(cachedPrefix.length).trim();
} else {
guildDB = await database.getGuild(message.guildID);
if (!guildDB) {
guildDB = await database.fixGuild(message.guildID);
}
if (message.content.startsWith(guildDB.prefix)) {
text = message.content.substring(guildDB.prefix.length).trim();
prefixCache.set(message.guildID, guildDB.prefix);
@ -49,6 +46,8 @@ export default async (client, message) => {
}
} else if (message.content.startsWith(process.env.PREFIX)) {
text = message.content.substring(process.env.PREFIX.length).trim();
} else if (!message.guildID) {
text = message.content;
} else {
return;
}
@ -62,8 +61,18 @@ export default async (client, message) => {
const cmd = commands.get(aliased ?? command);
if (!cmd) return;
// block certain commands from running in DMs
if (!cmd.directAllowed && !message.guildID) return;
if (cmd.dbRequired && !database) {
await client.rest.channels.createMessage(message.channelID, {
content: "This command is unavailable on stateless instances of esmBot."
})
return;
};
// don't run if message is in a disabled channel
if (message.guildID) {
if (message.guildID && database) {
let disabled = disabledCache.get(message.guildID);
if (!disabled) {
if (!guildDB) guildDB = await database.getGuild(message.guildID);
@ -81,9 +90,6 @@ export default async (client, message) => {
if (disabledCmds.includes(aliased ?? command)) return;
}
// block certain commands from running in DMs
if (!cmd.directAllowed && !message.guildID) return;
// actually run the command
log("log", `${message.author.username} (${message.author.id}) ran classic command ${command}`);
const reference = {
@ -100,7 +106,9 @@ export default async (client, message) => {
try {
// parse args
const parsed = parseCommand(preArgs);
await database.addCount(aliases.get(command) ?? command);
if (database) {
await database.addCount(aliases.get(command) ?? command);
}
const startTime = new Date();
// eslint-disable-next-line no-unused-vars
const commandClass = new cmd(client, { type: "classic", message, args: parsed._, content: text.replace(command, "").trim(), specialArgs: (({ _, ...o }) => o)(parsed) }); // we also provide the message content as a parameter for cases where we need more accuracy