0

我正在使用 quick.db 我想在每个项目之间留一点空间,因为如果用户购买了两个项目,这些项目会像“item1,item2”一样彼此相邻有没有办法在它们之间放置空间?

库存命令的代码

 let hats = db.get(`${message.author.id}.userHats`)
        if(hats === undefined) hats = "none"
        let outfits = db.get(`${message.author.id}.userOutfits`)
        if(outfits === undefined) outfits = "none"
        let pets = db.get(`${message.author.id}.userPets`)
        if(pets === undefined) pets = "none"

        const embed = new Discord.MessageEmbed()
        .setTitle(`${message.author.tag}\'s inventory`)
        .addField(`Hats`, `${hats}` || "none")
        .addField(`Outfits`, `${outfits}` || "none")
        .addField(`Pets`, `${pets}` || "none")
        .setTimestamp()
        .setColor('#00ffff')
        .setFooter(message.member.user.tag, message.author.avatarURL());
        message.channel.send(embed)

我如何将项目推入“userHats”

db.push(`${message.author.id}.userHats`, args[1])

args[1]是项目名称

在 inv 命令中的项目之间放置空格是否需要不同的存储方式?

编辑:我将数据存储在 sqlite 文件中,如下所示:

{
  balance: 2558,
  bank: 1898,
  skin: 'cyan',
  userHats: [ 'plaguedoctor', 'egg' ]
}

我尝试使用.join()并找到它并给出错误“TypeError:outfits.join is not a function”,因为“outfits”用户没有任何它,他有“帽子”删除“outfits”和“ pets" 使命令有效,因为他只有帽子,有没有办法让它忽略空的或未定义的?我试过if(outfits === undefined) outfits = "none"了,但它似乎没有工作,并给出了相同的错误更新代码:

let hats = db.get(`${message.author.id}.userHats`)
        if(hats === undefined) hats = "none"
        let hats2 = hats.join(", ")
        
        let outfits = db.get(`${message.author.id}.userOutfits`)
        if(outfits === undefined) outfits = "none"
        let outfits2 = outfits.join(", ")
        
        let pets = db.get(`${message.author.id}.userPets`)
        if(pets === undefined) pets = "none"
        let pets2 = pets.join(", ")
4

1 回答 1

2

如果项目存储为数组,例如:

outfits = [ outfit1, outfit2]

然后您可以使用在每个项目之间outfits.join(', ')加入数组的项目。,

或者,如果您的数据库将其存储为字符串,您可以这样做:

outfits.replace(',', ', ')

,这会将字符串中的第一个实例替换为, . 但是,由于可能有很多项目,您将希望使用正则表达式来替换逗号的所有实例,因此它看起来像:

outfits.replace(/,/g, ', ')

无论哪种方式,都无需担心重组所有数据,因为有办法让它以您想要的方式显示!

于 2021-04-14T00:31:50.727 回答