Stress_
Stress_

Reputation: 400

Checking if a member has a certain role

I'm trying to only allow a certain role to view a section of the help command. I've tried the following, but it doesn't seem to work and it just shows the default help embed:

List<Role> staff = event.getGuild().getRolesByName("Staff", true);

                if (event.getGuild().getSelfMember().getRoles().contains(staff)) {

                    final EmbedBuilder ahelp = new EmbedBuilder();

                    ahelp.setTitle("Commands List", null);
                    ahelp.setColor(Color.CYAN);
                    ahelp.addField("Member:", "*help: Returns with this message." +
                            "\n*bugreport <Bug>: Creates a new bug report." +
                            "\n*suggest <Suggestion>: Creates a new suggestion."
                            , false);
                    ahelp.addField("Staff:", "*ban <Member> [Reason]: Bans a member." +
                            "\n*mute <Member> [Reason]: Mutes a member." +
                            "\n*kick <Member> [Reason]: Kicks a member." +
                            "\n*unmute <Member>: Unmutes a member."
                            , false);
                    ahelp.setFooter("Bot • " + dtf.format(now));

                    event.getChannel().sendTyping().queue();
                    event.getChannel().sendMessage(ahelp.build()).queue();
                    return;

                } else {

                    final EmbedBuilder help = new EmbedBuilder();

                    help.setTitle("Commands List", null);
                    help.setColor(Color.CYAN);
                    help.addField("Member:", "*help: Returns with this message." +
                            "\n*bugreport <Bug>: Creates a new bug report." +
                            "\n*suggest <Suggestion>: Creates a new suggestion."
                            , false);
                    help.setFooter("Bot • " + dtf.format(now));

                    event.getChannel().sendTyping().queue();
                    event.getChannel().sendMessage(help.build()).queue();
                    return;

                }

If there is a better way, or if you know how to answer my question please let me know. Thanks.

Upvotes: 0

Views: 307

Answers (1)

Minn
Minn

Reputation: 6131

You are trying to check if a List<Role> contains another List<Role>. This is not how contains works. The correct usage would be with a Role object rather than a List<Role> object.

Since I assume you are looking for just the fact that the member has a role with the name "Staff" you can use the Stream API:

public boolean hasRole(Member member, String name) {
    return member.getRoles().stream().map(Role::getName).anyMatch(name::equalsIgnoreCase);
}

if (hasRole(event.getGuild().getSelfMember(), "Staff")) {
   // ... your code here ...
}

You also only check the "self member" which means you only check the roles of the bot itself rather than the roles of the user. I think you might want to use event.getMember() instead?

Upvotes: 3

Related Questions