13.1.6 mongodb failed connect limiter
This commit is contained in:
4 files changed
+61
-9
No files matched your search
+48
-8
@@ -25,6 +25,7 @@ class QuarksBot extends Client {
|
||||
this.db;
|
||||
this.dbo;
|
||||
this.connectMongo(this.config.mongoURI, this.config.dbo);
|
||||
this.databaseConnected = false;
|
||||
this.LoadCommandsAndInteractionHandlers();
|
||||
this.LoadEvents();
|
||||
|
||||
@@ -52,6 +53,15 @@ class QuarksBot extends Client {
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
if (!this.databaseConnected) {
|
||||
let dbFailedEmbed = new EmbedBuilder()
|
||||
.setDescription(`**Internal Error:**\nUh Oh D: Its not you, its me.\nThe bot has failed to connect to the database 5 times!\nContact the Developers\nhttps://discord.gg/YCXhvy9uZw`)
|
||||
.setColor(this.config.Colors.Red)
|
||||
|
||||
return interaction.send({ embeds: [dbFailedEmbed] });
|
||||
}
|
||||
|
||||
let cmd = client.commands.get(command);
|
||||
try {
|
||||
cmd.SlashCommand.run(this, interaction, args, { GuildDB }, start); // start is only used in ping / stats command
|
||||
@@ -65,16 +75,43 @@ class QuarksBot extends Client {
|
||||
}
|
||||
|
||||
async connectMongo(mongoURI, dbo) {
|
||||
let failed = false;
|
||||
|
||||
let dbLogDir = path.join(__dirname, '..', 'logs', 'database-logs.json');
|
||||
let databaselogs;
|
||||
try {
|
||||
// Connect to MongoDB database.
|
||||
this.db = await MongoClient.connect(mongoURI, {connectTimeoutMS: 5000});
|
||||
databaselogs = JSON.parse(fs.readFileSync(dbLogDir));
|
||||
} catch (err) {
|
||||
databaselogs = {
|
||||
attempts: 0,
|
||||
connected: false,
|
||||
};
|
||||
}
|
||||
|
||||
if (databaselogs.attempts >= 5) {
|
||||
this.error('Failed to connect to mongodb after multiple attempts');
|
||||
return; // prevent further attempts
|
||||
}
|
||||
|
||||
try {
|
||||
// Connect to Mongo database.
|
||||
this.db = await MongoClient.connect(mongoURI, {connectTimeoutMS: 1000});
|
||||
this.dbo = this.db.db(dbo);
|
||||
mongoose.connect(`${mongoURI}/${dbo}`);
|
||||
this.log('Successfully connected to mongoDB');
|
||||
databaselogs.connected = true;
|
||||
databaselogs.attempts = 0; // reset attempts
|
||||
this.databaseConnected = true;
|
||||
} catch (err) {
|
||||
this.error('Failed to connect to mongodb');
|
||||
process.exit(-1);
|
||||
databaselogs.attempts++;
|
||||
this.error(`Failed to connect to mongodb: attempt ${databaselogs.attempts}`);
|
||||
failed = true;
|
||||
}
|
||||
|
||||
// write JSON string to a file
|
||||
await fs.writeFileSync(dbLogDir, JSON.stringify(databaselogs));
|
||||
|
||||
if (failed) process.exit(-1);
|
||||
}
|
||||
|
||||
exists(n) {return null != n && undefined != n && "" != n}
|
||||
@@ -155,7 +192,8 @@ class QuarksBot extends Client {
|
||||
}
|
||||
|
||||
async GetGuild(GuildId) {
|
||||
let guild = await this.dbo.collection("guilds").findOne({"server.serverID":GuildId}).then(guild => guild);
|
||||
let guild = undefined;
|
||||
if (this.databaseConnected) guild = await this.dbo.collection("guilds").findOne({"server.serverID":GuildId}).then(guild => guild);
|
||||
|
||||
// If guild not found, generate guild default
|
||||
if (!guild) {
|
||||
@@ -178,9 +216,11 @@ class QuarksBot extends Client {
|
||||
canIgnoreInvoices: true,
|
||||
}
|
||||
}
|
||||
this.dbo.collection("guilds").insertOne(guild, function(err, res) {
|
||||
if (err) throw err;
|
||||
});
|
||||
if (this.databaseConnected) {
|
||||
this.dbo.collection("guilds").insertOne(guild, function(err, res) {
|
||||
if (err) throw err;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
let guildData = {
|
||||
|
||||
Reference in new issue
Block a user