use of st.photonbur.Discord.Bot.lightbotv3.entity.bannable.BannableEntity in project TheLighterBot by PhotonBursted.
the class ChannelPermissionController method applyAccessList.
/**
* Applies the properties of an accesslist to a certain channel right before it is constructed.
*
* @param c The channel to apply permissions to
* @param perms The permissions to apply
*/
@SuppressWarnings("ResultOfMethodCallIgnored")
private void applyAccessList(Channel c, Permission... perms) {
Guild g = c.getGuild();
// Figures out the blacklist and whitelist present in this guild
Set<BannableEntity> blacklist = l.getAccesslistController().getBlacklistForGuild(g);
Set<BannableEntity> whitelist = l.getAccesslistController().getWhitelistForGuild(g);
ChannelManager manager = c.getManager();
if (blacklist != null) {
for (BannableEntity item : blacklist) {
if (item.isOfClass(Role.class)) {
manager = manager.putPermissionOverride((Role) item.get(), 0L, Permission.getRaw(perms));
} else if (item.isOfClass(User.class)) {
manager = manager.putPermissionOverride(g.getMember((User) item.get()), 0L, Permission.getRaw(perms));
}
}
}
if (whitelist != null) {
for (BannableEntity item : whitelist) {
if (item.isOfClass(Role.class)) {
manager = manager.putPermissionOverride((Role) item.get(), Permission.getRaw(perms), 0L);
} else if (item.isOfClass(User.class)) {
manager = manager.putPermissionOverride(g.getMember((User) item.get()), Permission.getRaw(perms), 0L);
}
}
}
manager.reason("Applying initial access lists").queue();
}
use of st.photonbur.Discord.Bot.lightbotv3.entity.bannable.BannableEntity in project TheLighterBot by PhotonBursted.
the class AccesslistModificationCommand method execute.
@Override
protected void execute() {
// Check if the input actually had enough arguments
if (input.size() < 1) {
handleError(String.format("You didn't supply the ID of the entity to %s!\nPlease use `+%s <idTo%s>`.", getActionDescription("%s", "remove from the %s"), primaryCommandName, StringUtils.capitalize(targetAccesslist)));
return;
}
// Get the input after the arguments as one string representation
String target = Utils.drainQueueToString(input);
String[] targetParts = target.split(":", 2);
String targetType = targetParts[0];
String targetName = targetParts.length > 1 ? targetParts[1] : null;
// Identify what the input was targeting
switch(targetType) {
case "user":
{
// Retrieve a list of users which could be targeted by the search
List<Member> candidates = ev.getGuild().getMembersByEffectiveName(targetName, true);
// See if there were any search results
if (candidates.size() <= 0) {
handleError(String.format("No user was found in this server having name **%s**!", targetName));
return;
}
// Otherwise, generate a selector and let the user decide what the target was
if (candidates.size() == 1) {
performAccesslistModification(new BannableUser(candidates.get(0).getUser()));
} else {
LinkedHashMap<String, BannableEntity> candidateMap = new LinkedHashMap<>();
candidates.forEach(c -> candidateMap.put(Utils.userAsString(c.getUser()), new BannableUser(c.getUser())));
new SelectorBuilder<>(this).setOptionMap(candidateMap).build();
}
break;
}
case "role":
{
// Retrieve a list of roles which could be targeted by the search
List<Role> candidates = ev.getGuild().getRolesByName(targetName, true);
// See if there were any search results
if (candidates.size() <= 0) {
handleError(String.format("No role was found in this server having name **%s**!", targetName));
return;
}
// Otherwise, generate a selector and let the user decide what the target was
if (candidates.size() == 1) {
performAccesslistModification(new BannableRole(candidates.get(0)));
} else {
LinkedHashMap<String, BannableEntity> candidateMap = new LinkedHashMap<>();
candidates.forEach(c -> candidateMap.put(c.getName(), new BannableRole(c)));
new SelectorBuilder<>(this).setOptionMap(candidateMap).build();
}
break;
}
default:
BannableEntity targetEntity = null;
// Test if the id was targeting a role or user. If not, throw an error, otherwise whitelist the target
if (ev.getGuild().getRoles().stream().anyMatch(role -> role.getId().equals(target))) {
targetEntity = new BannableRole(target);
}
if (ev.getGuild().getMembers().stream().anyMatch(member -> member.getUser().getId().equals(target))) {
targetEntity = new BannableUser(target);
}
if (targetEntity == null) {
handleError(String.format("The ID you supplied (`%s`) was neither a role or user in this server!", target));
return;
}
// Detect if the id specified is already blacklisted
if (performActionCheck(ev.getGuild(), targetEntity)) {
handleError(String.format("The entity you tried to %s is already %sed for this server!", getActionDescription("%s", "remove from the %s"), targetAccesslist));
return;
}
performAccesslistModification(targetEntity);
break;
}
}
use of st.photonbur.Discord.Bot.lightbotv3.entity.bannable.BannableEntity in project TheLighterBot by PhotonBursted.
the class AccesslistModificationCommand method performAccesslistModification.
private void performAccesslistModification(BannableEntity target) {
if (performActionCheck(ev.getGuild(), target)) {
if (target.isOfClass(User.class)) {
User targetUser = (User) target.get();
if (targetUser.equals(l.getBot().getSelfUser())) {
handleError("Access list operations cannot be executed on the bot itself!");
return;
}
handleError(String.format("User **%s** is already %s for this server!", Utils.userAsString((User) target.get()), getActionDescription("%sed", "not present in the %s")));
return;
}
if (target.isOfClass(Role.class)) {
handleError(String.format("Role **%s** is already %s for this server!", ((Role) target.get()).getName(), getActionDescription("%sed", "not present in the %s")));
return;
}
handleError(String.format("This entity is already %s for this server!", getActionDescription("%sed", "not present in the %s")));
return;
}
String response = performAction(ev.getGuild(), target);
LoggerUtils.logAndDelete(log, response);
l.getDiscordController().sendMessage(ev, String.format("%s **%s** was successfully %s!", StringUtils.capitalize(target.get().getClass().getSimpleName().toLowerCase().replace("impl", "")), target.isOfClass(User.class) ? Utils.userAsString((User) target.get()) : target.isOfClass(Role.class) ? String.format("`%s`", ((Role) target.get()).getName()) : "", getActionDescription("%sed", "removed from the %s")), DiscordController.AUTOMATIC_REMOVAL_INTERVAL);
}
Aggregations