2020-12-19 00:50:25 +00:00
|
|
|
const collections = require("../collections.js");
|
|
|
|
const logger = require("../logger.js");
|
|
|
|
const misc = require("../misc.js");
|
|
|
|
|
|
|
|
const { Pool } = require("pg");
|
|
|
|
const pool = new Pool({
|
|
|
|
connectionString: process.env.DB
|
|
|
|
});
|
|
|
|
const connection = pool;
|
|
|
|
|
|
|
|
exports.getGuild = async (query) => {
|
|
|
|
return (await connection.query("SELECT * FROM guilds WHERE guild_id = $1", [query])).rows[0];
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.setPrefix = async (prefix, guild) => {
|
|
|
|
await connection.query("UPDATE guilds SET prefix = $1 WHERE guild_id = $2", [prefix, guild.id]);
|
|
|
|
collections.prefixCache.set(guild.id, prefix);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.setTag = async (name, content, guild) => {
|
|
|
|
const guildDB = await this.getGuild(guild.id);
|
|
|
|
guildDB.tags[name] = content;
|
|
|
|
await connection.query("UPDATE guilds SET tags = $1 WHERE guild_id = $2", [guildDB.tags, guild.id]);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.removeTag = async (name, guild) => {
|
|
|
|
const guildDB = await this.getGuild(guild.id);
|
|
|
|
delete guildDB.tags[name];
|
|
|
|
await connection.query("UPDATE guilds SET tags = $1 WHERE guild_id = $2", [guildDB.tags, guild.id]);
|
|
|
|
};
|
|
|
|
|
|
|
|
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.disableChannel = async (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);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.enableChannel = async (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, guildDB.disabled);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.getCounts = async () => {
|
|
|
|
const counts = await connection.query("SELECT * FROM counts");
|
2021-05-03 13:49:55 +00:00
|
|
|
//const countArray = [];
|
|
|
|
const countObject = {};
|
2020-12-19 00:50:25 +00:00
|
|
|
for (const { command, count } of counts.rows) {
|
2021-05-03 13:49:55 +00:00
|
|
|
countObject[command] = count;
|
2020-12-19 00:50:25 +00:00
|
|
|
}
|
2021-05-03 13:49:55 +00:00
|
|
|
return countObject;
|
2020-12-19 00:50:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.addCount = async (command) => {
|
2021-04-01 03:16:23 +00:00
|
|
|
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]);
|
|
|
|
}
|
2021-01-08 18:08:10 +00:00
|
|
|
await connection.query("UPDATE counts SET count = $1 WHERE command = $2", [count.rows[0].count ? count.rows[0].count + 1 : 1, command]);
|
2020-12-19 00:50:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.addGuild = async (guild) => {
|
Class commands, improved sharding, and many other changes (#88)
* Load commands recursively
* Sort commands
* Missed a couple of spots
* missed even more spots apparently
* Ported commands in "fun" category to new class-based format, added babel eslint plugin
* Ported general commands, removed old/unneeded stuff, replaced moment with day, many more fixes I lost track of
* Missed a spot
* Removed unnecessary abort-controller package, add deprecation warning for mongo database
* Added imagereload, clarified premature end message
* Fixed docker-compose path issue, added total bot uptime to stats, more fixes for various parts
* Converted image commands into classes, fixed reload, ignore another WS event, cleaned up command handler and image runner
* Converted music/soundboard commands to class format
* Cleanup unnecessary logs
* awful tag command class port
* I literally somehow just learned that you can leave out the constructor in classes
* Pass client directly to commands/events, cleaned up command handler
* Migrated bot to eris-sharder, fixed some error handling stuff
* Remove unused modules
* Fixed type returning
* Switched back to Eris stable
* Some fixes and cleanup
* might wanna correct this
* Implement image command ratelimiting
* Added Bot token prefix, added imagestats, added running endpoint to API
2021-04-12 16:16:12 +00:00
|
|
|
const query = await this.getGuild(guild);
|
|
|
|
if (query) return query;
|
2021-02-07 17:08:55 +00:00
|
|
|
await connection.query("INSERT INTO guilds (guild_id, tags, prefix, disabled, tags_disabled) VALUES ($1, $2, $3, $4, $5)", [guild.id, misc.tagDefaults, process.env.PREFIX, [], false]);
|
2020-12-19 00:50:25 +00:00
|
|
|
return await this.getGuild(guild.id);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.fixGuild = async (guild) => {
|
|
|
|
const guildDB = await connection.query("SELECT * FROM guilds WHERE guild_id = $1", [guild.id]);
|
|
|
|
if (guildDB.rows.length === 0) {
|
|
|
|
logger.log(`Registering guild database entry for guild ${guild.id}...`);
|
|
|
|
return await this.addGuild(guild);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.setup = async () => {
|
|
|
|
let counts;
|
|
|
|
try {
|
|
|
|
counts = await connection.query("SELECT * FROM counts");
|
|
|
|
} catch {
|
|
|
|
counts = { rows: [] };
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!counts.rows[0]) {
|
|
|
|
for (const command of collections.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()) {
|
|
|
|
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]);
|
|
|
|
}
|
|
|
|
exists.push(command);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const { command } of counts.rows) {
|
|
|
|
if (!exists.includes(command)) {
|
|
|
|
await connection.query("DELETE FROM counts WHERE command = $1", [command]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-12-19 00:57:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.stop = async () => {
|
|
|
|
await connection.end();
|
2020-12-19 00:50:25 +00:00
|
|
|
};
|