【问题标题】:How to check if a specific thing exists in quick.db?如何检查 quick.db 中是否存在特定的东西?
【发布时间】:2021-09-27 20:35:32
【问题描述】:

所以我正在制作一个不和谐的游戏机器人,您可以通过打开箱子来收集角色,但问题是当您已经拥有角色时它们会再次出现。

我用的方法db.push

代码:

if(has === true){
  console.log(has)
  let embed = new discord.MessageEmbed()
.setTitle('Chest Opening | Clash Chest')
.setDescription("<@"+message.author+"> got:\n"+amount+" :coin:")
.setColor('RANDOM')
.setFooter('Created by Tahmid GS#1867')
 message.channel.send(embed)
  await db.fetch(`coin_${message.author.id}`)
  db.add(`coin_${message.author.id}`, amount)
}
else if(has === false){
  console.log(has)
await db.fetch(`troop_${message.author.id}`)
db.push(`troop_${message.author.id}`, nada)



let embed = new discord.MessageEmbed()
.setTitle('Chest Opening | Clash Chest')
.setDescription("<@"+message.author+"> got:\n"+amount+" :coin:\n||Common: "+nada+"||")
.setColor('RANDOM')
.setFooter('Created by Tahmid GS#1867')
  
  message.channel.send(embed)

   await db.fetch(`coin_${message.author.id}`)
  db.add(`coin_${message.author.id}`, amount)
  await db.fetch(`card_${message.author.id}`)
  db.add(`card_${message.author.id}`, 1)
}

已经是let has = db.has(troop_${message.author.id}, nada) 我用 let has = db.has(troop_${message.author.id}.nada) 并让 has = db.has(nada,troop_${message.author.id})

但是好像不行,在控制台里是“假”

nada 是随机字符

【问题讨论】:

    标签: discord.js quick.db


    【解决方案1】:

    您正在检查数据库是否具有特定键。在troop_${message.author.id} 键下的数据库中有一个数组。您想检查该数组是否包含该字符。

    let has = (db.get(`troop_${message.author.id}`) || []).includes(nada);
    

    您可能还想检查db.get() 返回undefined 或类似的可能性。在这种情况下,我们在一个空数组上调用.includes()。而不是得到一个错误TypeError: Cannot read property 'includes' of undefined

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-12
      • 1970-01-01
      • 1970-01-01
      • 2012-03-12
      • 1970-01-01
      • 2021-09-20
      • 1970-01-01
      • 2017-03-26
      相关资源
      最近更新 更多