Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

253
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda