JDA - 如果添加了某个角色,则为用户添加角色

JDA - Add Role to User if a certain Role is added

我正在尝试编写一个 discord 机器人,如果向他添加了一个角色,它就会为用户添加一个新角色。我卡在角色添加部分了。

这是我的代码。希望有人能帮忙:

import net.dv8tion.jda.api.events.guild.member.GuildMemberRoleAddEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;

import java.util.List;

public class TeamAutorole extends ListenerAdapter {
    @Override
    public void onGuildMemberRoleAdd(GuildMemberRoleAddEvent e) {
        if(e.getRoles().toString().contains("\uD83D\uDC6ESupporter")) {


        }
    }
}

首先你要检查,如果成员的角色列表包含你想要的角色,然后根据它添加另一个角色。

//gets role with name "\uD83D\uDC6ESupporter"
Role supporter = e.getGuild().getRolesByName("\uD83D\uDC6ESupporter", true).get(0);

//gets role with name "Dummy"
Role dummy = e.getGuild().getRolesByName("Dummy", true).get(0);

//check if the list of roles of the user contains the role supporter
if (e.getMember().getRoles().contains(supporter)) {
    
    //adds role dummy to member
    e.getGuild().addRoleToMember(e.getMember(), dummy).queue();

}