0

Hello I am making a discord economy bot and I made a hunt command now I want to know that how to detect if the key inv_ has the value of deer

for hunting I used this code well I added only 1 animal till now for hunting

if(message.content === `${defaultprefix}hunt`){
  var animalHunt = [1 , 2 , 3 , 4 , 5]
  var animalHunted = animalHunt[Math.floor(Math.random()*animalHunt.length)];
  if(animalHunted === 1){
    db.push(message.author.id + 'inv_', 'Deer')
    message.channel.send("You were hunting and hunted a deer ")
  }
}

using djs v12

1 Answers1

0

You can use the .get() function to return the Array (since you used .push(), the data on the <userID>.inv will be an array), then simply search if there's a specific animal in it, in your case you can use this :

    let userinv = db.get(message.author.id + 'inv_')
    if(userinv.includes('Deer')) return message.channel.send('You do have a Deer  !')
  else return message.channel.send("You don't") 

And if you want to delete the Deer from the inventory you can use get the array, change it, and set it back.

// We get the array from the database.
let userinv = db.get(message.author.id + 'inv_')
// Remove all the "Deer"
let newuserinv = userinv.filter(animals => animals !== "Deer")
// And we set the array back to the database.
db.set(message.author.id + 'inv_',newuserinv)
Staxlinou
  • 1,351
  • 1
  • 5
  • 20