Jda: Add and remove a role, only one works

Created on 12 Oct 2017  路  5Comments  路  Source: DV8FromTheWorld/JDA

Hello,

I'm really blocked since few day.
Context :
A member want validate a newbie. He write $validation @newbie_name and, after verification, I remove "newbie role" then add "Member role".

Here is my method and the problem is identified by comment :

private void validation_membres(User user, MessageChannel channel, String[] arguments, Guild guild, Member demandeur) {
        String joueurAvalider = "";
        GuildController gc = new GuildController(guild);

        for(int i = 0; i < arguments.length; i++) {
            joueurAvalider += arguments[i] + " ";
        }
        Role role_membres = guild.getRolesByName("Membre", true).get(0);
        Role role_newbies = guild.getRolesByName("newbies", true).get(0);       

        //if demandeur is member so he can validate a newbie
        if(demandeur.getRoles().contains(role_membres)) {
            //if joueur is a member of serveur
            if(joueurAvalider.startsWith("@")) {
                joueurAvalider = joueurAvalider.replace("@", "");
                Member avalider = guild.getMembersByEffectiveName(joueurAvalider.trim(), false).get(0);

                //if joueur is a newbie
                if(avalider.getRoles().contains(role_newbies)) {
                    System.out.println("newbies oui");
                    //Validation running
                    //---------Here is the problem
                    gc.removeRolesFromMember(avalider, role_newbies).queue();
                    gc.addRolesToMember(avalider, role_membres).queue();


                    if(!avalider.getRoles().contains(role_newbies) && avalider.getRoles().contains(role_membres)) {
                        channel.sendMessage(":green_book: Joueur valid茅 !").queue();
                    }
                }
                else { //player not a newbie
                    channel.sendMessage(":closed_book: Le joueur " + avalider.getEffectiveName() + " n'est pas un " + role_newbies.getName() + ". Il ne peut pas 锚tre valid茅.").queue();
                }
            }
            else { //player unknown on server
                channel.sendMessage(":closed_book: Le joueur 脿 valider doit 锚tre tagu茅. Cel脿 permet de v茅rifier qu'il est connu du serveur.").queue();
            }
        }
        else { //demandeur not a member
            channel.sendMessage(":closed_book: Uniquement les membres peuvent valider des joueurs.").queue();
        }
    }

I have permissions for this because when I do one by one, that works. With this code, role is add but no remove.

I'm lost. This morning, I've found this : Same problem

But if I do : guild.getManager(). I don't have Add or remove ..

Have you any ideas?
Thank you

Most helpful comment

Well read my comment again edited it 馃憖 sorry

All 5 comments

Consult the documentation of GuildController#addRolesToMember

There is a huge warning explaining your issue:

Warning

This may not be used together with any other role add/remove/modify methods for the same Member within one event listener cycle! The changes made by this require cache updates which are triggered by lifecycle events which are received later. This may only be called again once the specific Member has been updated by a GuildMemberRoleAddEvent.
To add _and_ remove Roles from a Member you should use modifyMemberRoles(Member, Collection, Collection)

The issue you linked is from an old version of JDA (2.x) in which this functionality was different.

you are contructing a GuildController use Guild#getController
You can use gc.addSingleRoleToMember and gc.removeSingleRoleFromMember or the way minn mentioned

Blocked since 2 days and 1 hour after this post, i found this :

  • removeSingleRoleFromMember
  • addSingleRoleFromMember
    It's work.

But Thank you @MinnDevelopment, I will try with modifyMemberRoles.
Have a nice day.

Well read my comment again edited it 馃憖 sorry

Was this page helpful?
0 / 5 - 0 ratings

Related issues

napstr picture napstr  路  3Comments

Frederikam picture Frederikam  路  4Comments

AutismSpeaks picture AutismSpeaks  路  4Comments

MinnDevelopment picture MinnDevelopment  路  5Comments

Bastian picture Bastian  路  5Comments