Search code examples
discord-jda

i want auto role in jda, (when member join, add role by id)


i want when user join some discord guild, bot will add role by id to newcomer.

i have already use this code block, but that isn't work! `

if (((GuildMemberJoinEvent) event).getGuild().getName().equals("something name")){ System.out.println(((GuildMemberJoinEvent) event).getMember().getUser().getName() +((GuildMemberJoinEvent) event).getGuild()); Role r = ((GuildMemberJoinEvent) event).getGuild().getRoleById("1019811149602107402"); ((GuildMemberJoinEvent) event).getMember().getGuild().addRoleToMember(((GuildMemberJoinEvent) event).getMember().getUser(),r); } 

`


Solution

  • This is how to grant roles by id:

    event.getGuild().addRoleToMember(member, event.getGuild().getRoleById("1034143551568752682")).queue();
    

    Just put this in onGuildMemberJoin and check for guild name like you have here.

    Also please make your code readable