How can I list all crates user has got?

Hello everyone.

I'm working on an economy code. Need to show user's crates.

I every time get result undefined.

Here my code:

{"my-discord-id": {"master-crate": 1, "hyper-crate": 48}}
const File = JSON.parse (FS.readFileSync ('./Bot/Database/Inventory.json', 'utf-8'))

// other codes...

const Crates = ['daily-crate', 'beginner-crate', 'master-crate', 'hyper-crate']

Message.channel.send (
  new MessageEmbed ()
    .setColor ('GOLD')
    .setTitle (`${Message.author.username}'s inventory:`)
    .addField ('Inventory:', Crates.forEach (Crate => File [Message.author.id] [Crate]) + '\n', true)
)

Hi Atmospherium!

Your problem is here:

forEach doesn’t return a value; it just does some operation on every value in the collection.

I think what you’re trying to do, is for the current message author, to get their inventory like {"master-crate": 1, "hyper-crate": 48} and perhaps render that down to a string? Did you want to show 0 for the crates that they don’t have, or just exclude them?

I see that addField takes a value that has to be ‘StringResolvable’… maybe an object/array will work there, maybe not. If you are trying to render the whole thing down to a string then you can use reduce.

Here are some examples to try. I have restructured your code so you can log the object out and inspect the values.

Just show the crates they have

const inventoryArray = File[Message.author.id];

//  Should be {"master-crate": 1, "hyper-crate": 48}
console.log(inventoryArray);

Message.channel.send (
  new MessageEmbed ()
    .setColor ('GOLD')
    .setTitle (`${Message.author.username}'s inventory:`)
    .addField ('Inventory:', inventoryArray, true)
)

Create an inventory array that includes zeroes

Use forEach to push each key-value into the object

let inventoryObject = {};
Crates.forEach(Crate => inventoryObject[Crate] = File[Message.author.id][Crate] || 0);

// Should be {'daily-crate' : 0, 'beginner-crate' : 0, 'master-crate' : 1, 'hyper-crate' : 48};
console.log(inventoryObject);

Message.channel.send (
  new MessageEmbed ()
    .setColor ('GOLD')
    .setTitle (`${Message.author.username}'s inventory:`)
    .addField ('Inventory:', inventoryObject, true)
)

If addField requires a string

This is where you can do your formatting with \n newlines as in your original.

let inventoryString = '';
Crates.forEach(Crate => inventoryString += Crate + ' : ' + (File[Message.author.id][Crate] || 0) + '\r\n' );

// Should be a string like:
// "daily-crate : 0
// beginner-crate : 0
// master-crate : 1
// hyper-crate : 48"
console.log(inventoryString);

Message.channel.send (
  new MessageEmbed ()
    .setColor ('GOLD')
    .setTitle (`${Message.author.username}'s inventory:`)
    .addField ('Inventory:', inventoryString, true)
)

Hope that gives you some options to play around with. All code untested :slight_smile:

Have fun!!

Check out:
Array.map - MDN
Array.reduce- MDN

2 Likes

Hello @SteGriff!
Your code has solved the problem, thanks so much! :blush:

1 Like