guild specific banking data
This commit is contained in:
6 files changed
+289
-167
No files matched your search
+92
-50
@@ -1,4 +1,5 @@
|
||||
const { MessageEmbed } = require('discord.js');
|
||||
const { Bank, addBank } = require('../structures/bank');
|
||||
const { Inventory, addInventory } = require('../structures/inventory');
|
||||
|
||||
module.exports = {
|
||||
@@ -114,23 +115,44 @@ module.exports = {
|
||||
|
||||
if (!banking) {
|
||||
banking = {
|
||||
account: {
|
||||
userID: interaction.member.user.id,
|
||||
balance: GuildDB.startingBalance,
|
||||
cash: 0.00,
|
||||
userID: interaction.member.user.id,
|
||||
guidls: {
|
||||
[guildID]: {
|
||||
account: {
|
||||
balance: GuildDB.startingBalance,
|
||||
cash: 0.00,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
client.dbo.collection("banking").insertOne(banking, function(err, res) {
|
||||
if (err) {
|
||||
client.log(err);
|
||||
const embed = new MessageEmbed()
|
||||
.setDescriptions(`**Internal Error:** ${err}\nContact the Developers\nhttps://discord.gg/YCXhvy9uZw`)
|
||||
.setColor("RED")
|
||||
|
||||
return interaction.send({ embeds: [embed] });
|
||||
}
|
||||
});
|
||||
// Register inventory for user
|
||||
let newBank = new Bank();
|
||||
newBank.createBank(interaction.member.user.id, GuildDB.serverID, GuildDB.startingBalance);
|
||||
newBank.save()
|
||||
.catch(err => {
|
||||
if (err) {
|
||||
client.log(err);
|
||||
const embed = new MessageEmbed()
|
||||
.setDescriptions(`**Internal Error:** ${err}\nContact the Developers\nhttps://discord.gg/YCXhvy9uZw`)
|
||||
.setColor("RED")
|
||||
|
||||
return interaction.send({ embeds: [embed] });
|
||||
}
|
||||
});
|
||||
|
||||
} else banking = banking.banking;
|
||||
|
||||
if (!client.exists(banking.guilds[guildDB.serverID])) {
|
||||
const success = addBank(banking.guilds, guildDB.serverID, interaction.member.user.id, client, guildDB.startingBalance);
|
||||
if (!success) {
|
||||
client.log(err);
|
||||
const embed = new MessageEmbed()
|
||||
.setDescriptions(`**Internal Error:** ${err}\nContact the Developers\nhttps://discord.gg/YCXhvy9uZw`)
|
||||
.setColor("RED")
|
||||
|
||||
return interaction.send({ embeds: [embed] });
|
||||
}
|
||||
}
|
||||
|
||||
let inventory = await client.dbo.collection("inventories").findOne({"inventory.userID": interaction.member.user.id}).then(inventory => inventory);
|
||||
@@ -139,7 +161,6 @@ module.exports = {
|
||||
// Register inventory for user
|
||||
let newInventory = new Inventory();
|
||||
newInventory.createInventory(interaction.member.user.id, GuildDB.serverID);
|
||||
newInventory.inventory.guilds[guildDB.serverID].items[0].cash = args[0].options[0].value;
|
||||
newInventory.save()
|
||||
.catch(err => {
|
||||
if (err) {
|
||||
@@ -166,7 +187,7 @@ module.exports = {
|
||||
}
|
||||
|
||||
if (args[0].name == 'deposit') {
|
||||
if (banking.account.cash.toFixed(2) - args[0].options[0].value < 0) {
|
||||
if (banking.guilds[guildDB.serverID].account.cash.toFixed(2) - args[0].options[0].value < 0) {
|
||||
const nsf = new MessageEmbed()
|
||||
.setDescription('**Bank Notice:** NSF. Non sufficient funds')
|
||||
.setColor("RED");
|
||||
@@ -174,10 +195,10 @@ module.exports = {
|
||||
return interaction.send({ embeds: [nsf] });
|
||||
}
|
||||
|
||||
newBalance = banking.account.balance + args[0].options[0].value;
|
||||
newCash = Math.abs(banking.account.cash.toFixed(2) - args[0].options[0].value);
|
||||
newBalance = banking.guilds[guildDB.serverID].account.balance + args[0].options[0].value;
|
||||
newCash = Math.abs(banking.guilds[guildDB.serverID].account.cash.toFixed(2) - args[0].options[0].value);
|
||||
|
||||
client.dbo.collection("banking").updateOne({"account.userID":interaction.member.user.id},{$set:{"account.balance":newBalance,"account.cash":newCash}}, function(err, res) {
|
||||
client.dbo.collection("banking").updateOne({"account.userID":interaction.member.user.id},{$set:{[`banking.guilds.${guildDB.serverID}.account.balance`]:newBalance,[`banking.guilds.${guildDB.serverID}.account.cash`]:newCash}}, function(err, res) {
|
||||
if (err) {
|
||||
client.log(err);
|
||||
const embed = new MessageEmbed()
|
||||
@@ -207,7 +228,7 @@ module.exports = {
|
||||
return interaction.send({ embeds: [successEmbed] });
|
||||
|
||||
} else if (args[0].name == 'withdraw') {
|
||||
if (args[0].options[0].value > banking.account.balance) {
|
||||
if (args[0].options[0].value > banking.guilds[guildDB.serverID].account.balance) {
|
||||
let nsf = new MessageEmbed()
|
||||
.setDescription('**Bank Notice:** NSF. Non sufficient funds')
|
||||
.setColor("RED");
|
||||
@@ -215,10 +236,10 @@ module.exports = {
|
||||
return interaction.send({ embeds: [nsf] });
|
||||
}
|
||||
|
||||
newBalance = banking.account.balance - args[0].options[0].value;
|
||||
newCash = banking.account.cash + args[0].options[0].value;
|
||||
newBalance = banking.guilds[guildDB.serverID].account.balance - args[0].options[0].value;
|
||||
newCash = banking.guilds[guildDB.serverID].account.cash + args[0].options[0].value;
|
||||
|
||||
client.dbo.collection("banking").updateOne({"account.userID":interaction.member.user.id},{$set:{"account.balance":newBalance,"account.cash":newCash}}, function(err, res) {
|
||||
client.dbo.collection("banking").updateOne({"account.userID":interaction.member.user.id},{$set:{[`banking.guilds.${guildDB.serverID}.account.balance`]:newBalance,[`banking.guilds.${guildDB.serverID}.account.cash`]:newCash}}, function(err, res) {
|
||||
if (err) {
|
||||
client.log(err);
|
||||
const embed = new MessageEmbed()
|
||||
@@ -255,27 +276,48 @@ module.exports = {
|
||||
// Show target users balance
|
||||
|
||||
let targetUserID = args[0].options[0].value.replace('<@!', '').replace('>', '');
|
||||
let targetUserBanking = await client.dbo.collection("banking").findOne({"account.userID":targetUserID}).then(inventory => inventory);
|
||||
let targetUserBanking = await client.dbo.collection("banking").findOne({"account.userID": targetUserID}).then(banking => banking);
|
||||
|
||||
if (!targetUserBanking) {
|
||||
targetUserBanking = {
|
||||
account: {
|
||||
userID: interaction.member.user.id,
|
||||
balance: GuildDB.startingBalance,
|
||||
cash: 0.00,
|
||||
banking = {
|
||||
userID: interaction.member.user.id,
|
||||
guidls: {
|
||||
[guildID]: {
|
||||
account: {
|
||||
balance: GuildDB.startingBalance,
|
||||
cash: 0.00,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
client.dbo.collection("banking").insertOne(targetUserBanking, function(err, res) {
|
||||
if (err) {
|
||||
client.log(err);
|
||||
const embed = new MessageEmbed()
|
||||
.setDescriptions(`**Internal Error:** ${err}\nContact the Developers\nhttps://discord.gg/YCXhvy9uZw`)
|
||||
.setColor("RED")
|
||||
|
||||
return interaction.send({ embeds: [embed] });
|
||||
}
|
||||
});
|
||||
// Register inventory for user
|
||||
let newBank = new Bank();
|
||||
newBank.createBank(targetUserID, GuildDB.serverID, GuildDB.startingBalance);
|
||||
newBank.save()
|
||||
.catch(err => {
|
||||
if (err) {
|
||||
client.log(err);
|
||||
const embed = new MessageEmbed()
|
||||
.setDescriptions(`**Internal Error:** ${err}\nContact the Developers\nhttps://discord.gg/YCXhvy9uZw`)
|
||||
.setColor("RED")
|
||||
|
||||
return interaction.send({ embeds: [embed] });
|
||||
}
|
||||
});
|
||||
|
||||
} else banking = banking.banking;
|
||||
|
||||
if (!client.exists(targetUserBanking.guilds[guildDB.serverID])) {
|
||||
const success = addBank(targetUserBanking.guilds, guildDB.serverID, targetUserID, client, guildDB.startingBalance);
|
||||
if (!success) {
|
||||
client.log(err);
|
||||
const embed = new MessageEmbed()
|
||||
.setDescriptions(`**Internal Error:** ${err}\nContact the Developers\nhttps://discord.gg/YCXhvy9uZw`)
|
||||
.setColor("RED")
|
||||
|
||||
return interaction.send({ embeds: [embed] });
|
||||
}
|
||||
}
|
||||
|
||||
// This lame line of code to get username without ping on discord
|
||||
@@ -291,9 +333,9 @@ module.exports = {
|
||||
|
||||
balanceEmbed.setTitle('Personal Bank Records');
|
||||
balanceEmbed.addFields(
|
||||
{ name: '**Bank**', value: `$${banking.account.balance.toFixed(2)}`, inline: true },
|
||||
{ name: '**Cash**', value: `$${banking.account.cash.toFixed(2)}`, inline: true },
|
||||
{ name: '**Total**', value: `$${banking.account.cash.toFixed(2)+banking.account.balance.toFixed(2)}`, inline: true });
|
||||
{ name: '**Bank**', value: `$${banking.guilds[guildDB.serverID].account.balance.toFixed(2)}`, inline: true },
|
||||
{ name: '**Cash**', value: `$${banking.guilds[guildDB.serverID].account.cash.toFixed(2)}`, inline: true },
|
||||
{ name: '**Total**', value: `$${banking.guilds[guildDB.serverID].account.cash.toFixed(2)+banking.guilds[guildDB.serverID].account.balance.toFixed(2)}`, inline: true });
|
||||
}
|
||||
|
||||
return interaction.send({ embeds: [balanceEmbed] });
|
||||
@@ -301,7 +343,7 @@ module.exports = {
|
||||
} else if (args[0].name == 'give') {
|
||||
// send money from wallet
|
||||
|
||||
if (banking.account.cash.toFixed(2) - args[0].options[1].value < 0) {
|
||||
if (banking.guilds[guildDB.serverID].account.cash.toFixed(2) - args[0].options[1].value < 0) {
|
||||
let embed = new MessageEmbed()
|
||||
.setTitle('Non sufficient funds! Withdraw more cash')
|
||||
.setColor("RED");
|
||||
@@ -309,9 +351,9 @@ module.exports = {
|
||||
return interaction.send({ embeds: [embed] });
|
||||
}
|
||||
|
||||
newCash = banking.account.cash - args[0].options[1].value;
|
||||
newCash = banking.guilds[guildDB.serverID].account.cash - args[0].options[1].value;
|
||||
|
||||
client.dbo.collection("banking").updateOne({"account.userID":interaction.member.user.id},{$set:{"account.cash":newCash}}, function(err, res) {
|
||||
client.dbo.collection("banking").updateOne({"account.userID":interaction.member.user.id},{$set:{[`banking.guilds.${guildDB.serverID}.account.cash`]:newCash}}, function(err, res) {
|
||||
if (err) {
|
||||
client.log(err);
|
||||
const embed = new MessageEmbed()
|
||||
@@ -405,7 +447,7 @@ module.exports = {
|
||||
} else {
|
||||
let newTargetCash = targetUserBanking.account.cash + args[0].options[1].value;
|
||||
|
||||
client.dbo.collection("banking").updateOne({"account.userID": targetUserID}, {$set: {"account.cash": newTargetCash}}, function(err, res) {
|
||||
client.dbo.collection("banking").updateOne({"account.userID": targetUserID}, {$set: {[`banking.guilds.${guildDB.serverID}.account.cash`]: newTargetCash}}, function(err, res) {
|
||||
if (err) {
|
||||
client.log(err);
|
||||
const embed = new MessageEmbed()
|
||||
@@ -427,7 +469,7 @@ module.exports = {
|
||||
} else if (args[0].name == 'transfer') {
|
||||
// send money from bank
|
||||
|
||||
if (banking.account.balance.toFixed(2) - args[0].options[1].value < 0) {
|
||||
if (banking.guilds[guildDB.serverID].account.balance.toFixed(2) - args[0].options[1].value < 0) {
|
||||
let embed = new MessageEmbed()
|
||||
.setTitle('Non sufficient funds! Withdraw more cash')
|
||||
.setColor("RED");
|
||||
@@ -435,9 +477,9 @@ module.exports = {
|
||||
return interaction.send({ embeds: [embed] });
|
||||
}
|
||||
|
||||
newBalance = banking.account.balance - args[0].options[1].value;
|
||||
newBalance = banking.guilds[guildDB.serverID].account.balance - args[0].options[1].value;
|
||||
|
||||
client.dbo.collection("banking").updateOne({"account.userID":interaction.member.user.id},{$set:{"account.balance":newBalance}}, function(err, res) {
|
||||
client.dbo.collection("banking").updateOne({"account.userID":interaction.member.user.id},{$set:{[`banking.guilds.${guildDB.serverID}.account.balance`]:newBalance}}, function(err, res) {
|
||||
if (err) {
|
||||
client.log(err);
|
||||
const embed = new MessageEmbed()
|
||||
@@ -473,7 +515,7 @@ module.exports = {
|
||||
} else {
|
||||
let newTargetBalance = targetUserBanking.account.balance + args[0].options[1].value;
|
||||
|
||||
client.dbo.collection("banking").updateOne({"account.userID":targetUserID},{$set:{"account.balance":newTargetBalance}}, function(err, res) {
|
||||
client.dbo.collection("banking").updateOne({"account.userID":targetUserID},{$set:{[`banking.guilds.${guildDB.serverID}.account.balance`]:newTargetBalance}}, function(err, res) {
|
||||
if (err) {
|
||||
client.log(err);
|
||||
const embed = new MessageEmbed()
|
||||
|
||||
Reference in new issue
Block a user