Discord.js putting space between inventory items

151 Views Asked by At

I'm using quick.db I want to have a little space between each item because if a user bought two items the items would view like "item1,item2" next to each other Is there a way to put space between them?

Inventory command's code

 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)

How i push the items into "userHats"

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

the args[1] is the item name

would putting space between the items in the inv command require a different storing way?

EDIT: I store the data like in the sqlite file like this:

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

I tried to use .join() and just found it and gives the error "TypeError: outfits.join is not a function" because "outfits" the user doesn't have any of it and he had of "hats" removing the "outfits" and "pets" makes the command works because he only has hats, is there a way to make it ignore the empty or undefined ones? i tried if(outfits === undefined) outfits = "none" but it doesn't seem to be working and gives the same error updated code:

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(", ")
1

There are 1 best solutions below

1
On

If the items are stored as arrays such as:

outfits = [ outfit1, outfit2]

then you can use outfits.join(', ') to join the array's items with a , between each one.

Alternatively, if your database stores it as a string, you can just do:

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

which would replace the first instance of , in the string with , . However, since there are likely many items, you'll want to use regex to replace all instances of the comma, so that would instead look like:

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

Either way, no need to worry about restructuring all your data since there are ways to make it display the way you want it to!