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

Tags: , , ,



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!')
  }

}

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



Source: stackoverflow