Is it possible to send multiple buttons without specifying the exact quantity in Discord.js v13?

I am facing a challenge with sending buttons from an array. I have tested the code below, but it sends two different messages with buttons:

array.flatMap(user => {
                const cs = new client.discord.MessageActionRow()
                    .addComponents(
                        new client.discord.MessageButton()
                        .setLabel(user.type)
                        .setURL(${user.id})
                        .setEmoji('979681456781635700')
                        .setStyle('LINK')
                    );
                const ch = client.channels.cache.get('998376140326256158');
               ch.send({
                    embeds: [embed],
                    components: [cs, ]
                })
            })

Here is my array:

    
  [
    {
     id: '08933438391',
      type: 'ejx1'
    },
    {
      id: '12361425430',
      type: 'ejx3'
    }
  ]

The array may contain 4 id and type pairs, or 3, or any other number (but not more than 10).

Answer №1

If you want to use the addComponents method, you can do so by saving your buttons in an array and then using the method. Remember that you can only have up to five buttons per row.

const row = new client.discord.MessageActionRow();
let buttons = [];
for(let i = 0; i < array.length; i++){
  buttons.push(
    new client.discord.MessageButton()
    .setLabel(array[i].type)
    .setURL(${user.id})
    .setEmoji(array[i].id)
    .setStyle('LINK')
  );
}
row.addComponents(buttons);
//Now you can work with the row variable as needed.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Having trouble with the Ajax load function not functioning in your HTML code?

The issue has been resolved. Thank you to everyone who helped! Initially, I was attempting to run a file offline instead of on a web server (XAMPP server). Once I uploaded the file to the web server, it started working properly. I had been trying to load ...

Strategies for deploying on production as you develop a fresh Nuxt application

What are some recommended strategies for deploying a Vue/Nuxt project on production, especially for larger applications with lengthy build times? Typically, running the command npm run build Causes the app to be inaccessible to users until the build proc ...

Is it possible to install the lib ldap-client module in node.js?

I'm having trouble installing the lib ldap-client package in Node.js. To try and solve this issue, I consulted the following page: https://github.com/nodejs/node-gyp. In an attempt to fix the problem, I have installed python, node-gyp, and Visual St ...

How can I tally the number of channels within a specific category using discord.js?

I'm in the process of creating a bot that can provide me with the number of channels within a specific category. if (strMessage === "!outline") { var outlineSize; message.reply("There are " + outlineSize + " outlines curr ...

Exploring the power of asynchronous Mongoose queries using the async await

I have a system where authenticated users can create new user accounts and assign them to specific groups. The middleware flow for this process is outlined in the following route: router.post('/account/add-users', userController.validateRegist ...

The functionality of Bootstrap pagination is incompatible with the angular-ui-bootstrap/2.5.0/ui-bootstrap-tpls.js library

After reading through this discussion, I have found that in order to resolve an issue, I need to utilize a newer version of ui-bootstrap-tpls.js. However, the downside is that this disables the functionality of bootstrap pagination, which only works with a ...

Clicking the save button in the modal updates the text on both the current tab and any previously clicked tabs

I'm looking to implement tabs that, when clicking on the edit icon, open a modal for editing the content of each tab. The issue I'm currently encountering is that if I open the editor for one tab, close it without saving, then open the editor fo ...

How can you condense an if/else statement in JavaScript into one line?

When searching the DOM using $(current_form).find('input').data('option'), the result can either be undefined or a string of options like 'x|y|z'. To handle this, I typically split the string by calling split('|') to ...

Implement a bootstrap modal to pop up upon successful form validation using the formvalidation.io

Dealing with a form that can take a significant amount of time to submit due to posting data to multiple APIs is not uncommon. Normally, I display a message in a bootstrap modal asking the user to wait patiently without clicking the back button. This modal ...

Issues arise when trying to render a component in React after importing the react-bootstrap library

After running npm init and installing react, react-dom, bootstrap, and react-bootstrap packages, I encountered an issue while trying to import components from the react-bootstrap library. The code import Button from 'react-bootstrap/Button'; resu ...

Implementing flat shading using Phong material alongside a focused spotlight

I'm attempting to achieve flat shading using the Phong material in Three.js. According to the documentation, I need to set flatShading:true in the configuration when creating the material. I've successfully created a cube with these settings and ...

Proper functionality of Chrome Extension chrome.downloads.download

I am currently developing an extension with a feature that allows users to download a file. This file is generated using data retrieved from the localStorage in Chrome. Within my panel.html file, the code looks like this: <!DOCTYPE html> <html&g ...

Real-world demonstration of multiple screens using three.js for an immersive virtual experience

Check out these awesome examples of multiscreen capabilities: webgl_multiple_canvases_circle webgl_multiple_canvases_complex webgl_multiple_canvases_grid These examples even reference the Google Liquid Galaxy project: liquidGalaxy I am exploring how t ...

Learn how to efficiently import data into d3.js from several JavaScript arrays, and create a dynamically updating chart without the need to refresh the page

I currently have two arrays: one is a list of numbers called nums, and the other is a list of strings titled places. For example: nums=[1,2,3,4,5] places = ["house","gym", "work", "school", "park"] Both arrays are the same length. I am looking to crea ...

Ensure that the first option in the Woocommerce 2 Variable Product Dropdown List always displays all available choices

I am currently working on a variable WooCommerce product that includes 2 drop-down select fields. The first field is for selecting the Type of Product (framed or unframed artwork) and the second field is for choosing the Size of Artwork. With the help of ...

The Meteor.loginWithPassword() function bypasses password verification and still allows login

Here is a code snippet: Meteor.loginWithPassword(email, password, function (err) { if(err){ notify.show(i18n.translate('Signin error'), i18n.translate(err.reason)); console.log(err) } }); Users are able to log in regardl ...

Does Objective C have a counterpart to the encode() method in JavaScript?

NSString *searchString = @"Lyngbø"; NSLog("%@",[searchString stringByAddingPercentEscapeUsingEncoding:NSUTF8StringEncoding]); The result is: Lyng%C3%B8 <script type="text/javascript"> document.write(escape("Lyngbø")); </script> The result ...

A missing definition for req.body.object

The issue with the req.body.object variable persists. Even though I have body parser imported, the value remains undefined when I try to console.log() it. //javascript const express = require('express'); var bodyParser = require('body-pa ...

Attempting to establish a means to switch between languages

Currently, I am in the process of implementing a language switch feature for a website project that I am currently working on. This feature will allow users to seamlessly switch between English, Latvian, and Norwegian languages. To achieve this functionali ...

Error: ASP.Net MVC Controller Encountered NullReferenceException

A simple bowling game web app involves two text boxes and a submit button. The first text box is for the FirstRoll, and the second text box is for the SecondRoll. Users input two numbers and click the submit button to display a score. Problem: I am enco ...