Capture the attention of users through directives

I'm currently working on a script that checks for any mentioned users and then saves them to an array. This allows me to later apply methods to these users.

For instance, assigning a role to a user.


Capture the mentioned user(s) in the message (message.mentions.users) and store them in an array
Assign a specified role (or another method) to the mentioned user(s)


Answer №1

Scan for the mentioned user(s) and store them in an array

This step is unnecessary. The variable message.mentions.users already contains an array of users. If desired, you can save it to another variable. However, when assigning a role to each mentioned user, you only need to execute a command for each of them.

To achieve this, utilize the .forEach() method:

var userlist = message.mentions.users; // Save userlist to a variable
userlist.forEach(function(user){
    console.log(user); // Logs each mentioned user
});

This approach allows you to easily run a command for each individual mentioned user.

However, to assign roles to these users, you must use the Back-End API (HTTP Requests)

PUT/DELETE /api/guilds/{guildId}/members/{userId}/roles/{roleId} [1]

Perform HTTP Requests using jQuery's ajax: (Remember, the base URL for all Discord requests should be ):

$.ajax({
    url: '/api/guilds/{guildId}/members/{userId}/roles/{roleId}',
    type: 'PUT', // Or delete
    success: function(result) {
        // Handle the result
    }
});

You only need to convert message.mentions.users to their IDs, achievable with the .forEach() loop.

UPDATE

Example:

var userlist = message.mentions.users; // Save userlist to a variable
userlist.forEach(function(user){
    $.ajax({
        url: '/api/guilds/' + message.guild.id + '/members/' + user + '/roles/{roleId}', // Role ID
        type: 'PUT', // Or delete
        success: function(result) {
            // Handle the result
        };
    });
});

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

The process of delineating a specific area within a surface plot using Matlab

My 2D array is stored in the variable data and I use the function surf to plot it. I have identified a specific region within this surface plot where the values of data exceed 0.9*max(max(data)). My goal is to create an 'outline' around this regi ...

Having trouble accessing a React component class from a different component class

I just started learning reactjs and javascript. For a simple project, I'm working on creating a login and registration form. The issue I'm facing is that when a user enters their email and password and clicks 'register', instead of movi ...

I desire to activate the textbox only when the radiobtnlist value equals 1

I am trying to disable a textbox based on the selected value of a RadioButtonList in my code. However, the functionality is not working as expected and I am unsure why. <script type="text/javascript"> $(function () { $("#RadioButton ...

ReactJS: Checkbox status remains consistent through re-rendering of Component

I have developed a JSfiddle example Initially, this fiddle displays a list of checkboxes based on the passed props to the component. When you click the Re-render button, the same component is rendered with different props. Now, please follow these steps- ...

JavaScript variables can be declared as static to maintain a fixed value throughout the

Creating a signup and signin process using HTML and Javascript involves having two HTML pages - one for sign up and one for sign in, along with a shared Javascript file. The challenge is accessing a variable from the sign up page on the sign in page after ...

Steps for storing div content (image) on server

Currently in the process of developing a web application that allows users to crop images. The goal is for users to have the ability to email the URL so others can view the cropped image, ensuring that the URL remains active indefinitely by storing every c ...

What is the best way to add elements to an array that has a predetermined fixed size?

I am looking to create a program that involves inserting elements into an array: The initial elements of the array 20 34 45 2 10 Please provide the index and number for insertion 3 12 Elements after insertion 20 34 45 12 2 10 To start, I define an array ...

What is the process for transferring a file to a server and then returning it to the client?

Currently, I am working on manipulating PDF files in ReactJS and making modifications to them on the server side. However, I am facing an issue with passing my file data to the server side and then receiving it back. This is how I retrieve my file: <di ...

Obtain the reconstructed on-site dispatch mechanism from withReducer in conjunction with withHandlers

I have a situation where I have a component that is already connected to the redux store and has access to the dispatch function. In an attempt to update the local state of this component, I am utilizing withReducer and withHandlers in the following manner ...

What is the best way to handle parsing a JSON response from Flicker that does not include a

I've been working on a flicker application and noticed that Flickr uses a different type of JSONP callback. How can I parse this URL () as part of my JSONP callback, replacing my existing URL? My current URL is not providing page numbers even when th ...

What purpose does the by.js locator serve in Protractor/WebDriverJS?

Recently, I've come across a new feature in the Protractor documentation - the by.js(): This feature allows you to locate elements by evaluating a JavaScript expression, which can be either a function or a string. While I understand how this locat ...

`Searching for the perfect code: Collection of jTextField elements`

I've been searching everywhere online for a specific code snippet: JTextField[] jt = new JTextField{jTextField1,jTextField2,..}; Previously, I had this exact code saved on a hard drive in case I needed it again. Unfortunately, the hard drive failed ...

The onclick function set in HTML by updating innerHTML is not functioning properly

I'm encountering an issue with my JS function called buyAnimal(id). When I set my index.html to include <div onclick="buyAnimal(0)"></div>, it works perfectly. However, if I do let html = `<div onclick="buyAnimal(0)" ...

Maintaining an object's position steady in relation to the camera in Three.js

I am trying to figure out how to maintain an object's position relative to the camera. Specifically, I want one object to be viewable from all angles using a trackball camera, while another object should always stay in the same position relative to th ...

Ways to switch text on and off smoothly using transitions

I have created a webpage where text starts off hidden but has a visible heading. When you click on the heading, the text is revealed below. I am putting the final touches and aiming to make the text slide in smoothly. I am using Javascript for toggling ins ...

Angular HTML prints only halfway down the page when document is printed

While utilizing document.write(), I encountered an issue when printing the contents of an object specifically formatted for a printer. The text started printing only halfway down the page. I can successfully print the screen without any problems, which ma ...

Utilizing HTML5 data attributes to store intricate JSON objects and manipulate them using JavaScript

Recently, I encountered a unique challenge that I set for myself... I am currently in the process of developing an advanced ajax content loader plugin that comes with an array of options and callbacks. In order to streamline the initialization process and ...

Java - Accessing an array generated within a void method

I am grateful to those who supported me on this journey. I have made some revisions. However, I am encountering an error on the line where array1 is being assigned in the method, Method(). (see edited version below) class NewJavaApp { int[] array1; publi ...

Tips on sending jQuery ng-repeat values {{x.something}} to a JavaScript API URL such as /api/get_now/{{x.something}}

Is there a way to send {{x.something}} from a jQuery ng-repeat loop to a JavaScript URL? $(document).ready(function () { let address = '<?=ROOT_ADDRESS?>/api/get_now/{{x.something}}'; $.ajax({ url: a ...

Verify whether all of the iframes have finished loading

I am facing an issue with running a function when multiple iframes on the page are loaded using jQuery. The function works fine when there is only one iframe, but as soon as there are more than one, it stops working. Below is the code I am currently using: ...