nyx/commands/Economy/buy.js

67 lines
2 KiB
JavaScript
Raw Normal View History

2021-06-12 12:53:51 +02:00
const { Client, Message, MessageEmbed } = require("discord.js");
const inventory = require("../../models/econ");
2021-06-24 09:53:19 +02:00
const items = require("../../util/dist/item");
2021-06-12 12:53:51 +02:00
module.exports = {
name: "purchase",
aliases: ["buy"],
usage: "(Item)",
description: "Buy something from the shop",
category: "Economy",
run: async (client, message, args) => {
if (!args[0]) {
return client.err(message, "Economy", "buy", 21);
}
const itemToBuy = args[0];
const validItem = !!items.find(
item => item.alias.toLowerCase() === itemToBuy
);
if (!validItem) {
return client.err(message, "Economy", "buy", 22);
}
const itemName = items.find(
item => item.alias.toLowerCase() === itemToBuy
).item;
const itemPrice = items.find(
item => item.alias.toLowerCase() === itemToBuy
).price;
2021-07-13 05:17:39 +02:00
if ((await client.bal(message.author.id)) < itemPrice)
2021-06-12 12:53:51 +02:00
return client.err(message, "Economy", "buy", 20);
const params = {
User: message.author.id,
};
inventory.findOne(params, async (err, data) => {
if (data.Inventory) {
const hasItem = Object.keys(data.Inventory).includes(itemName);
if (!hasItem) {
data.Inventory[itemName] = 1;
} else {
data.Inventory[itemName]++;
}
await inventory.findOneAndUpdate(params, data);
} else if (data.CP) {
data.Inventory = {
[itemName]: 1,
};
await inventory.findOneAndUpdate(params, data);
} else {
new inventory({
User: message.author.id,
Inventory: {
[itemName]: 1,
},
}).save();
}
2021-09-06 12:38:25 +02:00
message.reply(
2021-06-12 12:53:51 +02:00
new MessageEmbed()
.setTimestamp()
.setDescription(
`**${message.author.username}** buys **${itemName}** for **${itemPrice}**${client.currency}`
)
.setColor("GREEN")
.setURL(client.web)
);
2021-07-13 05:17:39 +02:00
await client.rmv(message.author.id, itemPrice);
2021-06-12 12:53:51 +02:00
});
},
};