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

Commit

Permalink
Permission fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jonteohr committed Apr 22, 2020
1 parent 22a23af commit 12e8a03
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ db.pass=password
---

### Todo
1. Password protect voice channels
1. Password protect voice channels
2. Finish Wiki pages
13 changes: 9 additions & 4 deletions src/main/java/com/jonteohr/discord/guardian/events/OnDirect.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,19 @@ public void onPrivateMessageReceived(PrivateMessageReceivedEvent e) {
}

String guildName = "";
for(int i = 1; i < args.length; i++)
guildName = guildName + args[i] + " ";
for(int i = 1; i < args.length; i++) {
if(i == (args.length - 1)) {
guildName = guildName + args[i];
} else {
guildName = guildName + args[i] + " ";
}
}

List<String> guildNames = new ArrayList<String>();
for(int i = 0; i < guilds.size(); i++)
guildNames.add(e.getJDA().getGuildById(guilds.get(i)).getName().toLowerCase());

if(!passwords.contains(password) || !guildNames.contains(args[1].toLowerCase())) {
if(!passwords.contains(password) || !guildNames.contains(guildName.toLowerCase())) {
e.getChannel().sendMessage("The password or server name you entered is incorrect.").queue();
return;
}
Expand All @@ -60,7 +65,7 @@ public void onPrivateMessageReceived(PrivateMessageReceivedEvent e) {
TextChannel channel = guild.getTextChannelById(channels.get(i));
String pw = passwords.get(i);
Role role = guild.getRoleById(roles.get(i));

if(guild.getName().equalsIgnoreCase(guildName)) {
if(!guild.getTextChannels().contains(channel)) // if the channel is not in this server
continue;
Expand Down
12 changes: 9 additions & 3 deletions src/main/java/com/jonteohr/discord/guardian/sql/Channels.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,13 @@ public boolean protectChannel(TextChannel channel, String password, Role role) {

// Hides the channel for @everyone
PermissionOverride permOverride = channel.getPermissionOverride(channel.getGuild().getPublicRole());
permOverride.getManager().deny(Permission.VIEW_CHANNEL).queue();
if(permOverride == null)
channel.putPermissionOverride(channel.getGuild().getPublicRole()).setDeny(Permission.VIEW_CHANNEL).queue();
else
permOverride.getManager().deny(Permission.VIEW_CHANNEL).queue();

// Grants access to the channel for the new role
channel.createPermissionOverride(role)
channel.putPermissionOverride(role)
.setAllow(Permission.VIEW_CHANNEL)
.queue();

Expand Down Expand Up @@ -88,7 +91,10 @@ public boolean unProtectChannel(TextChannel channel, Role role) {

// Un-hides the channel for @everyone
PermissionOverride permOverride = channel.getPermissionOverride(channel.getGuild().getPublicRole());
permOverride.getManager().clear(Permission.VIEW_CHANNEL).queue();
if(permOverride == null)
channel.putPermissionOverride(channel.getGuild().getPublicRole()).clear(Permission.VIEW_CHANNEL).queue();
else
permOverride.getManager().clear(Permission.VIEW_CHANNEL).queue();

return true;
}
Expand Down

0 comments on commit 12e8a03

Please sign in to comment.