Company logo
  • Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Planes
    • Pruebas
    • Nómina
    • Blog
    • Calculadora

0

70
Vistas
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]})
    

    },
};
7 months ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

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

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

7 months ago · Juan Pablo Isaza Denunciar

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

7 months ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar empleo Planes Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2023 PeakU Inc. All Rights Reserved.