Changed DB config settings
This commit is contained in:
parent
3594c4d353
commit
d47c7cacbb
3 changed files with 36 additions and 42 deletions
15
.env.example
15
.env.example
|
@ -9,16 +9,13 @@ NODE_ENV=development
|
||||||
TOKEN=
|
TOKEN=
|
||||||
|
|
||||||
# Put database type here (mongo or postgres)
|
# Put database type here (mongo or postgres)
|
||||||
DB=mongo
|
DB_DRIVER=mongo
|
||||||
|
|
||||||
# If you're using MongoDB, put the database URL here
|
# Put the database connection URL here
|
||||||
MONGO=mongodb://localhost:27017/esmBot
|
# Example for PostgreSQL:
|
||||||
|
# DB=postgresql://esmbot@localhost:5432/esmbot
|
||||||
# If you're using Postgres, fill in the needed info here
|
# Example for MongoDB:
|
||||||
PG_USER=esmbot
|
DB=mongodb://localhost:27017/esmBot
|
||||||
PG_HOST=localhost
|
|
||||||
PG_DB=esmbot
|
|
||||||
PG_PORT=5432
|
|
||||||
|
|
||||||
# Put snowflake ID of bot owner here
|
# Put snowflake ID of bot owner here
|
||||||
OWNER=
|
OWNER=
|
||||||
|
|
4
app.js
4
app.js
|
@ -60,11 +60,11 @@ async function init() {
|
||||||
}
|
}
|
||||||
client.disconnect();
|
client.disconnect();
|
||||||
const db = require("./utils/database.js");
|
const db = require("./utils/database.js");
|
||||||
if (process.env.DB === "mongo") {
|
if (process.env.DB_DRIVER=== "mongo") {
|
||||||
db.connection.close(() => {
|
db.connection.close(() => {
|
||||||
process.exit(0);
|
process.exit(0);
|
||||||
});
|
});
|
||||||
} else if (process.env.DB === "postgres") {
|
} else if (process.env.DB_DRIVER=== "postgres") {
|
||||||
db.connection.end();
|
db.connection.end();
|
||||||
process.exit(0);
|
process.exit(0);
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,9 +3,9 @@ const logger = require("./logger.js");
|
||||||
const collections = require("../utils/collections.js");
|
const collections = require("../utils/collections.js");
|
||||||
const misc = require("./misc.js");
|
const misc = require("./misc.js");
|
||||||
|
|
||||||
if (process.env.DB === "mongo") {
|
if (process.env.DB_DRIVER=== "mongo") {
|
||||||
const mongoose = require("mongoose");
|
const mongoose = require("mongoose");
|
||||||
mongoose.connect(process.env.MONGO, {
|
mongoose.connect(process.env.DB, {
|
||||||
poolSize: 10,
|
poolSize: 10,
|
||||||
bufferMaxEntries: 0,
|
bufferMaxEntries: 0,
|
||||||
useNewUrlParser: true,
|
useNewUrlParser: true,
|
||||||
|
@ -28,43 +28,40 @@ if (process.env.DB === "mongo") {
|
||||||
exports.guilds = Guild;
|
exports.guilds = Guild;
|
||||||
exports.global = Global;
|
exports.global = Global;
|
||||||
exports.connection = mongoose.connection;
|
exports.connection = mongoose.connection;
|
||||||
} else if (process.env.DB === "postgres") {
|
} else if (process.env.DB_DRIVER=== "postgres") {
|
||||||
const { Pool } = require("pg");
|
const { Pool } = require("pg");
|
||||||
const pool = new Pool({
|
const pool = new Pool({
|
||||||
user: process.env.PG_USER,
|
connectionString: process.env.DB
|
||||||
host: process.env.PG_HOST,
|
|
||||||
database: process.env.PG_DB,
|
|
||||||
port: process.env.PG_PORT
|
|
||||||
});
|
});
|
||||||
exports.connection = pool;
|
exports.connection = pool;
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.getGuild = async (query) => {
|
exports.getGuild = async (query) => {
|
||||||
if (process.env.DB === "mongo") {
|
if (process.env.DB_DRIVER=== "mongo") {
|
||||||
return await this.guilds.findOne({ id: query });
|
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];
|
return (await this.connection.query("SELECT * FROM guilds WHERE guild_id = $1", [query])).rows[0];
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.setPrefix = async (prefix, guild) => {
|
exports.setPrefix = async (prefix, guild) => {
|
||||||
if (process.env.DB === "mongo") {
|
if (process.env.DB_DRIVER=== "mongo") {
|
||||||
const guildDB = await this.getGuild(guild.id);
|
const guildDB = await this.getGuild(guild.id);
|
||||||
guildDB.prefix = prefix;
|
guildDB.prefix = prefix;
|
||||||
await guildDB.save();
|
await guildDB.save();
|
||||||
collections.prefixCache.set(guild.id, prefix);
|
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]);
|
await this.connection.query("UPDATE guilds SET prefix = $1 WHERE guild_id = $2", [prefix, guild.id]);
|
||||||
collections.prefixCache.set(guild.id, prefix);
|
collections.prefixCache.set(guild.id, prefix);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.setTag = async (name, content, guild) => {
|
exports.setTag = async (name, content, guild) => {
|
||||||
if (process.env.DB === "mongo") {
|
if (process.env.DB_DRIVER=== "mongo") {
|
||||||
const guildDB = await this.getGuild(guild.id);
|
const guildDB = await this.getGuild(guild.id);
|
||||||
guildDB.tags[name] = content;
|
guildDB.tags[name] = content;
|
||||||
await guildDB.save();
|
await guildDB.save();
|
||||||
} else if (process.env.DB === "postgres") {
|
} else if (process.env.DB_DRIVER=== "postgres") {
|
||||||
const guildDB = await this.getGuild(guild.id);
|
const guildDB = await this.getGuild(guild.id);
|
||||||
guildDB.tags[name] = content;
|
guildDB.tags[name] = content;
|
||||||
await this.connection.query("UPDATE guilds SET tags = $1 WHERE guild_id = $2", [guildDB.tags, guild.id]);
|
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) => {
|
exports.removeTag = async (name, guild) => {
|
||||||
if (process.env.DB === "mongo") {
|
if (process.env.DB_DRIVER=== "mongo") {
|
||||||
const guildDB = await this.getGuild(guild.id);
|
const guildDB = await this.getGuild(guild.id);
|
||||||
delete guildDB.tags[name];
|
delete guildDB.tags[name];
|
||||||
await guildDB.save();
|
await guildDB.save();
|
||||||
} else if (process.env.DB === "postgres") {
|
} else if (process.env.DB_DRIVER=== "postgres") {
|
||||||
const guildDB = await this.getGuild(guild.id);
|
const guildDB = await this.getGuild(guild.id);
|
||||||
delete guildDB.tags[name];
|
delete guildDB.tags[name];
|
||||||
await this.connection.query("UPDATE guilds SET tags = $1 WHERE guild_id = $2", [guildDB.tags, guild.id]);
|
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) => {
|
exports.toggleTags = async (guild) => {
|
||||||
if (process.env.DB === "mongo") {
|
if (process.env.DB_DRIVER=== "mongo") {
|
||||||
const guildDB = await this.getGuild(guild.id);
|
const guildDB = await this.getGuild(guild.id);
|
||||||
guildDB.tagsDisabled = !guildDB.tagsDisabled;
|
guildDB.tagsDisabled = !guildDB.tagsDisabled;
|
||||||
await guildDB.save();
|
await guildDB.save();
|
||||||
return guildDB.tagsDisabled;
|
return guildDB.tagsDisabled;
|
||||||
} else if (process.env.DB === "postgres") {
|
} else if (process.env.DB_DRIVER=== "postgres") {
|
||||||
const guildDB = await this.getGuild(guild.id);
|
const guildDB = await this.getGuild(guild.id);
|
||||||
guildDB.tags_disabled = !guildDB.tags_disabled;
|
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]);
|
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) => {
|
exports.disableChannel = async (channel) => {
|
||||||
if (process.env.DB === "mongo") {
|
if (process.env.DB_DRIVER=== "mongo") {
|
||||||
const guildDB = await this.getGuild(channel.guild.id);
|
const guildDB = await this.getGuild(channel.guild.id);
|
||||||
guildDB.disabled.push(channel.id);
|
guildDB.disabled.push(channel.id);
|
||||||
await guildDB.save();
|
await guildDB.save();
|
||||||
collections.disabledCache.set(channel.guild.id, guildDB.disabled);
|
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 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]);
|
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);
|
collections.disabledCache.set(channel.guild.id, guildDB.disabled);
|
||||||
|
@ -111,12 +108,12 @@ exports.disableChannel = async (channel) => {
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.enableChannel = 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);
|
const guildDB = await this.getGuild(channel.guild.id);
|
||||||
guildDB.disabled = guildDB.disabled.filter(item => item !== channel.id);
|
guildDB.disabled = guildDB.disabled.filter(item => item !== channel.id);
|
||||||
await guildDB.save();
|
await guildDB.save();
|
||||||
collections.disabledCache.set(channel.guild.id, guildDB.disabled);
|
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 guildDB = await this.getGuild(channel.guild.id);
|
||||||
const newDisabled = guildDB.disabled.filter(item => item !== channel.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]);
|
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 () => {
|
exports.getCounts = async () => {
|
||||||
if (process.env.DB === "mongo") {
|
if (process.env.DB_DRIVER=== "mongo") {
|
||||||
return [...(await this.global.findOne({})).cmdCounts.entries()];
|
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 counts = await this.connection.query("SELECT * FROM counts");
|
||||||
const countArray = [];
|
const countArray = [];
|
||||||
for (const { command, count } of counts.rows) {
|
for (const { command, count } of counts.rows) {
|
||||||
|
@ -138,19 +135,19 @@ exports.getCounts = async () => {
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.addCount = async (command) => {
|
exports.addCount = async (command) => {
|
||||||
if (process.env.DB === "mongo") {
|
if (process.env.DB_DRIVER=== "mongo") {
|
||||||
const global = await this.global.findOne({});
|
const global = await this.global.findOne({});
|
||||||
const count = global.cmdCounts.get(command);
|
const count = global.cmdCounts.get(command);
|
||||||
global.cmdCounts.set(command, parseInt(count) + 1);
|
global.cmdCounts.set(command, parseInt(count) + 1);
|
||||||
await global.save();
|
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]);
|
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]);
|
await this.connection.query("UPDATE counts SET count = $1 WHERE command = $2", [count.rows[0].count + 1, command]);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.addGuild = async (guild) => {
|
exports.addGuild = async (guild) => {
|
||||||
if (process.env.DB === "mongo") {
|
if (process.env.DB_DRIVER=== "mongo") {
|
||||||
const guildDB = new this.guilds({
|
const guildDB = new this.guilds({
|
||||||
id: guild.id,
|
id: guild.id,
|
||||||
tags: misc.tagDefaults,
|
tags: misc.tagDefaults,
|
||||||
|
@ -160,14 +157,14 @@ exports.addGuild = async (guild) => {
|
||||||
});
|
});
|
||||||
await guildDB.save();
|
await guildDB.save();
|
||||||
return guildDB;
|
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]);
|
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);
|
return await this.getGuild(guild.id);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.fixGuild = async (guild) => {
|
exports.fixGuild = async (guild) => {
|
||||||
if (process.env.DB === "mongo") {
|
if (process.env.DB_DRIVER=== "mongo") {
|
||||||
const guildDB = await this.guilds.findOne({ id: guild.id });
|
const guildDB = await this.guilds.findOne({ id: guild.id });
|
||||||
if (!guildDB) {
|
if (!guildDB) {
|
||||||
logger.log(`Registering guild database entry for guild ${guild.id}...`);
|
logger.log(`Registering guild database entry for guild ${guild.id}...`);
|
||||||
|
@ -180,7 +177,7 @@ exports.fixGuild = async (guild) => {
|
||||||
return guildDB;
|
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]);
|
const guildDB = await this.connection.query("SELECT * FROM guilds WHERE guild_id = $1", [guild.id]);
|
||||||
if (guildDB.rows.length === 0) {
|
if (guildDB.rows.length === 0) {
|
||||||
logger.log(`Registering guild database entry for guild ${guild.id}...`);
|
logger.log(`Registering guild database entry for guild ${guild.id}...`);
|
||||||
|
@ -190,7 +187,7 @@ exports.fixGuild = async (guild) => {
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.handleCounts = async () => {
|
exports.handleCounts = async () => {
|
||||||
if (process.env.DB === "mongo") {
|
if (process.env.DB_DRIVER=== "mongo") {
|
||||||
const global = await this.global.findOne({});
|
const global = await this.global.findOne({});
|
||||||
if (!global) {
|
if (!global) {
|
||||||
const countObject = {};
|
const countObject = {};
|
||||||
|
@ -217,7 +214,7 @@ exports.handleCounts = async () => {
|
||||||
}
|
}
|
||||||
await global.save();
|
await global.save();
|
||||||
}
|
}
|
||||||
} else if (process.env.DB === "postgres") {
|
} else if (process.env.DB_DRIVER=== "postgres") {
|
||||||
let counts;
|
let counts;
|
||||||
try {
|
try {
|
||||||
counts = await this.connection.query("SELECT * FROM counts");
|
counts = await this.connection.query("SELECT * FROM counts");
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue