Migrated database to PostgreSQL, updated packages

This commit is contained in:
TheEssem 2020-07-10 12:07:24 -05:00
parent 0760136ded
commit 2a67b76169
15 changed files with 362 additions and 570 deletions

View file

@ -6,12 +6,5 @@ const client = require("../utils/client.js");
// run when the bot is added to a guild
module.exports = async (guild) => {
logger.log("info", `[GUILD JOIN] ${guild.name} (${guild.id}) added the bot. Owner: ${client.users.get(guild.ownerID).username}#${client.users.get(guild.ownerID).discriminator} (${guild.ownerID})`);
const guildDB = new db.guilds({
id: guild.id,
tags: misc.tagDefaults,
prefix: "&",
warns: {},
disabledChannels: []
});
await guildDB.save();
await db.query("INSERT INTO guilds (guild_id, tags, prefix, warns, disabled) VALUES ($1, $2, $3, $4, $5)", [guild.id, misc.tagDefaults, "&", {}, []]);
};

View file

@ -16,9 +16,9 @@ module.exports = async (message) => {
if (!message.channel.guild.members.get(client.user.id).permission.has("sendMessages") || !message.channel.permissionsOf(client.user.id).has("sendMessages")) return;
// prefix can be a mention or a set of special characters
const guildDB = await database.guilds.findOne({ id: message.channel.guild.id }).lean().exec();
const guildDB = await database.query("SELECT * FROM guilds WHERE guild_id = $1", [message.channel.guild.id]);
const prefixMention = new RegExp(`^<@!?${client.user.id}> `);
const prefix = prefixMention.test(message.content) ? message.content.match(prefixMention)[0] : guildDB.prefix;
const prefix = prefixMention.test(message.content) ? message.content.match(prefixMention)[0] : guildDB.rows[0].prefix;
// ignore other stuff
if (message.content.startsWith(prefix) === false) return;
@ -30,7 +30,7 @@ module.exports = async (message) => {
const command = args.shift().toLowerCase();
// don't run if message is in a disabled channel
if (guildDB.disabledChannels && guildDB.disabledChannels.includes(message.channel.id) && command != "channel") return;
if (guildDB.rows[0].disabled && guildDB.rows[0].disabled.includes(message.channel.id) && command != "channel") return;
// check if command exists
const cmd = collections.commands.get(command) || collections.commands.get(collections.aliases.get(command));
@ -39,10 +39,8 @@ module.exports = async (message) => {
// actually run the command
logger.log("info", `${message.author.username} (${message.author.id}) ran command ${command}`);
try {
const global = (await database.global.findOne({}).exec());
const count = global.cmdCounts.get(collections.aliases.has(command) ? collections.aliases.get(command) : command);
global.cmdCounts.set(collections.aliases.has(command) ? collections.aliases.get(command) : command, parseInt(count) + 1);
await global.save();
const count = await database.query("SELECT * FROM counts WHERE command = $1", [collections.aliases.has(command) ? collections.aliases.get(command) : command]);
await database.query("UPDATE counts SET count = $1 WHERE command = $2", [count.rows[0].count + 1, collections.aliases.has(command) ? collections.aliases.get(command) : command]);
const result = await cmd(message, args, content.replace(command, "").trim()); // we also provide the message content as a parameter for cases where we need more accuracy
if (typeof result === "string" || (typeof result === "object" && result.embed)) {
await client.createMessage(message.channel.id, result);

View file

@ -57,64 +57,41 @@ module.exports = async () => {
// make sure settings/tags exist
for (const [id] of client.guilds) {
const guildDB = (
await database.guilds
.findOne({
id: id,
})
.exec()
);
if (!guildDB) {
const guildDB = await database.query("SELECT * FROM guilds WHERE guild_id = $1", [id]);
if (guildDB.rows.length === 0) {
logger.log(`Registering guild database entry for guild ${id}...`);
const newGuild = new database.guilds({
id: id,
tags: misc.tagDefaults,
prefix: "&",
warns: {},
disabledChannels: []
});
await newGuild.save();
} else if (guildDB) {
if (!guildDB.warns) {
logger.log(`Creating warn object for guild ${id}...`);
guildDB.set("warns", {});
await guildDB.save();
} else if (!guildDB.disabledChannels) {
logger.log(`Creating disabled channels object for guild ${id}...`);
guildDB.set("disabledChannels", []);
await guildDB.save();
}
await database.query("INSERT INTO guilds (guild_id, tags, prefix, warns, disabled) VALUES ($1, $2, $3, $4, $5)", [id, misc.tagDefaults, "&", {}, []]);
}
}
const job = new cron.CronJob("0 0 * * 0", async () => {
logger.log("Deleting stale guild entries in database...");
const guildDB = (await database.guilds.find({}).exec());
for (const { id } of guildDB) {
if (!client.guilds.get(id)) {
await database.guilds.deleteMany({ id: id });
logger.log(`Deleted entry for guild ID ${id}.`);
const guildDB = await database.query("SELECT * FROM guilds");
for (const { guild_id } of guildDB.rows) {
if (!client.guilds.get(guild_id)) {
await database.query("DELETE FROM guilds WHERE guild_id = $1", [guild_id]);
logger.log(`Deleted entry for guild ID ${guild_id}.`);
}
}
logger.log("Finished deleting stale entries.");
});
job.start();
const global = (await database.global.findOne({}).exec());
if (!global) {
const countObject = {};
let counts;
try {
counts = await database.query("SELECT * FROM counts");
} catch {
counts = { rows: [] };
}
if (!counts.rows[0]) {
for (const command of collections.commands.keys()) {
countObject[command] = 0;
await database.query("INSERT INTO counts (command, count) VALUES ($1, $2)", [command, 0]);
}
const newGlobal = new database.global({
cmdCounts: countObject
});
await newGlobal.save();
} else {
for (const command of collections.commands.keys()) {
if (!global.cmdCounts.has(command)) {
global.cmdCounts.set(command, 0);
await global.save();
const count = await database.query("SELECT * FROM counts WHERE command = $1", [command]);
if (!count) {
await database.query("INSERT INTO counts (command, count) VALUES ($1, $2)", [command, 0]);
}
}
}
@ -134,14 +111,7 @@ module.exports = async () => {
if (twitter !== null && twitter.active === false) {
const blocks = await twitter.client.blocks.ids();
const tweet = async () => {
const tweets = (
await database.tweets
.find({
enabled: true,
})
.exec()
)[0];
const tweetContent = await misc.getTweet(tweets);
const tweetContent = await misc.getTweet(twitter.tweets);
try {
const info = await twitter.client.statuses.update(tweetContent);
logger.log(`Tweet with id ${info.id_str} has been posted.`);
@ -165,24 +135,11 @@ module.exports = async () => {
tweet.user.screen_name !== "esmBot_" &&
!blocks.ids.includes(tweet.user.id_str)
) {
const tweets = (
await database.tweets
.find({
enabled: true,
})
.exec()
)[0];
let tweetContent;
if (
tweet.text.includes("@this_vid") ||
tweet.text.includes("@DownloaderBot") ||
tweet.text.includes("@GetVideoBot") ||
tweet.text.includes("@DownloaderB0t") ||
tweet.text.includes("@thisvid_")
) {
tweetContent = await misc.getTweet(tweet, true, true);
if (new RegExp(["@this_vid", "@DownloaderBot", "GetVideoBot", "@thisvid_"].join("|")).test(tweet.text)) {
tweetContent = await misc.getTweet(twitter.tweets, true, true);
} else {
tweetContent = await misc.getTweet(tweets, true).replace(/{{user}}/gm, `@${tweet.user.screen_name}`);
tweetContent = await misc.getTweet(twitter.tweets, true).replace(/{{user}}/gm, `@${tweet.user.screen_name}`);
}
const payload = {
status: `@${tweet.user.screen_name} ${tweetContent}`,