【问题标题】:Select Menu Discord JS | How can i use array in .addOptions?Select Menu Discord JS | How can i use array in .addOptions?
【发布时间】:2022-12-19 03:25:14
【问题描述】:

I would like to map an array inside .addOptions.

Basically, I would like to add a value { } to addOptions by taking the values of an object or array.

Examples:

let menu = new SelectMenuBuilder()
  .setCustomId('select')
  .setPlaceholder('Nothing selected')
  .addOptions(arrayt.map(song => label: song.name, description: song.formattedDuration, value: blabla)

【问题讨论】:

  • Could you provide an example of how your array look like
  • its like [ { id: '8kcdvlgR1Uw', url: 'youbew', }, { id: 'n61ULEU7CO0', name: 'Best of lofi ', } ]

标签: node.js discord discord.js


【解决方案1】:

You are missing curly brackets in your map function. The function should look something like this

const songs = [
   {
     name: 'Song 1',
     formattedDuration: 2
   },
   {
     name: 'Song 2',
     formattedDuration: 5
   },
   {
     name: 'Song 3',
     formattedDuration: 6
   }
]

const menu = new SelectMenuBuilder()
      .setCustomId('select')
      .setPlaceholder('Nothing selected')
      .addOptions(songs.map(song => { return {label: song.name, description: song.formattedDuration }}))

【讨论】:

  • Bro, My editor tells me to replace the commas with semicolons Like this : .addOptions(result.map(song => { label: ${i1} . +song.name.slice(0, 20); description: song.formattedDuration.replace(/00:00/g, "Live"); value: ${i1} }))
  • Error : TypeError: Cannot destructure property 'emoji' of 'selectMenuOption' as it is undefined. I haven't coded anything about emoji though
  • I edited my code now it should work
【解决方案2】:

The values should be in the form of a string, not integer or double, whatever you are giving, use toString() on them.

const row = new ActionRowBuilder().addComponents(
            new StringSelectMenuBuilder()
                .setCustomId('select')
                .setPlaceholder('Nothing selected')
                .addOptions(
                    adana.map(function (curentValue, index) {
                        return {
                            label: index.toString(),
                            value: index.toString(),
                        }
                    })
                )
        )

【讨论】:

    猜你喜欢
    • 2022-12-27
    • 2023-01-31
    • 2022-12-01
    • 2022-12-28
    • 2022-12-27
    • 2022-12-26
    • 2022-12-19
    • 2022-11-20
    • 2022-12-01
    相关资源
    最近更新 更多