discord.js - Adding a reaction now triggers an immediate removal

https://i.sstatic.net/YbBSI.png

In my project using discord.js version 12.3.1, I am working on implementing a feature where users can react to a message and get assigned a role based on their reaction. For example, clicking on the 🔵 emoji gives you the 'BLUE' role. However, if you then click on 🟢, your 🔵 reaction is removed and you receive the 'GREEN' role instead.

The issue I'm facing is that adding the new role doesn't work consistently. Sometimes, even though the newly added role is not part of the collection of removed roles, it still gets instantly removed right after being added to the user.

Upon examining my code, here are the specific lines that I suspect might be causing the problem:

const addRole = guildRoles.find(role => role.name === colorRoles[emojiName])
// addRole: Role (e.g., with the name 'GREEN')
guildMember.roles.add(addRole)

const removeRoles = guildRoles.filter(role => Object.values(colorRoles).filter(color => color !== colorRoles[emojiName]).includes(role.name))
// removeRoles: Collection of Roles (e.g., with names 'BLUE', 'RED', 'YELLOW')
guildMember.roles.remove(removeRoles)

// At this point, 'GREEN' is added and removed; all other color roles are removed

You can find the full method along with more context on Pastebin

I'm struggling to understand why the newly added color role sometimes gets removed immediately and would greatly appreciate any advice on resolving this issue.

Answer â„–1

The issue at hand is that when using

guildMember.roles.remove(removeRoles)
, a Promise is returned, indicating that the operation will not be synchronous. To ensure proper execution flow, we must await its completion. To address this, we can utilize the await keyword when calling the function:

const removeRoles = guildRoles.filter(role => Object.values(colorRoles).filter(color => color !== colorRoles[emojiName]).includes(role.name));
await guildMember.roles.remove(removeRoles);

// Identify the Role representing the designated color and assign it to the GuildMember
const addRole = guildRoles.find(role => role.name === colorRoles[emojiName]);
await guildMember.roles.add(addRole);

Additionally, I have interchanged the order of removing and adding roles.

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

Why does the for loop function correctly with console.log but not with innerHTML?

Hello! I'm completely new to JavaScript, so please excuse any oversight... Below is the code that runs when a button on the page is clicked: function getNumbers() { var firstValue = document.getElementById("time_one").value; var ...

Can the Firefox Web Console be accessed while in headless mode?

Is it possible to interact with the Firefox console when starting Firefox in headless mode? Alternatively, is there a way to programmatically access the console in scripts? My attempts so far: I have been experimenting with the Javascript bindings to Se ...

Ways to determine if an element has exceeded its container's boundaries

After creating the codesandbox, I have developed a webapp that heavily relies on user input. To keep it simple for demonstration purposes, I am displaying various authors on an A4 formatted page using `page` and `font-size` with the `vw` unit for responsiv ...

Click to refresh a different component in ReactJS

My goal is to create a unique media player that can reload when a user wants to listen to an MP3 file. The concept is as follows: In media-player.js: - Display title, artist, and album art In programs.js: there is a button (LISTEN) that renders in medi ...

Delaying event listeners in Angular.js by using setTimeout within a factory or service

In order to delay the first iteration of the broadcast until the controller's $watchCollection is ready, I attempted using setTimeout() but it did not work as expected. Even trying with $timeout yielded the same result. What could be causing this issu ...

Tips for identifying and logging out a dormant user from the server side using Angular 2 Meteor

I'm currently diving into Angular 2 Meteor and working on a project that requires logging out the user when they close their browser window. I also need them to be redirected to the login page when they reopen the app. After searching online, I could ...

How can I translate "client.accept_invite" from discord.py to discord.py rewrite?

There doesn't seem to be any information in the documentation about a solution for this. Can someone guide me on how to tackle this issue? ...

Quality of check-boxes and radio-buttons

Looking for a list of checkboxes with a single selection option, similar to radio-buttons The ability to only select one checkbox is important. Although I need the functionality of checkboxes, such as being able to deselect them with an additional click, ...

The Dialog feature offered by jQuery-UI

Having just started with jQuery, I am looking to implement the jQuery-UI Dialog to display a message containing lengthy text to the user. My goal is to have a "More details" link in each row of my HTML table that will trigger the jQuery Dialog window to op ...

Guide on setting the HTML img src value with AR.ImageResource in ARchitectBrowser.html while utilizing the Wikitude SDK for iOS

I am currently customizing the ARchitectBrowser.html file provided by the Wikitude SDK for iOS and I am interested in finding a way to directly insert an image set in AR.ImageResource. bubbleImage = new AR.ImageResource("http://graph.facebook.com/" + json ...

AngularJS: accessing remote systems - a guide

I am looking to explain my objective clearly I need guidance on how to establish a remote desktop connection from my Angular.js application to a windows application running system. The server I am using is Google App Engine. My current ideas: The Windo ...

Extracting properties from an object in NodeJS: a step-by-step guide

I'm struggling to access a specific property within an object using a GET request in NodeJs (with express). Here is the object I am working with: const friends = [{ id: 1, name: "laura", country: "England", language ...

How can I incorporate a personalized checkbox into a column within a React material table?

I'm currently working on a React project where I am using a Material Table. I am trying to figure out how to add a checkbox in a table cell, for example, in the Birth year column instead of just having the year displayed. Can anyone provide guidance o ...

Creating a dynamic dropdown in a Rails application using the simple_form gem

In my Rails application, I have a straightforward has_many and belongs_to relationship. Using simple_form, I am looking to dynamically adjust the dropdown options based on the selection made by the user. Models class Processor < ApplicationRecord ...

Change the sound attributes when an A-frame is clicked

I'm currently working with A-frame () in my scene and have added some sound to it. Along with the sound, I have a button placed at the top of the screen. My question is how can I modify the code so that when the button is clicked, the rolloffFactor of ...

Steps for triggering a function when the 'Enter' key is pressed in a TextInput in a React Native application

I'm currently working on creating a Search Bar feature. I would like the user to input text into the TextInput field, and when they press 'Enter', I want it to trigger my function. <TextInput //when the user presses Enter, call the functi ...

Where did my HTML5 Canvas Text disappear to?

I am encountering a common issue with my code and could use some guidance. Despite numerous attempts, I can't seem to figure out why it isn't functioning properly. To better illustrate the problem, here is a link to the troublesome code snippet o ...

Associative TypeScript Arrays

I'm attempting to organize reservations based on business ID in order to achieve a specific end result. Here is the desired output: [ [businessID1] => [Object1,Object2, Object3], [businessID2] => [Object1,Object2], [businessID3] => [Obje ...

tidying up state management in react

My function, getLines, is asynchronous and time-consuming as it determines when text will wrap onto another line. Additionally, I have an API that returns a string of a specified length (query). import { useState, useEffect } from "react"; import ...

Using Leaflet JS to implement multiple waypoints on a map

I am trying to create a route with multiple waypoints without hardcoding any data. My waypoints array should dynamically include latitude and longitude values. How can I achieve this? var data = [ { "title": 'Chennai', " ...