Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

248
Views
a discord.js inventory system that shows full list

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] });

        });
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

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.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!