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

266
Visualizações
The embed in my search command isn't working properly

In this search command, the for in loop is not adding the fields to the embed. When I use the command, I get the name and description, which I defined while creating the embed. But I do not get the fields which I add in the for in loop.

Here is an image of what is returned, just in case it helps answer my question:

An image of what the command returns

My search command code:

const { MessageEmbed } = require('discord.js')

module.exports = {
    name: 'search',
    description: 'Searches for a song and returns the results.',
  options: [
    {
      name: 'search_term',
      description: 'The song to search for.',
      type: 'STRING',
      required: true
    },
    {
      name: 'limit',
      type: 'INTEGER',
      description: 'Number of results to return.',
      required: true
    },
    {
      name: 'type',
      type: 'STRING',
      description: 'The type of search result.',
      required: true,
      choices: [{
        name: 'Video',
        value: 'video'
      },
      {
        name: 'Playlist',
        value: 'playlist'
      }]
    }
  ],
    async execute(interaction) {
    const query = await interaction.options.getString('search_term')
    const limit = await interaction.options.getInteger('limit')
    const type = await interaction.options.getString('type')

    let type2 = 'x'
    if (type === 'video') type2 = 'Video'
    else if (type === 'playlist') type2 = 'Playlist'

    let results = await interaction.client.distube.search(query, { limit: limit, type: type })

    let embed = new MessageEmbed()
    .setTitle('search')
    .setDescription('Searches for a song and returns the results.')

    for (const result in results) {
      if (result.type === 'video') {
        embed.addFields({
          name :result.name, 
          value: `ID: ${result.id}\nType: ${type2}\nURL: ${result.url}\nViews: ${result.views}\nDuration: ${result.formattedDuration}\nLive: ${result.isLive}`, 
          inline: true})
      }
      else if (result.type === 'playlist') {
        embed.addFields({
          name: result.name, 
          value: `ID: ${result.id}\nType: ${type2}\nURL: ${result.url}\nViews: ${result.views}`,
          inline: true})
      }
    }

    await interaction.reply({embeds: [embed]})
    

    },
};
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You need to use embed.addField(...) or to pass an array to embed.addFields([...]).

<MessageEmbed>.addFields() takes an array as an input.

about 4 years ago · Juan Pablo Isaza Relatório

0

The solution: Use for of instead of for in

Difference between the 2:

The for...of statement creates a loop iterating over iterable objects, including: built-in String, Array, array-like objects (e.g., arguments or NodeList), TypedArray, Map, Set

The for...in statement iterates over all enumerable properties of an object that are keyed by strings (ignoring ones keyed by Symbols), including inherited enumerable properties.

For more information:

for of: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of

for in: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in

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