Obtain a compilation of users who have expressed their reaction to a message with a specific emoji on Discord

Check out this Discord.js code snippet for a Discord bot:

client.channels.fetch('channelID here').then(function (channel) {
 channel.messages.fetch('messageID here').then(function (message) {
  console.log(message.reactions.cache.get('EmojiID here').users);
 });
});

When executed, the console displays the following information:

ReactionUserManager {
  cacheType: [class Collection extends Collection],
  cache: Collection [Map] {},
  reaction: MessageReaction {
    message: Message {
      channel: [TextChannel],
      deleted: false,
      id: 'MessageID here',
      type: 'DEFAULT',
      system: false,
      content: 'What role do you want?\n' +
        'React with:\n' +
        '<:Red:870224025811558450>  for <@&870162738561814578> \n' +
        '<:Blue:870224213976444959> for <@&870162842983206922> \n' +
        '<:Yellow:870224106061172776> for <@&870162885412810773>\n' +
        'You will be assigned the role corresponding to your most recent reaction.\n' +
        'Unreact to remove the role.',
      author: [User],
      pinned: false,
      tts: false,
      nonce: null,
      embeds: [],
      attachments: Collection [Map] {},
      createdTimestamp: 1627548937713,
      editedTimestamp: 1627617831107,
      reactions: [ReactionManager],
      mentions: [MessageMentions],
      webhookID: null,
      application: null,
      activity: null,
      _edits: [],
      flags: [MessageFlags],
      reference: null
    },
    me: true,
    users: [Circular],
    _emoji: ReactionEmoji {
      animated: undefined,
      name: 'Red',
      id: 'EmojiID here',
      deleted: false,
      reaction: [Circular]
    },
    count: 2
  }
}

In the output, count: 2 is shown. To retrieve the list of objects associated with these two users, how can this be accomplished?

Answer №1

You were very close to the solution. The ReactionUserManager is a manager that includes a cache property which returns a collection of User objects.

If you want to fetch the users as well, you can follow this approach:

client.channels.fetch('channelID').then(function (channel) {
 channel.messages.fetch('messageID').then(function (message) {
  const reaction = message.reactions.cache.get('EmojiID')
  reaction.users.fetch().then(function (users) {
    console.log(users)
  })
 })
})

Using async-await in an async function can make the code more readable like this:

const channel = await client.channels.fetch('channelID')
const message = await channel.messages.fetch('messageID')
const reaction = message.reactions.cache.get('EmojiID')
const users = await reaction.users.fetch()

console.log(users)

// For example, you can add a role to each user
users.each(async (user) => {
  // Retrieve the member object as users do not have roles
  const member = await message.guild.members.fetch(user.id)
  member.roles.add('ROLE ID')
})

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

Issues with React router arise once the app has been built

I am new to utilizing react and react-router in my project. I have built the application using create-react-app and now I am facing an issue with routing between two main pages. After trying different approaches, I managed to get it working during develop ...

What is the best way to shorten string values that exceed a certain length?

Here is an array list I have: $myarray = array( array( 'name' => 'File name 1 - type.zip', 'size' => '600KB', ), array( 'name' => 'File name 2 - type.pdf&a ...

Employing distinct techniques for a union-typed variable in TypeScript

I'm currently in the process of converting a JavaScript library to TypeScript. One issue I've encountered is with a variable that can be either a boolean or an array. This variable cannot be separated into two different variables because it&apos ...

Obtaining and Assigning Filter Values in Material Table

Is there a way to programmatically obtain and adjust filter values with material-table? I am looking to enable users to save filter settings as reports and access them whenever necessary. ...

Retrieving data from an HTML Table using Javascript

I am in the process of designing a dynamic web form that populates from a stored procedure. The form consists of a table with a dropdown list, a text box, and a label. While I can successfully retrieve data from the dropdown and text box, I am facing diffi ...

What causes jQuery results to resemble a function?

When I create a jQuery wrapped set and insert it into console.log, the output appears as follows: I am aware that we can manipulate the console to display objects or arrays. For example, when we have: var obj = { 0: 'some', 1: 'dat ...

Browsing through the last items on a webpage

I have set up a jsfiddle that explains itself quite well: http://jsfiddle.net/nt7xzxur/ with the following smooth scrolling code: function smoothScroll(hash) { $('html, body').animate({ scrollTop: $(hash).offset().top }, 750); By clicking o ...

Creating an object efficiently by defining a pattern

As a newcomer to Typescript (and Javascript), I've been experimenting with classes. My goal is to create an object that can be filled with similar entries while maintaining type safety in a concise manner. Here is the code snippet I came up with: le ...

Utilize the power of dual API integration in a single request (multi-scope capability)

While there may not be a definitive answer to this question, I want to ensure that my situation cannot be resolved in any way. The crux of my application (and consequently the issue) is that each user possesses their own unique database and does not have ...

Changing the structure of a webpage in real-time and inserting new elements into the document

I have a custom x-template filled with a survey element (including a text field and radio button). Upon loading the screen, the database sends a JSON object to the UI based on previously stored sections. This JSON object is then used to populate the survey ...

Displaying a dynamic map with real-time coordinates sourced from a database using a combination of ajax and php

I'm currently facing an issue where my solution to retrieve coordinates for a specific place from a database and display a map centered on them is not working as expected. The problem seems to be arising because the map is being initialized without an ...

Sign up for an observable only when a specific variable has been modified

I am facing an issue where I need to restrict the usage of rxjs's subscribe method to only certain property changes. I attempted to achieve this using distinctUntilChanged, but it seems like there is something missing in my implementation. The specif ...

restrict the maximum character count in regex

The string can consist of a single number or multiple numbers separated by "-", but the total character count must not exceed 6. Examples of valid strings 5 55-33 4444-1 1-4444 666666 Examples of invalid strings -3 6666- 5555-6666 My initial regex / ...

Tips for integrating external JavaScript libraries and stylesheets into a widget

I am currently working on developing a custom Javascript widget that requires users to insert specific lines of code into their web pages. This code will then dynamically add an externally hosted javascript file, allowing me to inject HTML content onto the ...

Ways to invoke a function in Angular2 when the Boolean condition is met

Within my component class, I have implemented a popup function along with a Boolean flag that returns true or false based on specified conditions. In the template class, I want the popup function to be triggered when the flag becomes true, displaying a pop ...

Is it possible for consecutive json and jsonp requests to fail on Crossrider?

I am currently utilizing crossrider to develop a plugin that works across various browsers. Within my implementation, I have two consecutive AJAX requests (one JSON and one JSONP): The first request involves a JSON call for "login" which sets a brows ...

What is the best way to test speedy AJAX response times using Webdriver.io?

Currently, I am creating Cucumber.js tests using Webdriver.io. Everything seems to be going smoothly, but I'm encountering an issue with the mock server responding too quickly with AJAX. The "Loading..." message is not visible because the content load ...

Challenges faced when subscribing to global events in JavaScript

I have some questions about the JavaScript code snippet below: What does .events.slice(-1)[0][0] signify? Similarly, could you explain the purpose of nodes_params += "&ns=" + GLOBAL_EVENT + "," + start_from + ",-,-";? Could you elaborate on what is m ...

Refresh the page and navigate to the last active tab using PHP and jQuery jTable

After a user clicks the submit button on a jquery-jtable form, I am trying to reload the page and navigate to the tab that was active before the reload. However, my current solution only reloads the page without navigating to the previous tab. The tabs are ...

Step by step guide to implementing form step validation in a multi-step jQuery form with radio buttons

I have implemented the sample code provided in this link, with the addition of 2 extra form steps: LINK TO SAMPLE FORM My form consists of radio buttons (2 per page) instead of text boxes. How can I ensure that each form page is validated so that the for ...