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

Commit

Permalink
Extra permission handling
Browse files Browse the repository at this point in the history
  • Loading branch information
jonteohr committed Apr 23, 2020
1 parent 79959f0 commit b53b2fb
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
5 changes: 5 additions & 0 deletions src/main/java/com/jonteohr/discord/guardian/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class App {
public static String image = "https://netcube.xyz/guardian.png";

public static Collection<Permission> permissions = new ArrayList<Permission>();
public static Collection<Permission> channelPerms = new ArrayList<Permission>();

public static void main(String[] args) throws LoginException {
PropertyHandler prop = new PropertyHandler();
Expand Down Expand Up @@ -63,6 +64,10 @@ public static void main(String[] args) throws LoginException {
permissions.add(Permission.MESSAGE_READ);
permissions.add(Permission.MESSAGE_HISTORY);
permissions.add(Permission.VOICE_CONNECT);

channelPerms.add(Permission.MESSAGE_READ);
channelPerms.add(Permission.MANAGE_CHANNEL);
channelPerms.add(Permission.MANAGE_PERMISSIONS);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.jonteohr.discord.guardian.permission.PermissionCheck;
import com.jonteohr.discord.guardian.sql.Channels;

import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.PermissionOverride;
import net.dv8tion.jda.api.entities.Role;
import net.dv8tion.jda.api.entities.TextChannel;
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
Expand Down Expand Up @@ -33,10 +35,10 @@ public void onGuildMessageReceived(GuildMessageReceivedEvent e) {
return;
}

TextChannel target = e.getMessage().getMentionedChannels().get(0);
TextChannel targetChannel = e.getMessage().getMentionedChannels().get(0);
String password = args[2];

if(channels.isChannelProtected(target)) { // Tagged channel is already protected
if(channels.isChannelProtected(targetChannel)) { // Tagged channel is already protected
e.getChannel().sendMessage(":x: **Channel already protected!**").queue();
return;
}
Expand All @@ -46,17 +48,29 @@ public void onGuildMessageReceived(GuildMessageReceivedEvent e) {
return;
}

if(!e.getGuild().getSelfMember().hasPermission(Permission.ADMINISTRATOR)) {
PermissionOverride permOverride = targetChannel.getPermissionOverride(App.getSelfRole(e.getGuild()));
if(!permOverride.getAllowed().containsAll(App.channelPerms)) {
String perms = "";
for(Permission perm : App.channelPerms) {
perms = perms + "`" + perm.getName() + "`\n";
}
e.getChannel().sendMessage(":x: **Channel permissions insufficient!**\nI need these permissions in the channel:\n" + perms).queue();
return;
}
}

// Create a role for the channel and remember it's ID!
Role accessRole = e.getGuild().createRole()
.setName(target.getName())
.setName(targetChannel.getName())
.complete();

if(!channels.protectChannel(target, password, accessRole)) { // Something with the Query probably went wrong
if(!channels.protectChannel(targetChannel, password, accessRole)) { // Something with the Query probably went wrong
e.getChannel().sendMessage(":x: **Something went wrong.**").queue();
return;
}

e.getChannel().sendMessage(":white_check_mark: Channel " + target.getAsMention() + " is now password protected!").queue();
e.getChannel().sendMessage(":white_check_mark: Channel " + targetChannel.getAsMention() + " is now password protected!").queue();
return;
}
}

0 comments on commit b53b2fb

Please sign in to comment.