Changed DB config settings

This commit is contained in:
TheEssem 2020-12-18 16:05:43 -06:00
parent 3594c4d353
commit d47c7cacbb
3 changed files with 36 additions and 42 deletions

View File

@ -9,16 +9,13 @@ NODE_ENV=development
TOKEN=
# Put database type here (mongo or postgres)
DB=mongo
DB_DRIVER=mongo
# If you're using MongoDB, put the database URL here
MONGO=mongodb://localhost:27017/esmBot
# If you're using Postgres, fill in the needed info here
PG_USER=esmbot
PG_HOST=localhost
PG_DB=esmbot
PG_PORT=5432
# Put the database connection URL here
# Example for PostgreSQL:
# DB=postgresql://esmbot@localhost:5432/esmbot
# Example for MongoDB:
DB=mongodb://localhost:27017/esmBot
# Put snowflake ID of bot owner here
OWNER=

4
app.js
View File

@ -60,11 +60,11 @@ async function init() {
}
client.disconnect();
const db = require("./utils/database.js");
if (process.env.DB === "mongo") {
if (process.env.DB_DRIVER=== "mongo") {
db.connection.close(() => {
process.exit(0);
});
} else if (process.env.DB === "postgres") {
} else if (process.env.DB_DRIVER=== "postgres") {
db.connection.end();
process.exit(0);
}

View File

@ -3,9 +3,9 @@ const logger = require("./logger.js");
const collections = require("../utils/collections.js");
const misc = require("./misc.js");
if (process.env.DB === "mongo") {
if (process.env.DB_DRIVER=== "mongo") {
const mongoose = require("mongoose");
mongoose.connect(process.env.MONGO, {
mongoose.connect(process.env.DB, {
poolSize: 10,
bufferMaxEntries: 0,
useNewUrlParser: true,
@ -28,43 +28,40 @@ if (process.env.DB === "mongo") {
exports.guilds = Guild;
exports.global = Global;
exports.connection = mongoose.connection;
} else if (process.env.DB === "postgres") {
} else if (process.env.DB_DRIVER=== "postgres") {
const { Pool } = require("pg");
const pool = new Pool({
user: process.env.PG_USER,
host: process.env.PG_HOST,
database: process.env.PG_DB,
port: process.env.PG_PORT
connectionString: process.env.DB
});
exports.connection = pool;
}
exports.getGuild = async (query) => {
if (process.env.DB === "mongo") {
if (process.env.DB_DRIVER=== "mongo") {
return await this.guilds.findOne({ id: query });
} else if (process.env.DB === "postgres") {
} else if (process.env.DB_DRIVER=== "postgres") {
return (await this.connection.query("SELECT * FROM guilds WHERE guild_id = $1", [query])).rows[0];
}
};
exports.setPrefix = async (prefix, guild) => {
if (process.env.DB === "mongo") {
if (process.env.DB_DRIVER=== "mongo") {
const guildDB = await this.getGuild(guild.id);
guildDB.prefix = prefix;
await guildDB.save();
collections.prefixCache.set(guild.id, prefix);
} else if (process.env.DB === "postgres") {
} else if (process.env.DB_DRIVER=== "postgres") {
await this.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) => {
if (process.env.DB === "mongo") {
if (process.env.DB_DRIVER=== "mongo") {
const guildDB = await this.getGuild(guild.id);
guildDB.tags[name] = content;
await guildDB.save();
} else if (process.env.DB === "postgres") {
} else if (process.env.DB_DRIVER=== "postgres") {
const guildDB = await this.getGuild(guild.id);
guildDB.tags[name] = content;
await this.connection.query("UPDATE guilds SET tags = $1 WHERE guild_id = $2", [guildDB.tags, guild.id]);
@ -72,11 +69,11 @@ exports.setTag = async (name, content, guild) => {
};
exports.removeTag = async (name, guild) => {
if (process.env.DB === "mongo") {
if (process.env.DB_DRIVER=== "mongo") {
const guildDB = await this.getGuild(guild.id);
delete guildDB.tags[name];
await guildDB.save();
} else if (process.env.DB === "postgres") {
} else if (process.env.DB_DRIVER=== "postgres") {
const guildDB = await this.getGuild(guild.id);
delete guildDB.tags[name];
await this.connection.query("UPDATE guilds SET tags = $1 WHERE guild_id = $2", [guildDB.tags, guild.id]);
@ -84,12 +81,12 @@ exports.removeTag = async (name, guild) => {
};
exports.toggleTags = async (guild) => {
if (process.env.DB === "mongo") {
if (process.env.DB_DRIVER=== "mongo") {
const guildDB = await this.getGuild(guild.id);
guildDB.tagsDisabled = !guildDB.tagsDisabled;
await guildDB.save();
return guildDB.tagsDisabled;
} else if (process.env.DB === "postgres") {
} else if (process.env.DB_DRIVER=== "postgres") {
const guildDB = await this.getGuild(guild.id);
guildDB.tags_disabled = !guildDB.tags_disabled;
await this.connection.query("UPDATE guilds SET tags_disabled = $1 WHERE guild_id = $2", [guildDB.tags_disabled, guild.id]);
@ -98,12 +95,12 @@ exports.toggleTags = async (guild) => {
};
exports.disableChannel = async (channel) => {
if (process.env.DB === "mongo") {
if (process.env.DB_DRIVER=== "mongo") {
const guildDB = await this.getGuild(channel.guild.id);
guildDB.disabled.push(channel.id);
await guildDB.save();
collections.disabledCache.set(channel.guild.id, guildDB.disabled);
} else if (process.env.DB === "postgres") {
} else if (process.env.DB_DRIVER=== "postgres") {
const guildDB = await this.getGuild(channel.guild.id);
await this.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);
@ -111,12 +108,12 @@ exports.disableChannel = async (channel) => {
};
exports.enableChannel = async (channel) => {
if (process.env.DB === "mongo") {
if (process.env.DB_DRIVER=== "mongo") {
const guildDB = await this.getGuild(channel.guild.id);
guildDB.disabled = guildDB.disabled.filter(item => item !== channel.id);
await guildDB.save();
collections.disabledCache.set(channel.guild.id, guildDB.disabled);
} else if (process.env.DB === "postgres") {
} else if (process.env.DB_DRIVER=== "postgres") {
const guildDB = await this.getGuild(channel.guild.id);
const newDisabled = guildDB.disabled.filter(item => item !== channel.id);
await this.connection.query("UPDATE guilds SET disabled = $1 WHERE guild_id = $2", [newDisabled, channel.guild.id]);
@ -125,9 +122,9 @@ exports.enableChannel = async (channel) => {
};
exports.getCounts = async () => {
if (process.env.DB === "mongo") {
if (process.env.DB_DRIVER=== "mongo") {
return [...(await this.global.findOne({})).cmdCounts.entries()];
} else if (process.env.DB === "postgres") {
} else if (process.env.DB_DRIVER=== "postgres") {
const counts = await this.connection.query("SELECT * FROM counts");
const countArray = [];
for (const { command, count } of counts.rows) {
@ -138,19 +135,19 @@ exports.getCounts = async () => {
};
exports.addCount = async (command) => {
if (process.env.DB === "mongo") {
if (process.env.DB_DRIVER=== "mongo") {
const global = await this.global.findOne({});
const count = global.cmdCounts.get(command);
global.cmdCounts.set(command, parseInt(count) + 1);
await global.save();
} else if (process.env.DB === "postgres") {
} else if (process.env.DB_DRIVER=== "postgres") {
const count = await this.connection.query("SELECT * FROM counts WHERE command = $1", [command]);
await this.connection.query("UPDATE counts SET count = $1 WHERE command = $2", [count.rows[0].count + 1, command]);
}
};
exports.addGuild = async (guild) => {
if (process.env.DB === "mongo") {
if (process.env.DB_DRIVER=== "mongo") {
const guildDB = new this.guilds({
id: guild.id,
tags: misc.tagDefaults,
@ -160,14 +157,14 @@ exports.addGuild = async (guild) => {
});
await guildDB.save();
return guildDB;
} else if (process.env.DB === "postgres") {
} else if (process.env.DB_DRIVER=== "postgres") {
await this.connection.query("INSERT INTO guilds (guild_id, tags, prefix, warns, disabled, tags_disabled) VALUES ($1, $2, $3, $4, $5, $6)", [guild.id, misc.tagDefaults, process.env.PREFIX, {}, [], false]);
return await this.getGuild(guild.id);
}
};
exports.fixGuild = async (guild) => {
if (process.env.DB === "mongo") {
if (process.env.DB_DRIVER=== "mongo") {
const guildDB = await this.guilds.findOne({ id: guild.id });
if (!guildDB) {
logger.log(`Registering guild database entry for guild ${guild.id}...`);
@ -180,7 +177,7 @@ exports.fixGuild = async (guild) => {
return guildDB;
}
}
} else if (process.env.DB === "postgres") {
} else if (process.env.DB_DRIVER=== "postgres") {
const guildDB = await this.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}...`);
@ -190,7 +187,7 @@ exports.fixGuild = async (guild) => {
};
exports.handleCounts = async () => {
if (process.env.DB === "mongo") {
if (process.env.DB_DRIVER=== "mongo") {
const global = await this.global.findOne({});
if (!global) {
const countObject = {};
@ -217,7 +214,7 @@ exports.handleCounts = async () => {
}
await global.save();
}
} else if (process.env.DB === "postgres") {
} else if (process.env.DB_DRIVER=== "postgres") {
let counts;
try {
counts = await this.connection.query("SELECT * FROM counts");