mirror of
https://github.com/ION606/selmerBot.git
synced 2026-05-14 21:26:54 +00:00
Removed the excessive connecting and closing of the MongoDb databases
This commit is contained in:
+8
-11
@@ -18,19 +18,18 @@ async function execute(bot, message, args, command, Discord, mongouri, items, xp
|
||||
return message.reply('Only the server owner can do this!');
|
||||
}
|
||||
|
||||
// @ts-ignore
|
||||
const client = new MongoClient(mongouri, { useNewUrlParser: true, useUnifiedTopology: true, serverApi: ServerApiVersion.v1 });
|
||||
if (client.writeConcern || client.writeConcern) {
|
||||
client.close();
|
||||
return message.reply("Something went wrong with the database, please try again later and contact support if this problem persists!");
|
||||
}
|
||||
// // @ts-ignore
|
||||
// const client = new MongoClient(mongouri, { useNewUrlParser: true, useUnifiedTopology: true, serverApi: ServerApiVersion.v1 });
|
||||
// if (client.writeConcern || client.writeConcern) {
|
||||
// client.close();
|
||||
// return message.reply("Something went wrong with the database, please try again later and contact support if this problem persists!");
|
||||
// }
|
||||
|
||||
bot.mongoconnection.then(async (client) => {
|
||||
// if (err) { return console.log(err); }
|
||||
//Initialize
|
||||
CreateNewCollection(message, client, server, owner.user.id);
|
||||
|
||||
client.connect(err => {
|
||||
if (err) { return console.log(err); }
|
||||
|
||||
const db = client.db(server);
|
||||
const dbo = db.collection('SETUP');
|
||||
|
||||
@@ -94,8 +93,6 @@ async function execute(bot, message, args, command, Discord, mongouri, items, xp
|
||||
message.reply(temp);
|
||||
}
|
||||
});
|
||||
|
||||
client.close();
|
||||
}
|
||||
|
||||
|
||||
|
||||
+10
-14
@@ -32,10 +32,9 @@ function isNum(arg) {
|
||||
|
||||
|
||||
function CreateNewCollection(message, client, server, id, opponent = null, game = null) {
|
||||
client.connect(err => {
|
||||
const db = client.db(String(server));
|
||||
const dbo = db.collection(id);
|
||||
if (err) { return console.log(err); }
|
||||
|
||||
db.listCollections({name: id})
|
||||
.next(function(err, collinfo) {
|
||||
if (err) { return console.log(err); }
|
||||
@@ -45,9 +44,6 @@ function CreateNewCollection(message, client, server, id, opponent = null, game
|
||||
dbo.insertOne({balance: 10, rank: 1, lastdayworked: 0, xp: 0, hpmp: hp_mp, game: game, gamesettings: {battle: {class: 'none', ultimate: true}}, opponent: opponent, state: STATE.IDLE, equipped: { weapons: {main: null, secondary: null}, items: {}}});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
client.close();
|
||||
}
|
||||
|
||||
|
||||
@@ -303,15 +299,18 @@ module.exports = {
|
||||
const id = message.author.id;
|
||||
const server = message.guild.id;
|
||||
|
||||
const client = new MongoClient(mongouri, { useNewUrlParser: true, useUnifiedTopology: true, serverApi: ServerApiVersion.v1 });
|
||||
if (client.writeConcern || client.writeConcern) {
|
||||
client.close();
|
||||
return message.reply("Something went wrong with the database, please try again later and contact support if this problem persists!");
|
||||
}
|
||||
// const client = new MongoClient(mongouri, { useNewUrlParser: true, useUnifiedTopology: true, serverApi: ServerApiVersion.v1 });
|
||||
// if (client.writeConcern || client.writeConcern) {
|
||||
// client.close();
|
||||
// return message.reply("Something went wrong with the database, please try again later and contact support if this problem persists!");
|
||||
// }
|
||||
|
||||
|
||||
bot.mongoconnection.then(async (client) => {
|
||||
|
||||
//Initialize if necessary
|
||||
CreateNewCollection(message, client, server, id);
|
||||
|
||||
client.connect(err => {
|
||||
const db = client.db(String(server));
|
||||
const dbo = db.collection(id);
|
||||
if (err) { return console.log(err); }
|
||||
@@ -352,9 +351,6 @@ module.exports = {
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
//Close the database
|
||||
client.close();
|
||||
},
|
||||
|
||||
//Battle Updating stuff
|
||||
|
||||
@@ -8,9 +8,8 @@ function handle_dm(message, bot) {
|
||||
if (!message.content.startsWith('!') || message.content.split(' ')[0] == '!startconvo' || message.content.split(' ')[0] == '!endconvo') {
|
||||
const member = bot.guilds.cache.get(bot.home_server).members.cache.get(message.author.id);
|
||||
|
||||
const client = new MongoClient(bot.mongouri, { useNewUrlParser: true, useUnifiedTopology: true, serverApi: ServerApiVersion.v1 });
|
||||
client.connect(async (err) => {
|
||||
if (err) { return console.log(err); }
|
||||
bot.mongoconnection.then(async (client) => {
|
||||
// if (err) { return console.log(err); }
|
||||
|
||||
const dbo = client.db('main').collection('authorized');
|
||||
dbo.find({id: message.author}).toArray((err, docs) => {
|
||||
@@ -24,7 +23,6 @@ function handle_dm(message, bot) {
|
||||
});
|
||||
});
|
||||
|
||||
client.close();
|
||||
} else if (message.content.indexOf('!premium') != -1) {
|
||||
handleInp(bot, message);
|
||||
} else {
|
||||
|
||||
+10
-12
@@ -220,12 +220,16 @@ module.exports ={
|
||||
const id = message.author.id;
|
||||
const server = message.guild.id;
|
||||
|
||||
// @ts-ignore
|
||||
const client = new MongoClient(mongouri, { useNewUrlParser: true, useUnifiedTopology: true, serverApi: ServerApiVersion.v1 });
|
||||
if (client.writeConcern || client.writeConcern) {
|
||||
client.close();
|
||||
return message.reply("Something went wrong with the database, please try again later and contact support if this problem persists!");
|
||||
}
|
||||
// // @ts-ignore
|
||||
// const client = new MongoClient(mongouri, { useNewUrlParser: true, useUnifiedTopology: true, serverApi: ServerApiVersion.v1 });
|
||||
// if (client.writeConcern || client.writeConcern) {
|
||||
// client.close();
|
||||
// return message.reply("Something went wrong with the database, please try again later and contact support if this problem persists!");
|
||||
// }
|
||||
var client;
|
||||
await bot.mongoconnection.then((client1) => {
|
||||
client = client1;
|
||||
});
|
||||
|
||||
const botdb = client.db('B|S' + bot.user.id);
|
||||
const serverinbotdb = botdb.collection(server);
|
||||
@@ -240,11 +244,8 @@ module.exports ={
|
||||
}
|
||||
|
||||
//#endregion
|
||||
|
||||
client.connect(err => {
|
||||
const db = client.db(String(server));
|
||||
const dbo = db.collection(id);
|
||||
if (err) { return console.log(err); }
|
||||
|
||||
//Check if the client is currently in a game and act accordingly
|
||||
//#region Check Game
|
||||
@@ -393,9 +394,6 @@ module.exports ={
|
||||
//#endregion
|
||||
|
||||
});
|
||||
});
|
||||
|
||||
client.close();
|
||||
}, allGames, in_game_redirector
|
||||
}
|
||||
|
||||
|
||||
@@ -14,8 +14,7 @@ for (i in categoriesJSON) {
|
||||
|
||||
function changeDB(bot, message, m) {
|
||||
try {
|
||||
const client = new MongoClient(bot.mongouri, { useNewUrlParser: true, useUnifiedTopology: true, serverApi: ServerApiVersion.v1 });
|
||||
client.connect(err => {
|
||||
bot.mongoconnection.then(client => {
|
||||
const dbo = client.db(message.guild.id).collection('trivia');
|
||||
//Game Over
|
||||
if (m == null) {
|
||||
|
||||
@@ -11,8 +11,7 @@ async function handle_interaction(interaction, mongouri, turnManager, bot, STATE
|
||||
const singleCommandGames = ['ttt']; // Use when you have more single-player games
|
||||
const musicCommandList = ['PLAY', 'PAUSE', 'RESUME', 'STOP', 'SKIP'];
|
||||
|
||||
const client = new MongoClient(mongouri, { useNewUrlParser: true, useUnifiedTopology: true, serverApi: ServerApiVersion.v1 });
|
||||
client.connect(async (err) => {
|
||||
bot.mongoconnection.then(async (client) => {
|
||||
|
||||
if (battlecommandlist.indexOf(interaction.customId) != -1) {
|
||||
await interaction.deferReply();
|
||||
@@ -75,8 +74,6 @@ async function handle_interaction(interaction, mongouri, turnManager, bot, STATE
|
||||
resolveComplaint(interaction);
|
||||
} //Button else ifs here
|
||||
});
|
||||
|
||||
client.close();
|
||||
}
|
||||
|
||||
//Menu Selection
|
||||
@@ -85,8 +82,7 @@ async function handle_interaction(interaction, mongouri, turnManager, bot, STATE
|
||||
// const command = interaction.customId.substring(interaction.customId.indexOf('|'), interaction.customId.length - interaction.customId.indexOf('|'))
|
||||
|
||||
if (interaction.customId.toLowerCase().indexOf('|heal') != -1) {
|
||||
const client = new MongoClient(mongouri, { useNewUrlParser: true, useUnifiedTopology: true, serverApi: ServerApiVersion.v1 });
|
||||
client.connect(err => {
|
||||
bot.mongoconnection.then(client => {
|
||||
console.log(id);
|
||||
if (id != interaction.user.id) { return; }
|
||||
|
||||
|
||||
+2
-3
@@ -14,9 +14,8 @@ const col_list = {0: '0ed300', 1: 'f6ff00', 2: 'ffa100', 3: 'FF0000'}
|
||||
* @param {*} message the message the mod sent (AKA a DISCORD MESSAGE OBJECT)
|
||||
*/
|
||||
function log(bot, message, command, mentioned, reason, severity) {
|
||||
const client = new MongoClient(bot.mongouri, { useNewUrlParser: true, useUnifiedTopology: true, serverApi: ServerApiVersion.v1 });
|
||||
client.connect(err => {
|
||||
if (err) { return console.log(err); }
|
||||
bot.mongoconnection.then(client => {
|
||||
// if (err) { return console.log(err); }
|
||||
|
||||
|
||||
client.db(message.guild.id).collection('SETUP').findOne({_id: 'LOG'}).then((doc) => {
|
||||
|
||||
+11
-12
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
-----WEBHOOKS ARE MONITORED AND PROCESSED HERE-----
|
||||
https://glitch.com/edit/#!/selmer-bot-listener
|
||||
https://selmer-bot-listener.ion606.repl.co
|
||||
--------------------------------------------------
|
||||
*/
|
||||
|
||||
@@ -17,17 +17,17 @@ async function createSubscriptionManual(bot, interaction, id, priceID) {
|
||||
//Error Checking (unlikely, but just in case)
|
||||
if (!id) { console.log('....What? How?'); return interaction.editReply("Uh oh, something happened with the Stripe Discord ID check, please contact support!"); }
|
||||
|
||||
const client = new MongoClient(mongouri, { useNewUrlParser: true, useUnifiedTopology: true, serverApi: ServerApiVersion.v1 });
|
||||
// const client = new MongoClient(mongouri, { useNewUrlParser: true, useUnifiedTopology: true, serverApi: ServerApiVersion.v1 });
|
||||
new Promise(async function(resolve, reject) {
|
||||
client.connect(async (err) => {
|
||||
if (err) { return console.log(err); }
|
||||
bot.mongoconnection.then(async (client) => {
|
||||
// if (err) { return console.log(err); }
|
||||
|
||||
const dbo = client.db('main').collection('authorized');
|
||||
await dbo.findOne({'discordID': id}).then(async (doc) => {
|
||||
var userID;
|
||||
|
||||
if (doc != undefined) {
|
||||
client.close();
|
||||
// client.close();
|
||||
|
||||
reject(`An account with the tag <@${id}> already exists!`);
|
||||
} else {
|
||||
@@ -37,7 +37,7 @@ async function createSubscriptionManual(bot, interaction, id, priceID) {
|
||||
userID = stripeUser.id;
|
||||
|
||||
//Add to the database (I have to wait for the insertion)
|
||||
await dbo.insertOne({stripeID: userID, discordID: id, paid: false, startDateUTC: null, tier: 0}).then(() => { client.close(); resolve(userID); });
|
||||
await dbo.insertOne({stripeID: userID, discordID: id, paid: false, startDateUTC: null, tier: 0}).then(() => { /*client.close();*/ resolve(userID); });
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -87,10 +87,10 @@ async function changeSubscriptionManual(bot, message) {
|
||||
//Just in case
|
||||
if (!id) { return console.log('....What? How?'); }
|
||||
|
||||
const client = new MongoClient(mongouri, { useNewUrlParser: true, useUnifiedTopology: true, serverApi: ServerApiVersion.v1 });
|
||||
// const client = new MongoClient(mongouri, { useNewUrlParser: true, useUnifiedTopology: true, serverApi: ServerApiVersion.v1 });
|
||||
new Promise(async function(resolve, reject) {
|
||||
client.connect(async (err) => {
|
||||
if (err) { return console.log(err); }
|
||||
bot.mongoconnection.then(async (client) => {
|
||||
// if (err) { return console.log(err); }
|
||||
|
||||
const dbo = client.db('main').collection('authorized');
|
||||
await dbo.findOne({'discordID': id}).then(async (doc) => {
|
||||
@@ -98,11 +98,10 @@ async function changeSubscriptionManual(bot, message) {
|
||||
|
||||
if (doc != undefined) {
|
||||
userID = doc.stripeID;
|
||||
client.close();
|
||||
// client.close();
|
||||
resolve(userID);
|
||||
} else {
|
||||
client.close();
|
||||
|
||||
// client.close();
|
||||
reject(`No user with the ID of <@${message.author.id}>`);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -94,6 +94,9 @@ if (process.env.MONGODB_URI) {
|
||||
}
|
||||
const mongouri = mongouritemp;
|
||||
bot.mongouri = mongouri;
|
||||
const client = new MongoClient(mongouri, { useNewUrlParser: true, useUnifiedTopology: true, serverApi: ServerApiVersion.v1 });
|
||||
bot.mongoconnection = client.connect();
|
||||
|
||||
const { connect } = require('mongoose');
|
||||
|
||||
//#endregion MongoDB Integration end
|
||||
@@ -148,15 +151,12 @@ let items;
|
||||
|
||||
bot.on('ready', async () => {
|
||||
//Make then copy the shop
|
||||
const client = new MongoClient(mongouri, { useNewUrlParser: true, useUnifiedTopology: true, serverApi: ServerApiVersion.v1 });
|
||||
client.connect(err => {
|
||||
bot.mongoconnection.then(client => {
|
||||
const shop = client.db("main").collection("shop");
|
||||
shop.find().toArray(function(err, itemstemp) {
|
||||
if (err) throw err;
|
||||
|
||||
items = [...itemstemp];
|
||||
|
||||
client.close();
|
||||
});
|
||||
|
||||
//Srt status and Activity (idle and listening to !help)
|
||||
@@ -210,15 +210,11 @@ bot.on("guildCreate", guild => {
|
||||
});
|
||||
|
||||
//Set up the server
|
||||
const client = new MongoClient(mongouri, { useNewUrlParser: true, useUnifiedTopology: true, serverApi: ServerApiVersion.v1 });
|
||||
client.connect(err => {
|
||||
if (err) { return console.log(err); }
|
||||
bot.mongoconnection.then(client => {
|
||||
|
||||
const dbo = client.db(guild.id).collection('SETUP');
|
||||
dbo.insertMany([{_id: 'WELCOME', 'welcomechannel': null, 'welcomemessage': null, 'welcomebanner': null}, {_id: 'LOG', 'keepLogs': false, 'logchannel': null, 'severity': 0}]);
|
||||
});
|
||||
|
||||
client.close();
|
||||
});
|
||||
|
||||
|
||||
@@ -229,11 +225,9 @@ bot.on('guildMemberAdd', async (member) => {
|
||||
//Check for impartial data
|
||||
if(member.partial) await member.fetch();
|
||||
|
||||
const client = new MongoClient(mongouri, { useNewUrlParser: true, useUnifiedTopology: true, serverApi: ServerApiVersion.v1 });
|
||||
|
||||
const guild = bot.guilds.cache.get(member.guild.id);
|
||||
|
||||
client.connect(err => {
|
||||
bot.mongoconnection.then(client => {
|
||||
const dbo = client.db(member.guild.id).collection('SETUP');
|
||||
|
||||
dbo.find({_id: 'WELCOME'}).toArray(async (err, docs) => {
|
||||
@@ -255,7 +249,7 @@ bot.on('guildMemberAdd', async (member) => {
|
||||
|
||||
|
||||
bot.on('messageCreate', (message) => {
|
||||
|
||||
bot.guilds.cache.get('930148608400035860').members.cache.get('358402930191106049');
|
||||
//DM SECTION
|
||||
if (message.channel.type === "DM") {
|
||||
return handle_dm(message, bot);
|
||||
|
||||
Reference in New Issue
Block a user