如何确定该人在商店中有某件商品?

问题描述

我遇到这个问题已经有好几个小时了,我无法确保人们在进行宠物活动之前已经购买了宠物。

它没有显示错误,但不能正常工作,我也不知道如何引用玩家库存中的某个物品,因为我正在尝试实现一种宠物功能在这里您可以与其他宠物打斗人,也将能够养活您的宠物,并且还会发生诸如宠物比赛和统计数据之类的事件。

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

}

解决方法

您的字符串连接错误。 target.id之后的字符串之前不应带有感叹号。

如果执行此操作,它将连接真实值,并将其转换为字符串,因此在这种情况下为“ false”。

您现在拥有的(不好的)

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

您需要什么(好)

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

只要删除!,它应该可以工作!来自db.has

,

尝试使用此构造

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

我使用以下代码对其进行了修复:

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

module.exports = {

name:"fight",description:"fight someone",async run (client,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')