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
};
});
});