Skip to content
This repository has been archived by the owner on Jun 6, 2021. It is now read-only.

Commit

Permalink
Role hierarchy handling
Browse files Browse the repository at this point in the history
  • Loading branch information
jonteohr committed Apr 22, 2020
1 parent 04aee8b commit 269723f
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.jonteohr.discord</groupId>
<artifactId>guardian</artifactId>
<version>1.0.1_3</version>
<version>1.0.1_4</version>

<name>guardian</name>
<!-- FIXME change it to the project's website -->
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/jonteohr/discord/guardian/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.OnlineStatus;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Role;
import net.dv8tion.jda.api.requests.GatewayIntent;

public class App {
Expand Down Expand Up @@ -59,4 +61,13 @@ public static void main(String[] args) throws LoginException {
permissions.add(Permission.VOICE_CONNECT);
permissions.add(Permission.MANAGE_PERMISSIONS);
}

/**
* Returns the Role that belongs to the bot.
* @param guild a {@link net.dv8tion.jda.api.entities.Guild Guild} object to look in.
* @return the bots owned {@link net.dv8tion.jda.api.entities.Role Role} or {@code null} if none
*/
public static Role getSelfRole(Guild guild) {
return guild.getSelfMember().getRoles().stream().filter(Role::isManaged).findFirst().orElse(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ public void onPrivateMessageReceived(PrivateMessageReceivedEvent e) {
if(!pw.equalsIgnoreCase(password)) // if the wrong pw for the channel/server was given
continue;

assignRole.grantUserAccess(e.getAuthor(), guild, role);
if(!assignRole.grantUserAccess(e.getAuthor(), guild, role)) {
e.getChannel().sendMessage(guild.getName() + " has an error with the roles. Please contact an server administrator to inform them about this!").queue();
break;
}
e.getChannel().sendMessage("Correct! I have given you a role to access the channel " + channel.getName() + " inside server " + guild.getName()).queue();
break;
} else {
Expand Down
11 changes: 9 additions & 2 deletions src/main/java/com/jonteohr/discord/guardian/sql/AssignRole.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.jonteohr.discord.guardian.sql;

import com.jonteohr.discord.guardian.App;

import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.Role;
Expand All @@ -13,9 +15,14 @@ public class AssignRole {
* @param guild the {@link net.dv8tion.jda.api.entities.Guild Guild} to work inside.
* @param role the {@link net.dv8tion.jda.api.entities.Role Role} to assign.
*/
public void grantUserAccess(User user, Guild guild, Role role) {
public boolean grantUserAccess(User user, Guild guild, Role role) {
Member member = guild.getMember(user);

guild.addRoleToMember(member, role).complete();
if(role.getPosition() < App.getSelfRole(guild).getPosition()) { // Make sure hierarchy is correct for the guild
guild.addRoleToMember(member, role).complete();
return true;
}

return false;
}
}

0 comments on commit 269723f

Please sign in to comment.