1.3.2 Optimization/fixes

This commit is contained in:
SowinskiBraeden committed 2021-08-06 16:59:10 -07:00
1 parent a3bbbfa577
commit 6f6ea7b472
2 files changed
+35 -37

No files matched your search

+34 -36
View File
@@ -5,21 +5,24 @@ const Discord = require('discord.js');
const client = new Discord.Client(); const client = new Discord.Client();
const config = require('./config'); const config = require('./config');
class Bot { class Bot {
constructor(dev, token) { constructor(dev, token) {
this.dev = dev; this.dev = dev;
this.token = token; this.token = token;
this.connectMongo()
}
async connectMongo() {
this.db = await MongoClient.connect(config.mongoURI,{useUnifiedTopology:true});
this.dbo = this.db.db(config.dbo);
} }
// Updates Prefix // Updates Prefix
async newPrefix(message, n) { async newPrefix(message, n) {
if (n[0]==undefined) return message.channel.send(`You must provide a **new prefix** ${message.author}!`); if (n[0]==undefined) return message.channel.send(`You must provide a **new prefix** ${message.author}!`);
if (n[0].length<1) return message.channel.send('Your new prefix must be \`1\` character!') if (n[0].length<1) return message.channel.send('Your new prefix must be \`1\` character!')
let db = await MongoClient.connect(config.mongoURI,{useUnifiedTopology:true}); this.dbo.collection("prefixes").updateOne({"server.serverID":message.guild.id},{$set:{"server.prefix":n[0]}},function(err, res) {
let dbo = db.db(config.dbo);
dbo.collection("prefixes").updateOne({"server.serverID":message.guild.id},{$set:{"server.prefix":n[0]}},function(err, res) {
if (err) throw err; if (err) throw err;
return message.channel.send(`The new prefix is now **\`${n}\`**`); return message.channel.send(`The new prefix is now **\`${n}\`**`);
}); });
@@ -29,9 +32,7 @@ class Bot {
async remoteLogin(message, args) { async remoteLogin(message, args) {
if (args.length==0) return message.author.send(`You must provide a **email** and **password** ${message.author}!`); if (args.length==0) return message.author.send(`You must provide a **email** and **password** ${message.author}!`);
if (!args[0]==null||!args[0]==undefined&&args[1]==null||args[1]==undefined) return message.author.send(`You must provide a **password** ${message.author}!`); if (!args[0]==null||!args[0]==undefined&&args[1]==null||args[1]==undefined) return message.author.send(`You must provide a **password** ${message.author}!`);
let db = await MongoClient.connect(config.mongoURI,{useUnifiedTopology:true}); let user = await this.dbo.collection("users").findOne({"user.email":args[0]}).then(user => user);
let dbo = db.db(config.dbo);
let user = await dbo.collection("users").findOne({"user.email":args[0]}).then(user => user);
if (user==null||user==undefined) return message.author.send(`Cannot find the email **${args[0]}** ${message.author}!`); if (user==null||user==undefined) return message.author.send(`Cannot find the email **${args[0]}** ${message.author}!`);
if (user.user.discord) { if (user.user.discord) {
if (user.user.discord.id==message.author.id) return message.author.send(`You are already logged in ${message.author}!`); if (user.user.discord.id==message.author.id) return message.author.send(`You are already logged in ${message.author}!`);
@@ -47,7 +48,7 @@ class Bot {
username: message.author.username, username: message.author.username,
discriminator: message.author.discriminator discriminator: message.author.discriminator
} }
dbo.collection("users").updateOne({"user.email":args[0]},{$set:{"user.discord": discord}},function(err,res) { this.dbo.collection("users").updateOne({"user.email":args[0]},{$set:{"user.discord": discord}},function(err,res) {
if (err) throw err; if (err) throw err;
return message.author.send(`Logged in as **${user.user.username}** ${message.author}!`); return message.author.send(`Logged in as **${user.user.username}** ${message.author}!`);
}); });
@@ -58,49 +59,50 @@ class Bot {
} }
async remoteLogout(message) { async remoteLogout(message) {
let db = await MongoClient.connect(config.mongoURI,{useUnifiedTopology:true}); let user = await this.dbo.collection("users").findOne({"user.discord.id":message.author.id}).then(user => user);
let dbo = db.db(config.dbo);
let user = await dbo.collection("users").findOne({"user.discord.id":message.author.id}).then(user => user);
if (!user) return message.channel.send(`You cannot logout if your not logged in ${message.author}!`); if (!user) return message.channel.send(`You cannot logout if your not logged in ${message.author}!`);
dbo.collection("users").updateOne({"user.discord.id":message.author.id},{$unset:{"user.discord":""}},function(err,res) { this.dbo.collection("users").updateOne({"user.discord.id":message.author.id},{$unset:{"user.discord":""}},function(err,res) {
if (err) throw err; if (err) throw err;
return message.channel.send(`Succesfully logged out ${message.author}!`); return message.channel.send(`Succesfully logged out ${message.author}!`);
}); });
} }
async getUser(id) {
let db = await MongoClient.connect(config.mongoURI,{useUnifiedTopology:true});
let dbo = db.db(config.dbo);
return await dbo.collection("users").findOne({"user.discord.id":id}).then(user => user);
}
async account(message) { async account(message) {
getUser(message).then(user => { let user = await this.dbo.collection("users").findOne({"user.discord.id":message.author.id}).then(user => user);
if (user==null) return message.channel.send(`You are not logged in ${message.author}!`); if (user==null) return message.channel.send(`You are not logged in ${message.author}!`);
return message.author.send(`${message.author} Logged in as **${user.user.username}** | **${user.user.email}**`); return message.author.send(`${message.author} Logged in as **${user.user.username}** | **${user.user.email}**`);
});
} }
// Disabled for Production
// async nameSearch(message, args) {
// let user = await this.dbo.collection("users").findOne({"user.discord.id":message.author.id}).then(user => user);
// if (user.user.activeCommunity=='' || user.user.activeCommunity==null) {
// if (args.length==0) return message.channel.send(`You must provide a **First Name**, **Last Name** and **DOB**(mm/dd/yyyy) ${message.author}!`);
// if (args.length==1) return message.channel.send(`You're missing a **Last Name** and **DOB**(mm/dd/yyyy) ${message.author}!`);
// if (args.length==2) return message.channel.send(`You're missing a **DOB**(mm/dd/yyyy) ${message.author}!`);
// }
// if (args.length==0) return message.channel.send(`You must provide a **First Name** and **Last Name** ${message.author}!`);
// if (args.length==1) return message.channel.send(`You're missing a **Last Name** ${message.author}!`);
// return message.channel.send('Debug, valid inputs');
// }
async checkStatus(message, args) { async checkStatus(message, args) {
if (args.length==0) { if (args.length==0) {
getUser(message.author.id).then(user => { let user = await this.dbo.collection("users").findOne({"user.discord.id":message.author.id}).then(user => user);
if (user==null) return message.channel.send(`You are not logged in ${message.author}!`); if (user==null) return message.channel.send(`You are not logged in ${message.author}!`);
return message.channel.send(`${message.author}'s status: ${user.user.dispatchStatus} | Set by: ${user.user.dispatchStatusSetBy}`); return message.channel.send(`${message.author}'s status: ${user.user.dispatchStatus} | Set by: ${user.user.dispatchStatusSetBy}`);
});
} else { } else {
getUser(args[0].id).then(user => { let user = await this.dbo.collection("users").findOne({"user.discord.id":message.author.id}).then(user => user); if (user==null) return message.channel.send(`Cannot find **${args[0]}** ${message.author}!`);
if (user==null) return message.channel.send(`Cannot find **${args[0]}** ${message.author}!`); // This lame line of code to get username without ping on discord
// This lame code to get username without ping on discord const User = client.users.cache.get(args[0].replace('<@!', '').replace('>', ''));
let id = args[0].replace('<@!', '').replace('>', '');
const User = client.users.cache.get(id);
return message.channel.send(`${message.author}, **${User.tag}'s** status: ${user.user.dispatchStatus} | Set by: ${user.user.dispatchStatusSetBy}`); return message.channel.send(`${message.author}, **${User.tag}'s** status: ${user.user.dispatchStatus} | Set by: ${user.user.dispatchStatusSetBy}`);
});
} }
} }
async updateStatus(message, args, prefix) { async updateStatus(message, args, prefix) {
let validStatus=['10-8','10-7','10-6','10-11','10-23','10-97','10-15','10-70','10-80']; let validStatus=['10-8','10-7','10-6','10-11','10-23','10-97','10-15','10-70','10-80'];
getUser(message.author.id).then(user => { let user = await this.dbo.collection("users").findOne({"user.discord.id":message.author.id}).then(user => user);
if (user==null) return message.channel.send(`You are not logged in ${message.author}!`); if (user==null) return message.channel.send(`You are not logged in ${message.author}!`);
if (args.length==0) return message.channel.send(`You must provide a new status ${message.author} | To see a list of valid statuses, use command \`${prefix}validStatus\`.`); if (args.length==0) return message.channel.send(`You must provide a new status ${message.author} | To see a list of valid statuses, use command \`${prefix}validStatus\`.`);
if (!validStatus.includes(args[0])) return message.channel.send(`**${args[0]}** is a Invalid Status ${message.author}! To see a list of valid statuses, use command \`${prefix}validStatus\`.`); if (!validStatus.includes(args[0])) return message.channel.send(`**${args[0]}** is a Invalid Status ${message.author}! To see a list of valid statuses, use command \`${prefix}validStatus\`.`);
@@ -129,16 +131,12 @@ class Bot {
socket.on('bot_updated_status', (res) => { socket.on('bot_updated_status', (res) => {
return message.channel.send(`Succesfully updated status to **${args[0]}** ${message.author}!`); return message.channel.send(`Succesfully updated status to **${args[0]}** ${message.author}!`);
}); });
});
} }
async getPrefix(message) { async getPrefix(message) {
let prefix; let prefix;
let db = await MongoClient.connect(config.mongoURI,{useUnifiedTopology:true});
let dbo = db.db(config.dbo);
if (message.channel.type!="dm") { if (message.channel.type!="dm") {
let guild = await dbo.collection("prefixes").findOne({"server.serverID":message.guild.id}).then(guild => guild); let guild = await this.dbo.collection("prefixes").findOne({"server.serverID":message.guild.id}).then(guild => guild);
// If not prefix, generate server default // If not prefix, generate server default
if (!guild) { if (!guild) {
@@ -148,7 +146,7 @@ class Bot {
prefix: config.defaultPrefix prefix: config.defaultPrefix
} }
} }
dbo.collection("prefixes").insertOne(newGuild, function(err, res) { this.dbo.collection("prefixes").insertOne(newGuild, function(err, res) {
if (err) throw err; if (err) throw err;
}); });
prefix = newGuild.server.serverID; prefix = newGuild.server.serverID;
@@ -229,9 +227,9 @@ class Bot {
if (command == 'updatestatus') this.updateStatus(message, args, prefix); if (command == 'updatestatus') this.updateStatus(message, args, prefix);
if (command == 'account') this.account(message); if (command == 'account') this.account(message);
if (command == 'penalcodes') return message.channel.send('https://www.linespolice-cad.com/penal-code'); if (command == 'penalcodes') return message.channel.send('https://www.linespolice-cad.com/penal-code');
if (command == 'namedb') this.nameSearch(message, args);
// Disabled for dev // Disabled for dev
// if (command == 'namedb') this.nameSearch(message, args);
// if (command == 'platedb') this.plateSearch(message, args); // if (command == 'platedb') this.plateSearch(message, args);
// if (command == 'firearmdb') this.weaponSearch(message, args); // if (command == 'firearmdb') this.weaponSearch(message, args);
// if (command == 'createbolo') this.createBolo(message, args); // if (command == 'createbolo') this.createBolo(message, args);
+1 -1
View File
@@ -2,7 +2,7 @@
"defaultPrefix": "?", "defaultPrefix": "?",
"botID": "860298681047056434", "botID": "860298681047056434",
"mongoURI": "mongodb+srv://lps_bot:ktSkHml2BU9EIQ0a@heroku-police-cad-dev.j7aom.mongodb.net/heroku_8pc4g86l?retryWrites=true&w=majority", "mongoURI": "mongodb+srv://lps_bot:ktSkHml2BU9EIQ0a@heroku-police-cad-dev.j7aom.mongodb.net/heroku_8pc4g86l?retryWrites=true&w=majority",
"version": "Beta 1.1.4", "version": "Beta 1.3.2",
"socket": "https://police-cad-dev.herokuapp.com/", "socket": "https://police-cad-dev.herokuapp.com/",
"dbo": "heroku_8pc4g86l" "dbo": "heroku_8pc4g86l"
} }