Added command, reverted database service split

This commit is contained in:
Essem 2021-08-12 22:28:09 -05:00
parent da709c485f
commit 9f36a79a2b
No known key found for this signature in database
GPG key ID: 7D497397CC3A2A8C
20 changed files with 119 additions and 91 deletions

View file

@ -11,9 +11,10 @@ exports.addCount = async () => {};
exports.getCounts = async () => {
return {};
};
exports.disableCommand = async () => {};
exports.enableCommand = async () => {};
exports.disableChannel = async () => {};
exports.enableChannel = async () => {};
exports.toggleTags = async () => {};
exports.getTags = async () => {};
exports.setTag = async () => {};
exports.removeTag = async () => {};

View file

@ -37,11 +37,17 @@ exports.removeTag = async (name, guild) => {
await connection.query("DELETE FROM tags WHERE guild_id = $1 AND name = $2", [guild.id, name]);
};
exports.toggleTags = async (guild) => {
const guildDB = await this.getGuild(guild.id);
guildDB.tags_disabled = !guildDB.tags_disabled;
await connection.query("UPDATE guilds SET tags_disabled = $1 WHERE guild_id = $2", [guildDB.tags_disabled, guild.id]);
return guildDB.tags_disabled;
exports.disableCommand = async (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));
};
exports.enableCommand = async (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);
};
exports.disableChannel = async (channel) => {
@ -79,7 +85,7 @@ exports.addCount = async (command) => {
exports.addGuild = async (guild) => {
const query = await this.getGuild(guild);
if (query) return query;
await connection.query("INSERT INTO guilds (guild_id, prefix, disabled, tags_disabled) VALUES ($1, $2, $3, $4)", [guild.id, process.env.PREFIX, [], false]);
await connection.query("INSERT INTO guilds (guild_id, prefix, disabled, disabled_commands) VALUES ($1, $2, $3, $4)", [guild.id, process.env.PREFIX, [], []]);
return await this.getGuild(guild.id);
};

View file

@ -50,7 +50,7 @@ exports.fixGuild = async (guild) => {
try {
guildDB = connection.prepare("SELECT * FROM guilds WHERE guild_id = ?").get(guild.id);
} catch {
connection.prepare("CREATE TABLE guilds ( guild_id VARCHAR(30) NOT NULL PRIMARY KEY, prefix VARCHAR(15) NOT NULL, disabled text NOT NULL, tags_disabled integer NOT NULL DEFAULT 0 CHECK(tags_disabled IN (0,1)) )").run();
connection.prepare("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 )").run();
}
if (!guildDB) {
logger.log(`Registering guild database entry for guild ${guild.id}...`);
@ -71,6 +71,19 @@ exports.getCounts = async () => {
return countObject;
};
exports.disableCommand = async (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) => {
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) => {
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);
@ -84,12 +97,6 @@ exports.enableChannel = async (channel) => {
collections.disabledCache.set(channel.guild.id, newDisabled);
};
exports.toggleTags = async (guild) => {
const guildDB = await this.getGuild(guild.id);
guildDB.tags_disabled = guildDB.tags_disabled ? 0 : 1;
connection.prepare("UPDATE guilds SET tags_disabled = ? WHERE guild_id = ?").run(guildDB.tags_disabled, guild.id);
};
exports.getTags = async (guild) => {
const tagArray = connection.prepare("SELECT * FROM tags WHERE guild_id = ?").all(guild);
const tags = {};
@ -130,9 +137,9 @@ exports.addGuild = async (guild) => {
id: guild.id,
prefix: process.env.PREFIX,
disabled: "[]",
tagsDisabled: 0
disabledCommands: "[]"
};
connection.prepare("INSERT INTO guilds (guild_id, prefix, disabled, tags_disabled) VALUES (@id, @prefix, @disabled, @tagsDisabled)").run(guildObject);
connection.prepare("INSERT INTO guilds (guild_id, prefix, disabled, disabled_commands) VALUES (@id, @prefix, @disabled, @tagsDisabled)").run(guildObject);
return guildObject;
};