问题描述
我正在使用 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 命令中的项目之间放置空间是否需要不同的存储方式?
{
balance: 2558,bank: 1898,skin: 'cyan',userHats: [ 'plaguedoctor','egg' ]
}
我尝试使用 .join()
并刚刚找到它并给出错误“TypeError:outfits.join is not a function”因为“outfits”用户没有任何它而他有“hats ” 删除“服装”和“宠物”使命令有效,因为他只有帽子,有没有办法让它忽略空的或未定义的?我试过 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(",")
解决方法
如果项目存储为数组,例如:
outfits = [ outfit1,outfit2]
然后您可以使用 outfits.join(',')
将数组的项目与每个项目之间的 ,
连接起来。
或者,如果您的数据库将其存储为字符串,您可以这样做:
outfits.replace(',',')
将字符串中 ,
的第一个实例替换为 ,
。
但是,由于可能有很多项目,您需要使用正则表达式来替换逗号的 all 实例,因此看起来像:
outfits.replace(/,/g,')
无论哪种方式,都无需担心重组您的所有数据,因为有多种方法可以让它以您想要的方式显示!