nyx/commands/Economy/buy.js
night0721 bc346138dd Bot
2021-06-12 18:53:51 +08:00

71 lines
2.1 KiB
JavaScript

const { Client, Message, MessageEmbed } = require("discord.js");
const inventory = require("../../models/econ");
const items = require("../../util/item");
module.exports = {
name: "purchase",
aliases: ["buy"],
usage: "(Item)",
description: "Buy something from the shop",
category: "Economy",
/**
* @param {Client} client
* @param {Message} message
* @param {String[]} args
*/
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;
if ((await client.data.bal(message.author.id)) < itemPrice)
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();
}
message.inlineReply(
new MessageEmbed()
.setTimestamp()
.setDescription(
`**${message.author.username}** buys **${itemName}** for **${itemPrice}**${client.currency}`
)
.setColor("GREEN")
.setURL(client.web)
);
await client.data.rmv(message.author.id, itemPrice);
});
},
};