-3

I've been trying to figure out how to make an inventory system that shows the whole list of that categories items + a users quantity of that item, for example:

if someone does >inv fruits, it would show:

  • apple - 0
  • banana - 1
  • orange - 3

if they did >inv candy, it would show a diff list of items:

  • chocolate - 2
  • lollipop - 0
  • skittles - 0

I store the user_id, category, item and quantity in my database table and I've got the list of items in a category stored in a JSON file.

Right now I've only figured out how to pull the item and quantity that a user has from the database but how would I show the list of items and match the name of item to quantity like the examples above?

CODE SO FAR:

const ValidGroup = args[0];

getInventory(user_id = `${message.author.id}`, group_name = `${ValidGroup}`).then(([rows]) => {

            const InvEmbed = new MessageEmbed()
            .setAuthor(`${message.author.username} | Inventory`)
            .setTimestamp();

            let cardString = " ";
            
            Object.keys(rows).forEach(function (key) {

                //GET DATA
                const row = rows[key];
                let cardCode = row.card_name;
                let cardQuan = row.quanity;

                //CARD ARRAY FORMAT
                let cardArray = `${Code} | ${CardFullName} - ${cardQuan}`
                let invArray = cardArray.split("\n")
                cardString += `\n${invArray}`
            });


            InvEmbed.setDescription(cardString);

            message.channel.send({ embeds: [InvEmbed] });

        });
node_modules
  • 4,790
  • 6
  • 21
  • 37

1 Answers1

0

The first thing I realised while reading your code was the part where you did this:

cardArray.split("\n")

First of all, cardArray is not an array. If it is an array, you should specify what each thing is in params, as people cannot understand what a certain thing is unless if you mention. You can learn how to do this at: https://jsdoc.app/tags-param.html.

cardArray is a string!

const ValidGroup = args[0];

getInventory(user_id = `${message.author.id}`, group_name = `${ValidGroup}`).then(([rows]) => {
  const InvEmbed = new MessageEmbed()
  .setAuthor(`${message.author.username} | Inventory`)
  .setTimestamp();

  let cardString = " ";

  Object.keys(rows).forEach(function (key) {
    //GET DATA
    const row = rows[key];
    let cardCode = row.card_name;
    let cardQuan = row.quanity;

    //CARD ARRAY FORMAT
    let card = `${Code} | ${CardFullName} - ${cardQuan}`
    cardString += `${card}\n`
  });

  InvEmbed.setDescription(cardString);

  message.channel.send({ embeds: [InvEmbed] });
});

I've changed the code by removing invArray and replacing cardArray with 'card'. I've also replaced the \n at the front and moved it to the back, therefore there wouldn't be an extra break at the start of your description.

You can also try adding error catching or checking if the user has anything in their inventory, otherwise it may cause certain errors such as a the MessageEmbed having an empty description.

Jeffplays2005
  • 172
  • 15