473 lines
18 KiB
JavaScript
473 lines
18 KiB
JavaScript
const { MessageEmbed } = require('discord.js');
|
|
|
|
module.exports = {
|
|
name: "bank",
|
|
debug: false,
|
|
description: "Manage your banking",
|
|
usage: "[cmd] [opt], ephemeral: true",
|
|
permissions: {
|
|
channel: ["VIEW_CHANNEL", "SEND_MESSAGES", "EMBED_LINKS"],
|
|
member: [],
|
|
},
|
|
SlashCommand: {
|
|
options: [
|
|
{
|
|
name: "deposit",
|
|
description: "Deposit cash into your bank",
|
|
value: "deposit",
|
|
type: 1,
|
|
options: [{
|
|
name: "amount",
|
|
description: "Amount to deposit",
|
|
value: "amount",
|
|
type: 10,
|
|
min_value: 0.01,
|
|
required: true,
|
|
}]
|
|
},
|
|
{
|
|
name: "withdraw",
|
|
description: "Withdraw cash from you bank",
|
|
value: "deposit",
|
|
type: 1,
|
|
options: [{
|
|
name: "amount",
|
|
description: "Amount to withdraw",
|
|
value: "amount",
|
|
type: 10,
|
|
min_value: 0.01,
|
|
required: true,
|
|
}]
|
|
},
|
|
{
|
|
name: "balance",
|
|
description: "View your bank balance and count your cash",
|
|
value: "balance",
|
|
type: 1,
|
|
options: [{
|
|
name: "user",
|
|
description: "User to view ballance",
|
|
value: "user",
|
|
type: 6,
|
|
required: false,
|
|
}]
|
|
},
|
|
{
|
|
name: "give",
|
|
description: "Give a user cash",
|
|
value: "give",
|
|
type: 1,
|
|
options: [
|
|
{
|
|
name: "user",
|
|
description: "User to give cash to",
|
|
value: "user",
|
|
type: 6,
|
|
required: true,
|
|
},
|
|
{
|
|
name: "amount",
|
|
description: "The amount to give",
|
|
value: 1,
|
|
type: 10,
|
|
required: true,
|
|
},
|
|
]
|
|
},
|
|
{
|
|
name: "transfer",
|
|
description: "Transfer directly to users bank",
|
|
value: "transfer",
|
|
type: 1,
|
|
options: [
|
|
{
|
|
name: "user",
|
|
description: "User to transfer to",
|
|
value: "user",
|
|
type: 6,
|
|
required: true,
|
|
},
|
|
{
|
|
name: "amount",
|
|
description: "The amount to transfer",
|
|
value: 1,
|
|
type: 10,
|
|
required: true,
|
|
},
|
|
]
|
|
}
|
|
],
|
|
/**
|
|
*
|
|
* @param {require("../structures/QuarksBot")} client
|
|
* @param {import("discord.js").MessageCreate} message
|
|
* @param {string[]} args
|
|
* @param {*} param3
|
|
*/
|
|
run: async (client, interaction, args, { GuildDB }) => {
|
|
if (GuildDB.customChannelStatus==true&&!GuildDB.allowedChannels.includes(interaction.channel_id)) {
|
|
return interaction.send(`You are not allowed to use the bot in this channel.`);
|
|
}
|
|
|
|
let banking = await client.dbo.collection("banking").findOne({"account.userID": interaction.member.user.id}).then(banking => banking);
|
|
|
|
if (!banking) {
|
|
banking = {
|
|
account: {
|
|
userID: interaction.member.user.id,
|
|
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] });
|
|
}
|
|
});
|
|
}
|
|
|
|
|
|
if (args[0].name == 'deposit') {
|
|
if (banking.account.cash.toFixed(2) - args[0].options[0].value < 0) {
|
|
const nsf = new MessageEmbed()
|
|
.setDescription('**Bank Notice:** NSF. Non sufficient funds')
|
|
.setColor("RED");
|
|
|
|
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);
|
|
|
|
client.dbo.collection("banking").updateOne({"account.userID":interaction.member.user.id},{$set:{"account.balance":newBalance,"account.cash":newCash}}, 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] });
|
|
}
|
|
});
|
|
client.dbo.collection("inventories").updateOne({"inventory.userID":interaction.member.user.id, "inventory.items.type":"wallet"},
|
|
{$set:{"inventory.items.$.cash":newCash}}, 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] });
|
|
}
|
|
});
|
|
|
|
const successEmbed = new MessageEmbed()
|
|
.setTitle('Bank Notice:')
|
|
.setDescription(`Successfully deposited **$${args[0].options[0].value.toFixed(2)}**\nUse \`/bank balance\` to view your balance`)
|
|
.setColor('GREEN');
|
|
|
|
return interaction.send({ embeds: [successEmbed] });
|
|
|
|
} else if (args[0].name == 'withdraw') {
|
|
if (args[0].options[0].value > banking.account.balance) {
|
|
let nsf = new MessageEmbed()
|
|
.setDescription('**Bank Notice:** NSF. Non sufficient funds')
|
|
.setColor("RED");
|
|
|
|
return interaction.send({ embeds: [nsf] });
|
|
}
|
|
|
|
newBalance = banking.account.balance - args[0].options[0].value;
|
|
newCash = banking.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) {
|
|
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] });
|
|
}
|
|
});
|
|
let inventory = await client.dbo.collection("inventories").findOne({"inventory.userID": interaction.member.user.id}).then(inventory => inventory);
|
|
|
|
if (!inventory) {
|
|
// Register inventory for user
|
|
let newInventory = new Inventory();
|
|
newInventory.createInventory(interaction.member.user.id);
|
|
newInventory.inventory.items[0].cash = args[0].options[0].value;
|
|
newInventory.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 {
|
|
client.dbo.collection("inventories").updateOne({"inventory.userID":interaction.member.user.id, "inventory.items.type":"wallet"},
|
|
{$set:{"inventory.items.$.cash":newCash}}, 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] });
|
|
}
|
|
});
|
|
}
|
|
|
|
const successEmbed = new MessageEmbed()
|
|
.setTitle('Bank Notice:')
|
|
.setDescription(`Successfully withdrew **$${args[0].options[0].value.toFixed(2)}**\nUse \`/bank balance\` or \`/inventory wallet\` to view your cash balance`)
|
|
.setColor('GREEN');
|
|
|
|
return interaction.send({ embeds: [successEmbed] });
|
|
|
|
} else if (args[0].name == 'balance') {
|
|
let balanceEmbed = new MessageEmbed()
|
|
.setColor(client.config.EmbedColor);
|
|
|
|
if (args[0].options&&args[0].options[0]) {
|
|
// 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);
|
|
|
|
if (!targetUserBanking) {
|
|
targetUserBanking = {
|
|
account: {
|
|
userID: interaction.member.user.id,
|
|
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] });
|
|
}
|
|
});
|
|
}
|
|
|
|
// This lame line of code to get username without ping on discord
|
|
const User = client.users.cache.get(targetUserID);
|
|
|
|
balanceEmbed.setTitle(`${User.tag}'s Bank Records`);
|
|
balanceEmbed.addFields(
|
|
{ name: '**Bank**', value: `$${targetUserBanking.account.balance}`, inline: true },
|
|
{ name: '**Cash**', value: `$${targetUserBanking.account.cash}`, inline: true });
|
|
|
|
} else {
|
|
// Show command authors balance
|
|
|
|
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 });
|
|
}
|
|
|
|
return interaction.send({ embeds: [balanceEmbed] });
|
|
|
|
} else if (args[0].name == 'give') {
|
|
// send money from wallet
|
|
|
|
if (banking.account.cash.toFixed(2) - args[0].options[1].value < 0) {
|
|
let embed = new MessageEmbed()
|
|
.setTitle('Non sufficient funds! Withdraw more cash')
|
|
.setColor("RED");
|
|
|
|
return interaction.send({ embeds: [embed] });
|
|
}
|
|
|
|
newCash = banking.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) {
|
|
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] });
|
|
}
|
|
});
|
|
|
|
client.dbo.collection("inventories").updateOne({"inventory.userID":interaction.member.user.id, "inventory.items.type":"wallet"},
|
|
{$set:{"inventory.items.$.cash":newCash}}, 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] });
|
|
}
|
|
});
|
|
|
|
let targetUserID = args[0].options[0].value.replace('<@!', '').replace('>', '');
|
|
let targetUserInventory = await client.dbo.collection("inventories").findOne({"inventory.userID":targetUserID}).then(inventory => inventory);
|
|
|
|
if (!targetUserInventory) {
|
|
// Register inventory for user
|
|
let newInventory = new Inventory();
|
|
newInventory.createInventory(targetUserID);
|
|
newInventory.inventory.items[0].cash = args[0].options[1].value;
|
|
newInventory.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 {
|
|
let newTargetCash = targetUserInventory.inventory.items[0].cash + args[0].options[1].value;
|
|
|
|
client.dbo.collection("inventories").updateOne({"inventory.userID":targetUserID, "inventory.items.type":"wallet"},
|
|
{$set:{"inventory.items.$.cash":newTargetCash}}, 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] });
|
|
}
|
|
});
|
|
}
|
|
|
|
let targetUserBanking = await client.dbo.collection("banking").findOne({"account.userID": targetUserID}).then(banking => banking);
|
|
|
|
if (!targetUserBanking) {
|
|
targetUserBanking = {
|
|
account: {
|
|
userID: targetUserID,
|
|
balance: GuildDB.startingBalance,
|
|
cash: args[0].options[1].value,
|
|
}
|
|
}
|
|
|
|
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] });
|
|
}
|
|
});
|
|
} 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) {
|
|
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] });
|
|
}
|
|
});
|
|
}
|
|
|
|
const successEmbed = new MessageEmbed()
|
|
.setTitle('Success')
|
|
.setDescription(`Successfully gave <@${targetUserID}> $${args[0].options[1].value.toFixed(2)}`)
|
|
.setColor('GREEN');
|
|
|
|
return interaction.send({ embeds: [successEmbed] });
|
|
|
|
} else if (args[0].name == 'transfer') {
|
|
// send money from bank
|
|
|
|
if (banking.account.balance.toFixed(2) - args[0].options[1].value < 0) {
|
|
let embed = new MessageEmbed()
|
|
.setTitle('Non sufficient funds! Withdraw more cash')
|
|
.setColor("RED");
|
|
|
|
return interaction.send({ embeds: [embed] });
|
|
}
|
|
|
|
newBalance = banking.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) {
|
|
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] });
|
|
}
|
|
});
|
|
|
|
let targetUserID = args[0].options[0].value.replace('<@!', '').replace('>', '');
|
|
let targetUserBanking = await client.dbo.collection("banking").findOne({"account.userID": targetUserID}).then(banking => banking);
|
|
|
|
if (!targetUserBanking) {
|
|
targetUserBanking = {
|
|
account: {
|
|
userID: targetUserID,
|
|
balance: (GuildDB.startingBalance + args[0].options[1].value),
|
|
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] });
|
|
}
|
|
});
|
|
} 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) {
|
|
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] });
|
|
}
|
|
});
|
|
}
|
|
|
|
const successEmbed = new MessageEmbed()
|
|
.setTitle('Bank Notice:')
|
|
.setDescription(`Successfully transfered <@${targetUserID}> $${args[0].options[1].value.toFixed(2)}`)
|
|
.setColor('GREEN');
|
|
|
|
return interaction.send({ embeds: [successEmbed] });
|
|
}
|
|
},
|
|
},
|
|
} |