Port to ESM modules (haha funny), removed cache request, many other changes that I forgot about

This commit is contained in:
Essem 2021-08-19 09:19:14 -05:00
parent 2fe45d842b
commit ae2ebe0337
No known key found for this signature in database
GPG key ID: 7D497397CC3A2A8C
157 changed files with 1661 additions and 897 deletions

View file

@ -1,33 +1,32 @@
// dummy (no-op) database handler
const misc = require("../misc.js");
const logger = require("../logger.js");
import { warn } from "../logger.js";
logger.warn("Using dummy database adapter. If this isn't what you wanted, check your DB variable.");
warn("Using dummy database adapter. If this isn't what you wanted, check your DB variable.");
exports.setup = async () => {};
exports.stop = async () => {};
exports.fixGuild = async () => {};
exports.addCount = async () => {};
exports.getCounts = async () => {
export async function setup() {}
export async function stop() {}
export async function fixGuild() {}
export async function addCount() {}
export async function getCounts() {
return {};
};
exports.disableCommand = async () => {};
exports.enableCommand = async () => {};
exports.disableChannel = async () => {};
exports.enableChannel = async () => {};
exports.getTags = async () => {};
exports.getTag = async () => {};
exports.setTag = async () => {};
exports.removeTag = async () => {};
exports.editTag = async () => {};
exports.setPrefix = async () => {};
exports.addGuild = async (guild) => {
}
export async function disableCommand() {}
export async function enableCommand() {}
export async function disableChannel() {}
export async function enableChannel() {}
export async function getTags() {}
export async function getTag() {}
export async function setTag() {}
export async function removeTag() {}
export async function editTag() {}
export async function setPrefix() {}
export async function addGuild(guild) {
return {
id: guild.id,
tags: misc.tagDefaults,
tags: {},
prefix: process.env.PREFIX,
disabled: [],
disabled_commands: []
};
};
exports.getGuild = exports.addGuild;
}
export const getGuild = addGuild;

View file

@ -1,74 +1,74 @@
const collections = require("../collections.js");
const logger = require("../logger.js");
import { prefixCache, disabledCmdCache, disabledCache, commands } from "../collections.js";
import { error, log } from "../logger.js";
const { Pool } = require("pg");
const connection = new Pool({
import Postgres from "pg";
const connection = new Postgres.Pool({
connectionString: process.env.DB,
statement_timeout: 10000
});
exports.getGuild = async (query) => {
export async function getGuild(query) {
return (await connection.query("SELECT * FROM guilds WHERE guild_id = $1", [query])).rows[0];
};
}
exports.setPrefix = async (prefix, guild) => {
export async function setPrefix(prefix, guild) {
await connection.query("UPDATE guilds SET prefix = $1 WHERE guild_id = $2", [prefix, guild.id]);
collections.prefixCache.set(guild.id, prefix);
};
prefixCache.set(guild.id, prefix);
}
exports.getTag = async (guild, tag) => {
export async function getTag(guild, tag) {
const tagResult = (await connection.query("SELECT * FROM tags WHERE guild_id = $1 AND name = $2", [guild, tag])).rows;
return tagResult[0] ? { content: tagResult[0].content, author: tagResult[0].author } : undefined;
};
}
exports.getTags = async (guild) => {
export async function getTags(guild) {
const tagArray = (await connection.query("SELECT * FROM tags WHERE guild_id = $1", [guild])).rows;
const tags = {};
for (const tag of tagArray) {
tags[tag.name] = { content: tag.content, author: tag.author };
}
return tags;
};
}
exports.setTag = async (name, content, guild) => {
export async function setTag(name, content, guild) {
await connection.query("INSERT INTO tags (guild_id, name, content, author) VALUES ($1, $2, $3, $4)", [guild.id, name, content.content, content.author]);
};
}
exports.editTag = async (name, content, guild) => {
export async function editTag(name, content, guild) {
await connection.query("UPDATE tags SET content = $1, author = $2 WHERE guild_id = $3 AND name = $4", [content.content, content.author, guild.id, name]);
};
}
exports.removeTag = async (name, guild) => {
export async function removeTag(name, guild) {
await connection.query("DELETE FROM tags WHERE guild_id = $1 AND name = $2", [guild.id, name]);
};
}
exports.disableCommand = async (guild, command) => {
export async function disableCommand(guild, command) {
const guildDB = await this.getGuild(guild);
await connection.query("UPDATE guilds SET disabled_commands = $1 WHERE guild_id = $2", [(guildDB.disabled_commands ? [...guildDB.disabled_commands, command] : [command]).filter((v) => v !== undefined), guild]);
collections.disabledCmdCache.set(guild, guildDB.disabled_commands ? [...guildDB.disabled_commands, command] : [command].filter((v) => v !== undefined));
};
disabledCmdCache.set(guild, guildDB.disabled_commands ? [...guildDB.disabled_commands, command] : [command].filter((v) => v !== undefined));
}
exports.enableCommand = async (guild, command) => {
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 connection.query("UPDATE guilds SET disabled_commands = $1 WHERE guild_id = $2", [newDisabled, guild]);
collections.disabledCmdCache.set(guild, newDisabled);
};
disabledCmdCache.set(guild, newDisabled);
}
exports.disableChannel = async (channel) => {
export async function disableChannel(channel) {
const guildDB = await this.getGuild(channel.guild.id);
await connection.query("UPDATE guilds SET disabled = $1 WHERE guild_id = $2", [[...guildDB.disabled, channel.id], channel.guild.id]);
collections.disabledCache.set(channel.guild.id, [...guildDB.disabled, channel.id]);
};
disabledCache.set(channel.guild.id, [...guildDB.disabled, channel.id]);
}
exports.enableChannel = async (channel) => {
export async function enableChannel(channel) {
const guildDB = await this.getGuild(channel.guild.id);
const newDisabled = guildDB.disabled.filter(item => item !== channel.id);
await connection.query("UPDATE guilds SET disabled = $1 WHERE guild_id = $2", [newDisabled, channel.guild.id]);
collections.disabledCache.set(channel.guild.id, newDisabled);
};
disabledCache.set(channel.guild.id, newDisabled);
}
exports.getCounts = async () => {
export async function getCounts() {
const counts = await connection.query("SELECT * FROM counts");
//const countArray = [];
const countObject = {};
@ -76,37 +76,37 @@ exports.getCounts = async () => {
countObject[command] = count;
}
return countObject;
};
}
exports.addCount = async (command) => {
export async function addCount(command) {
let count = await connection.query("SELECT * FROM counts WHERE command = $1", [command]);
if (!count.rows[0]) {
await connection.query("INSERT INTO counts (command, count) VALUES ($1, $2)", [command, 0]);
count = await connection.query("SELECT * FROM counts WHERE command = $1", [command]);
}
await connection.query("UPDATE counts SET count = $1 WHERE command = $2", [count.rows[0].count ? count.rows[0].count + 1 : 1, command]);
};
}
exports.addGuild = async (guild) => {
export async function addGuild(guild) {
const query = await this.getGuild(guild);
if (query) return query;
try {
await connection.query("INSERT INTO guilds (guild_id, prefix, disabled, disabled_commands) VALUES ($1, $2, $3, $4)", [guild.id, process.env.PREFIX, [], []]);
} catch (e) {
logger.error(`Failed to register guild ${guild.id}: ${e}`);
error(`Failed to register guild ${guild.id}: ${e}`);
}
return await this.getGuild(guild.id);
};
}
exports.fixGuild = async (guild) => {
export async function fixGuild(guild) {
const guildDB = await connection.query("SELECT exists(SELECT 1 FROM guilds WHERE guild_id = $1)", [guild.id]);
if (!guildDB.rows[0].exists) {
logger.log(`Registering guild database entry for guild ${guild.id}...`);
log(`Registering guild database entry for guild ${guild.id}...`);
return await this.addGuild(guild);
}
};
}
exports.setup = async () => {
export async function setup() {
let counts;
try {
counts = await connection.query("SELECT * FROM counts");
@ -115,12 +115,12 @@ exports.setup = async () => {
}
if (!counts.rows[0]) {
for (const command of collections.commands.keys()) {
for (const command of commands.keys()) {
await connection.query("INSERT INTO counts (command, count) VALUES ($1, $2)", [command, 0]);
}
} else {
const exists = [];
for (const command of collections.commands.keys()) {
for (const command of commands.keys()) {
const count = await connection.query("SELECT * FROM counts WHERE command = $1", [command]);
if (!count.rows[0]) {
await connection.query("INSERT INTO counts (command, count) VALUES ($1, $2)", [command, 0]);
@ -134,8 +134,8 @@ exports.setup = async () => {
}
}
}
};
}
exports.stop = async () => {
export async function stop() {
await connection.end();
};
}

View file

@ -1,10 +1,10 @@
const collections = require("../collections.js");
const logger = require("../logger.js");
import collections from "../collections.js";
import logger from "../logger.js";
const sqlite3 = require("better-sqlite3");
import sqlite3 from "better-sqlite3";
const connection = sqlite3(process.env.DB.replace("sqlite://", ""));
exports.setup = async () => {
export async function setup() {
let counts;
try {
counts = connection.prepare("SELECT * FROM counts").all();
@ -39,13 +39,13 @@ exports.setup = async () => {
}
}
}
};
}
exports.stop = async () => {
export async function stop() {
connection.close();
};
}
exports.fixGuild = async (guild) => {
export async function fixGuild(guild) {
let guildDB;
try {
guildDB = connection.prepare("SELECT * FROM guilds WHERE guild_id = ?").get(guild.id);
@ -56,53 +56,53 @@ exports.fixGuild = async (guild) => {
logger.log(`Registering guild database entry for guild ${guild.id}...`);
return await this.addGuild(guild);
}
};
}
exports.addCount = async (command) => {
export async function addCount(command) {
connection.prepare("UPDATE counts SET count = count + 1 WHERE command = ?").run(command);
};
}
exports.getCounts = async () => {
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;
};
}
exports.disableCommand = async (guild, command) => {
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 !== undefined)), guild);
collections.disabledCmdCache.set(guild, guildDB.disabled_commands ? [...JSON.parse(guildDB.disabledCommands), command] : [command].filter((v) => v !== undefined));
};
}
exports.enableCommand = async (guild, command) => {
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);
collections.disabledCmdCache.set(guild, newDisabled);
};
}
exports.disableChannel = async (channel) => {
export async function disableChannel(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) => {
export async function enableChannel(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) => {
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;
};
}
exports.getTags = async (guild) => {
export async function getTags(guild) {
const tagArray = connection.prepare("SELECT * FROM tags WHERE guild_id = ?").all(guild);
const tags = {};
if (!tagArray) return [];
@ -110,9 +110,9 @@ exports.getTags = async (guild) => {
tags[tag.name] = { content: tag.content, author: tag.author };
}
return tags;
};
}
exports.setTag = async (name, content, guild) => {
export async function setTag(name, content, guild) {
const tag = {
id: guild.id,
name: name,
@ -120,22 +120,22 @@ exports.setTag = async (name, content, guild) => {
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) => {
export async function removeTag(name, guild) {
connection.prepare("DELETE FROM tags WHERE guild_id = ? AND name = ?").run(guild.id, name);
};
}
exports.editTag = async (name, content, guild) => {
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);
};
}
exports.setPrefix = async (prefix, guild) => {
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);
};
}
exports.addGuild = async (guild) => {
export async function addGuild(guild) {
const query = await this.getGuild(guild);
if (query) return query;
const guildObject = {
@ -146,12 +146,12 @@ exports.addGuild = async (guild) => {
};
connection.prepare("INSERT INTO guilds (guild_id, prefix, disabled, disabled_commands) VALUES (@id, @prefix, @disabled, @disabledCommands)").run(guildObject);
return guildObject;
};
}
exports.getGuild = async (query) => {
export async function getGuild(query) {
try {
return connection.prepare("SELECT * FROM guilds WHERE guild_id = ?").get(query);
} catch {
return;
}
};
}