use of st.photonbur.Discord.Bot.lightbotv3.misc.menu.selector.Selector 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;
}
}
Aggregations