How do I make sure that the person has a certain item from the shop?

94 Views Asked by At

I've had this problem for hours where I can't make sure that people have bought a pet before doing activities with it.

It doesn't show an error but it doesn't work properly and I don't know how to reference a certain item in a player's inventory because I'm trying to implement a pet feature where you can fight pets with other people and also will be able to feed your pet and there will be events like pet races and stats etc.

const db = require('quick.db');
const Discord = require('discord.js');

module.exports = {

  name: "fight",

  description: "fight someone",

  async run(client, message, args) {
    let target = message.mentions.users.first();
    if (!target) return message.channel.send('please provide a person to fight');
    let user = message.author;
    let theitemsofuser = await db.fetch(message.author.id, {
      items: []
    });
    if (target === user) return message.channel.send('You can\'t fight yourself!')


    if (db.has(user.id + !'.items.hamster')) return message.channel.send('you need a pet to fight');
    if (db.has(user.id + !'.items.dog')) return message.channel.send('you need a pet to fight');
    if (db.has(user.id + !'.items.cat')) return message.channel.send('you need a pet to fight');

    if (db.has(target.id + !'.items.hamster')) return message.channel.send('your opponent needs a pet to fight');
    if (db.has(target.id + !'.items.dog')) return message.channel.send('your opponent needs a pet to fight');
    if (db.has(target.id + !'.items.cat')) return message.channel.send('your opponent needs a pet to fight');


    message.channel.send('your all good!')
  }

}
3

There are 3 best solutions below

0
On BEST ANSWER

Your string concatenation is wrong. There shouldn't be an exclamation mark before the strings after target.id.

If you do this, it'll concatenate the truthy value, converted to a string, so 'false' in this case.

What you have right now (bad)

'test' + !'string'
// >> 'testfalse'

What you need (good)

'test' + 'string'
// >> 'teststring'

It should work if you just remove the ! from db.has

0
On

Try this construct

async run(client, message, args) {
  ...
  const match = { 
    userPet : "",
    targetPet : ""
  }

  ["hamster","dog","cat"].forEach(pet => {
    if (db.has(user.id.items[pet])) match.userPet = pet
    if (db.has(target.id.items[pet])) match.targetPet = pet 
  });
  if (!match.userPet) { message.channel.send('You need a pet to fight'); return }
  if (!match.targetPet) {message.channel.send('Your opponent needs a pet to fight'); return; }

  message.channel.send('your all good!')
}
0
On

I fixed it using this code :

const Discord = require('discord.js');

module.exports = {

name:"fight",

description:"fight someone",

async run (client, message, args){
let target = message.mentions.users.first();
if(!target)return message.channel.send('please provide a person to fight');
let user = message.author;
let theitemsofuser = await db.fetch(message.author.id, { items: []});
 if(target === user) return message.channel.send('You can\'t fight yourself!')


if(!db.has(user.id + '.items', 'hamster'))return message.channel.send('you need a pet to fight');
if(!db.has(user.id + '.items', 'dog'))return message.channel.send('you need a pet to fight');
if(!db.has(user.id + '.items', 'cat'))return message.channel.send('you need a pet to fight');

if(!db.has(target.id + '.items', 'hamster'))return message.channel.send('your opponent needs a pet to fight');
if(!db.has(target.id + '.items', 'dog'))return message.channel.send('your opponent needs a pet to fight');
if(!db.has(target.id + '.items', 'cat'))return message.channel.send('your opponent needs a pet to fight');


message.channel.send('your all good!')
}}```
Fixes: I used !db.has(user.id + '.items', 'dog') return message.channel.send('you dont have a dog at the moment') Instead of db.has(user.id + '.items', 'dog') return message.channel.send('you dont have a dog at the moment')