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

Commit

Permalink
Bot now works with Admin and without.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonteohr committed Apr 23, 2020
1 parent d7ff180 commit bd51768
Show file tree
Hide file tree
Showing 4 changed files with 59 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.1_5</version>
<version>1.0.2</version>

<name>guardian</name>
<!-- FIXME change it to the project's website -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.Activity;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.TextChannel;
Expand Down Expand Up @@ -56,7 +57,7 @@ private void newGuild(Guild guild) {
msg.setThumbnail(App.image);

msg.setDescription("I'm very excited to join you!\n"
+ "My setup is really easy. There is none.\n\n"
+ "My setup is really easy. I'll even walk you through it right here!\n\n"
+ "To password protect a channel, simply use the command `" + App.prefix + "protect <#channel> <password>`.\n"
+ "Note that you can only set one password per channel. Also make sure you actually tag the channel with the `#` character.\n\n"
+ "If you wish to un-protect a channel, then simply do `" + App.prefix + "unprotect <#channel>`.\n\n"
Expand All @@ -65,6 +66,22 @@ 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)) {
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.");
emb.appendDescription("\nHere's a list of permissions I need in the server.");
String permList = "";
for(Permission perm : App.permissions) {
permList = permList + perm.getName() + "\n";
}
emb.addField("Permissions", permList, false);

channel.sendMessage(emb.build()).queue();
}

channel.sendMessage(msg.build()).queue();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.jonteohr.discord.guardian.permission;

import com.jonteohr.discord.guardian.App;

import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.TextChannel;

public class PermissionErr {
public static void BotPerm(TextChannel channel) {
EmbedBuilder msg = new EmbedBuilder();

msg.setColor(0xD52D42);
msg.setAuthor("Permission error", null, "http://guardianbot.xyz/attention-clipart-warning-triangle-2.png");
msg.setDescription("I'm missing one or more permissions! Make sure I have the permissions listed down below, if not I will not work.");

String srvPermList = "";
for(Permission perm : App.permissions) {
srvPermList = srvPermList + perm.getName() + "\n";
}

msg.addField("Server Permissions", srvPermList, false);

channel.sendMessage(msg.build()).queue();
}
}
23 changes: 14 additions & 9 deletions src/main/java/com/jonteohr/discord/guardian/sql/Channels.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import java.util.ArrayList;
import java.util.List;

import com.jonteohr.discord.guardian.permission.PermissionErr;

import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.PermissionOverride;
Expand Down Expand Up @@ -55,17 +57,20 @@ public boolean protectChannel(TextChannel channel, String password, Role role) {
if(!sql.queryExec("INSERT INTO channels(`guild_id`,`channel`,`password`,`role`) VALUES('" + guild.getId() + "','" + chId + "','" + password + "','" + roleId + "');"))
return false;

// Grants access to the channel for the new role
channel.putPermissionOverride(role)
.setAllow(Permission.VIEW_CHANNEL)
.queue(
null,
failure -> PermissionErr.BotPerm(channel.getGuild().getDefaultChannel())
);

// Hides the channel for @everyone
PermissionOverride permOverride = channel.getPermissionOverride(channel.getGuild().getPublicRole());
if(permOverride == null)
channel.putPermissionOverride(channel.getGuild().getPublicRole()).setDeny(Permission.VIEW_CHANNEL).queue();
channel.putPermissionOverride(channel.getGuild().getPublicRole()).setDeny(Permission.VIEW_CHANNEL).queue(null, error -> PermissionErr.BotPerm(channel.getGuild().getDefaultChannel()));
else
permOverride.getManager().deny(Permission.VIEW_CHANNEL).queue();

// Grants access to the channel for the new role
channel.putPermissionOverride(role)
.setAllow(Permission.VIEW_CHANNEL)
.queue();
permOverride.getManager().deny(Permission.VIEW_CHANNEL).queue(null, error -> PermissionErr.BotPerm(channel.getGuild().getDefaultChannel()));

return true;
}
Expand All @@ -92,9 +97,9 @@ public boolean unProtectChannel(TextChannel channel, Role role) {
// Un-hides the channel for @everyone
PermissionOverride permOverride = channel.getPermissionOverride(channel.getGuild().getPublicRole());
if(permOverride == null)
channel.putPermissionOverride(channel.getGuild().getPublicRole()).clear(Permission.VIEW_CHANNEL).queue();
channel.putPermissionOverride(channel.getGuild().getPublicRole()).clear(Permission.VIEW_CHANNEL).queue(null, error -> PermissionErr.BotPerm(channel.getGuild().getDefaultChannel()));
else
permOverride.getManager().clear(Permission.VIEW_CHANNEL).queue();
permOverride.getManager().clear(Permission.VIEW_CHANNEL).queue(null, error -> PermissionErr.BotPerm(channel.getGuild().getDefaultChannel()));

return true;
}
Expand Down

0 comments on commit bd51768

Please sign in to comment.