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

Commit

Permalink
Even more permission error handling..
Browse files Browse the repository at this point in the history
  • Loading branch information
jonteohr committed Apr 23, 2020
1 parent b53b2fb commit ec4da37
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 11 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.2_1</version>
<version>1.0.2_4</version>

<name>guardian</name>
<!-- FIXME change it to the project's website -->
Expand Down
13 changes: 10 additions & 3 deletions src/main/java/com/jonteohr/discord/guardian/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ public static void main(String[] args) throws LoginException {
// PM
jda.addEventListener(new OnDirect());

permissions.add(Permission.MANAGE_CHANNEL);
permissions.add(Permission.MANAGE_SERVER);
permissions.add(Permission.MANAGE_ROLES);
permissions.add(Permission.MANAGE_CHANNEL);
permissions.add(Permission.NICKNAME_CHANGE);
permissions.add(Permission.VIEW_CHANNEL);
permissions.add(Permission.MANAGE_PERMISSIONS);
permissions.add(Permission.NICKNAME_CHANGE);
permissions.add(Permission.MESSAGE_WRITE);
permissions.add(Permission.MESSAGE_READ);
permissions.add(Permission.MESSAGE_HISTORY);
Expand All @@ -76,6 +76,13 @@ public static void main(String[] args) throws LoginException {
* @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);
if(guild.getSelfMember().getRoles().stream().filter(Role::isManaged).findFirst().orElse(null) == null) { // If the bot didn't create its own role
if(guild.getRolesByName(guild.getSelfMember().getUser().getName(), true).size() > 0) // Get a role with name == bot name and make sure we can find something
return guild.getRolesByName(guild.getSelfMember().getUser().getName(), true).get(0);

return null; // The role doesn't exist
}

return guild.getSelfMember().getRoles().stream().filter(Role::isManaged).findFirst().orElse(null); // Return the bots' own managed role
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ public void onGuildMessageReceived(GuildMessageReceivedEvent e) {
}

if(!e.getGuild().getSelfMember().hasPermission(App.permissions)) {
e.getChannel().sendMessage(":x: **Permissions are not correct!**\nMake sure the I have the requested permissions from the invite-link. Else I won't work properly!").queue();
String perms = "";
for(Permission perm : App.permissions) {
perms = perms + "`" + perm.getName() + "`\n";
}
e.getChannel().sendMessage(":x: **Permissions are not correct!**\nMake sure the I have the following server permissions:\n" + perms).queue();
return;
}

Expand All @@ -49,7 +53,7 @@ public void onGuildMessageReceived(GuildMessageReceivedEvent e) {
}

if(!e.getGuild().getSelfMember().hasPermission(Permission.ADMINISTRATOR)) {
PermissionOverride permOverride = targetChannel.getPermissionOverride(App.getSelfRole(e.getGuild()));
PermissionOverride permOverride = targetChannel.getPermissionOverride(e.getGuild().getSelfMember());
if(!permOverride.getAllowed().containsAll(App.channelPerms)) {
String perms = "";
for(Permission perm : App.channelPerms) {
Expand Down
12 changes: 10 additions & 2 deletions src/main/java/com/jonteohr/discord/guardian/events/GuildReady.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,16 @@ private void newGuild(Guild guild) {
+ "But they will need the password to gain access to a protected channel.\n\n"
+ "That was all for me now! Good luck!");

if(!App.getSelfRole(guild).hasPermission(Permission.ADMINISTRATOR)) {
if(!guild.getSelfMember().hasPermission(Permission.ADMINISTRATOR)) {
String perms = "";
for(Permission perm : App.channelPerms) {
perms = perms + "`" + perm.getName() + "` ";
}
EmbedBuilder emb = new EmbedBuilder();
emb.setAuthor("Permission warning", null, "http://guardianbot.xyz/attention-clipart-warning-triangle-2.png");
emb.setColor(0xD52D42);
emb.setDescription("I don't seem to have administrator access. That's fine! I can still do my job.\n"
+ "However you have to make sure I have the `" + Permission.MANAGE_CHANNEL.getName() + "`, `" + Permission.MANAGE_PERMISSIONS.getName() + "` and `" + Permission.MESSAGE_READ.getName() + "` permissions in the channel you want me to work inside.");
+ "However you have to make sure I have the " + perms + " permissions in the channel you want me to work inside.");
emb.appendDescription("\nHere's a list of permissions I need in the server.");
String permList = "";
for(Permission perm : App.permissions) {
Expand All @@ -83,5 +87,9 @@ private void newGuild(Guild guild) {
}

channel.sendMessage(msg.build()).queue();

if(App.getSelfRole(guild) == null || !guild.getSelfMember().getRoles().contains(App.getSelfRole(guild))) { // If the bot doesn't have a role assigned, or the role does not exist
channel.sendMessage(":x: **I don't have a role assigned to me!**\nI need a role assigned to me. Create one, give it the correct permissions as listed above and make sure to name it `Channel Guardian`!").queue();
}
}
}
11 changes: 8 additions & 3 deletions src/main/java/com/jonteohr/discord/guardian/sql/AssignRole.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,14 @@ public class AssignRole {
public boolean grantUserAccess(User user, Guild guild, Role role) {
Member member = guild.getMember(user);

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

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

return false;
Expand Down

0 comments on commit ec4da37

Please sign in to comment.