【问题标题】:How do I make sure that the person has a certain item from the shop?我如何确保该人拥有商店中的特定商品?
【发布时间】:2021-01-31 01:49:23
【问题描述】:

这个问题已经有好几个小时了,我无法确定人们在与它进行活动之前是否已经购买了宠物。

它没有显示错误,但无法正常工作,而且我不知道如何引用玩家库存中的某个物品,因为我正在尝试实现一个宠物功能,您可以在其中与其他宠物战斗人,也将能够喂养你的宠物,还会有宠物比赛和统计数据等活动。

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

}

【问题讨论】:

  • user.id + !'.items.hamster' 为什么字符串前有!? o.O
  • 也许他的意思是if(!db.has(user.id.items.hamster))
  • 我的意思是如果他们没有 items.hamster 它说你没有宠物

标签: javascript discord discord.js quick.db


【解决方案1】:

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

如果您这样做,它将连接真值,转换为字符串,因此在这种情况下为“假”。

你现在拥有的(坏的)

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

你需要什么(好)

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

如果你只是删除它应该可以工作!来自 db.has

【讨论】:

    【解决方案2】:

    试试这个结构

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

    【讨论】:

      【解决方案3】:

      我用这段代码修复了它:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-05-07
        • 2021-09-27
        • 2021-05-22
        • 1970-01-01
        相关资源
        最近更新 更多