Hi, I did this command, for my economy system, it's about being able to sell objects from the inventory, the error I have is that, when subtracting the type of objects in the db, instead of eliminating the amount of object, the amount of the db is taken into account and adds the amount entered in the command, And he adds it to the amount of the db and my task is to subtract the db and on other occasions he throws me an error from unknown interaction can you help me please
#mongoose help
12 messages · Page 1 of 1 (latest)
- Consider reading #how-to-get-help to improve your question!
- Explain what exactly your issue is.
- Post the full error stack trace, not just the top part!
- Show your code!
- Issue solved? Press the button!
const { MessageEmbed } = require("discord.js");
const ecoSchema = require(`${process.cwd()}/Modelos/ecoSchema.js`);
module.exports = {
name: `sell`,
description: `vende objetos`,
PermsBot: [`SendMessages`],
PermsUser: [],
options: [
{
name: "object",
description: "objeto a vender",
type: 3,
required: true,
choices: [
{
name: "fish",
value: "fish"
},
{
name: "wood",
value: "wood"
},
{
name: "iron",
value: "iron"
},
{
name: "coal",
value: "coal"
},
{
name: "diamont",
value: "diamont"
}
]
},
{
name: "amount",
description: "cantidad a vender",
type: 10,
required: true,
},
],
run: async (client, interaction, args) => {
const tipo = interaction.options.get("object").value;
const cantidad = interaction.options.getNumber("amount");
const data = await ecoSchema.findOne({ userID: interaction.member.id });
const precios = {
fish: 10,
wood: 5,
iron: 8,
coal: 12,
diamond: 50
};
const precioUnitario = precios[tipo];
const precioTotal = precioUnitario * cantidad;
console.log("data[tipo]:", data[tipo]);
console.log("cantidad:", cantidad);
if (data[tipo] < cantidad) {
return interaction.reply(`No tienes suficientes ${tipo} para vender.`);
}
await ecoSchema.findOneAndUpdate(
{ userID: interaction.member.id },
{ $inc: { [tipo]: (data[tipo] - cantidad), dinero: precioTotal } },
{ new: true }
);
return interaction.reply(`Has vendido ${cantidad} ${tipo}(s) por ${precioTotal} monedas.`);
}
}
They are the same command but discord does not allow me to send it in a single message
You tell it to increase by (currentValue - amount), not to set it to that value or decrease by amount
and how can I do that, I already tried different ways
Either use $inc and pass -amount only or use $set instead
I already tried with { $inc: { [type]: -amount } and what it does is put the amount in negative example -3
The name $inc suggests it‘s only for increasing, not decreasing. So maybe it doesn’t work correctly for that. So use $set for that instead
Ready thank you very much, by using $set I kept putting negatives but I defined a variable for with equal to the subtraction of the data and the quantity, and now it works, it subtracts instead of adding, I eliminated the { new: true } and modified { $set: { [type]: final quantity }, $inc: { money: total price } }, so that the money would not be subtracted since that happened
Another thing, what can be the reason for the unknown interaction, because I have several commands that give that error but after executing them several times, the command is already executed, and there are times that it executes the command but does not send the message and gives the error