use of net.dv8tion.jda.api.entities.IPermissionHolder in project Sx4 by sx4-discord-bot.
the class PermissionsCommand method onCommand.
public void onCommand(Sx4CommandEvent event, @Argument(value = "role | user", endless = true, nullDefault = true) IPermissionHolder holder) {
IPermissionHolder effectiveHolder = holder == null ? event.getMember() : holder;
boolean isRole = effectiveHolder instanceof Role;
Member member = isRole ? null : (Member) effectiveHolder;
User user = isRole ? null : member.getUser();
Role role = isRole ? (Role) holder : null;
EnumSet<Permission> permissions = effectiveHolder.getPermissions();
StringJoiner joiner = new StringJoiner("\n");
for (Permission permission : permissions) {
joiner.add(permission.getName());
}
EmbedBuilder embed = new EmbedBuilder().setAuthor(isRole ? role.getName() : user.getAsTag(), null, isRole ? event.getGuild().getIconUrl() : user.getEffectiveAvatarUrl()).setDescription(joiner.toString()).setColor(isRole ? role.getColorRaw() : member.getColorRaw());
event.reply(embed.build()).queue();
}
use of net.dv8tion.jda.api.entities.IPermissionHolder in project Sx4 by sx4-discord-bot.
the class BlacklistCommand method remove.
@Command(value = "remove", description = "Remove a role/user from being blacklisted from a specified command/module in a channel")
@CommandId(180)
@Examples({ "blacklist remove #general @Shea#6653 fish", "blacklist remove #bots @Members ban" })
@AuthorPermissions(permissions = { Permission.MANAGE_SERVER })
public void remove(Sx4CommandEvent event, @Argument(value = "channel", nullDefault = true) TextChannel channel, @Argument(value = "user | role") IPermissionHolder holder, @Argument(value = "command | module", endless = true) List<Sx4Command> commands) {
List<TextChannel> channels = channel == null ? event.getGuild().getTextChannels() : List.of(channel);
boolean role = holder instanceof Role;
BitSet bitSet = new BitSet();
commands.stream().map(Sx4Command::getId).forEach(bitSet::set);
List<Long> longArray = Arrays.stream(bitSet.toLongArray()).boxed().collect(Collectors.toList());
List<Bson> update = List.of(Operators.set("holders", Operators.let(new Document("holder", Operators.filter("$holders", Operators.eq("$$this.id", holder.getIdLong()))), Operators.cond(Operators.or(Operators.extinct("$holders"), Operators.isEmpty("$$holder")), "$holders", Operators.concatArrays(Operators.filter("$holders", Operators.ne("$$this.id", holder.getIdLong())), Operators.let(new Document("result", Operators.bitSetAndNot(Operators.ifNull(Operators.first(Operators.map("$$holder", "$$this.blacklisted")), Collections.EMPTY_LIST), longArray)), Operators.cond(Operators.and(Operators.isEmpty(Operators.ifNull(Operators.first(Operators.map("$$holder", "$$this.whitelisted")), Collections.EMPTY_LIST)), Operators.bitSetIsEmpty("$$result")), Collections.EMPTY_LIST, List.of(Operators.cond(Operators.bitSetIsEmpty("$$result"), Operators.removeObject(Operators.first("$$holder"), "blacklisted"), Operators.mergeObjects(Operators.first("$$holder"), new Document("blacklisted", "$$result")))))))))));
List<WriteModel<Document>> bulkData = channels.stream().map(textChannel -> new UpdateOneModel<Document>(Filters.eq("channelId", textChannel.getIdLong()), update, new UpdateOptions())).collect(Collectors.toList());
event.getMongo().bulkWriteBlacklists(bulkData).whenComplete((result, exception) -> {
if (ExceptionUtility.sendExceptionally(event, exception)) {
return;
}
if (result.getModifiedCount() == 0) {
event.replyFailure((commands.size() == 1 ? "That command is" : "Those commands are") + " not blacklisted for that " + (role ? "role" : "user") + " in those channels").queue();
return;
}
event.replySuccess((commands.size() == 1 ? "That command is" : "Those commands are") + " no longer blacklisted for that " + (role ? "role" : "user") + " in **" + result.getModifiedCount() + "** channel" + (result.getModifiedCount() == 1 ? "" : "s")).queue();
});
}
use of net.dv8tion.jda.api.entities.IPermissionHolder in project Sx4 by sx4-discord-bot.
the class BlacklistCommand method add.
@Command(value = "add", description = "Add a role/user to be blacklisted from a specified command/module in a channel")
@CommandId(168)
@Examples({ "blacklist add #general @Shea#6653 fish", "blacklist add #bots @Members ban" })
@AuthorPermissions(permissions = { Permission.MANAGE_SERVER })
public void add(Sx4CommandEvent event, @Argument(value = "channel", nullDefault = true) TextChannel channel, @Argument(value = "user | role") IPermissionHolder holder, @Argument(value = "command | module", endless = true) List<Sx4Command> commands) {
List<TextChannel> channels = channel == null ? event.getGuild().getTextChannels() : List.of(channel);
boolean role = holder instanceof Role;
int type = role ? HolderType.ROLE.getType() : HolderType.USER.getType();
BitSet bitSet = new BitSet();
commands.stream().map(Sx4Command::getId).forEach(bitSet::set);
Document defaultData = new Document("id", holder.getIdLong()).append("type", type).append("blacklisted", Collections.EMPTY_LIST);
List<Long> longArray = Arrays.stream(bitSet.toLongArray()).boxed().collect(Collectors.toList());
List<Bson> update = List.of(Operators.set("holders", Operators.let(new Document("holders", Operators.ifNull("$holders", Collections.EMPTY_LIST)), Operators.let(new Document("holder", Operators.filter("$$holders", Operators.eq("$$this.id", holder.getIdLong()))), Operators.concatArrays(Operators.ifNull(Operators.filter("$$holders", Operators.ne("$$this.id", holder.getIdLong())), Collections.EMPTY_LIST), List.of(Operators.mergeObjects(Operators.ifNull(Operators.first("$$holder"), defaultData), new Document("blacklisted", Operators.bitSetOr(longArray, Operators.ifNull(Operators.first(Operators.map("$$holder", "$$this.blacklisted")), Collections.EMPTY_LIST))))))))), Operators.setOnInsert("guildId", event.getGuild().getIdLong()));
List<WriteModel<Document>> bulkData = channels.stream().map(textChannel -> new UpdateOneModel<Document>(Filters.eq("channelId", textChannel.getIdLong()), update, new UpdateOptions().upsert(true))).collect(Collectors.toList());
event.getMongo().bulkWriteBlacklists(bulkData).whenComplete((result, exception) -> {
if (ExceptionUtility.sendExceptionally(event, exception)) {
return;
}
if (result.getModifiedCount() == 0) {
event.replyFailure((commands.size() == 1 ? "That command is" : "Those commands are") + " already blacklisted for that " + (role ? "role" : "user") + " in those channels").queue();
return;
}
event.replySuccess((commands.size() == 1 ? "That command is" : "Those commands are") + " now blacklisted for that " + (role ? "role" : "user") + " in **" + result.getModifiedCount() + "** extra channel" + (result.getModifiedCount() == 1 ? "" : "s")).queue();
});
}
use of net.dv8tion.jda.api.entities.IPermissionHolder in project Sx4 by sx4-discord-bot.
the class FakePermissionsCommand method delete.
@Command(value = "delete", aliases = { "remove" }, description = "Deletes fake permissions for a user or role")
@CommandId(172)
@Examples({ "fake permissions delete @Shea#6653", "fake permissions delete @Mods", "fake permissions delete all" })
@AuthorPermissions(permissions = { Permission.ADMINISTRATOR })
public void delete(Sx4CommandEvent event, @Argument(value = "user | role | all", endless = true, nullDefault = true) @AlternativeOptions("all") Alternative<IPermissionHolder> option) {
if (option == null) {
List<Document> fakePermissions = event.getProperty("fakePermissions");
if (fakePermissions.isEmpty()) {
event.replyFailure("Nothing has fake permissions in this server").queue();
return;
}
PagedResult<Document> paged = new PagedResult<>(event.getBot(), fakePermissions).setAuthor("User & Roles", null, event.getGuild().getIconUrl()).setTimeout(60).setDisplayFunction(data -> {
int type = data.getInteger("type");
long id = data.getLong("id");
if (type == HolderType.USER.getType()) {
User user = event.getShardManager().getUserById(id);
return user == null ? "Anonymous#0000 (" + id + ")" : user.getAsTag();
} else {
Role role = event.getGuild().getRoleById(id);
return role == null ? "Deleted Role (" + id + ")" : role.getAsMention();
}
});
paged.onTimeout(() -> event.reply("Timed out :stopwatch:").queue());
paged.onSelect(select -> {
Document data = select.getSelected();
boolean isRole = data.getInteger("type") == HolderType.ROLE.getType();
long id = data.getLong("id");
User user = isRole ? null : event.getShardManager().getUserById(id);
Role role = isRole ? event.getGuild().getRoleById(id) : null;
event.getMongo().updateGuildById(event.getGuild().getIdLong(), Updates.pull("fakePermissions.holders", Filters.eq("id", id))).whenComplete((result, exception) -> {
if (ExceptionUtility.sendExceptionally(event, exception)) {
return;
}
if (result.getModifiedCount() == 0) {
event.replyFailure("That " + (isRole ? "role" : "user") + " doesn't have any fake permissions").queue();
return;
}
event.replySuccess((isRole ? (role == null ? "Deleted Role (" + id + ")" : role.getAsMention()) : "**" + (user == null ? "Anonymous#0000** (" + id + ")" : user.getAsTag() + "**")) + " no longer has any fake permissions").queue();
});
});
paged.execute(event);
} else if (option.isAlternative()) {
List<Button> buttons = List.of(Button.success("yes", "Yes"), Button.danger("no", "No"));
event.reply(event.getAuthor().getName() + ", are you sure you want to delete **all** fake permissions data?").setActionRow(buttons).submit().thenCompose(message -> {
return new Waiter<>(event.getBot(), ButtonClickEvent.class).setPredicate(e -> ButtonUtility.handleButtonConfirmation(e, message, event.getAuthor())).setCancelPredicate(e -> ButtonUtility.handleButtonCancellation(e, message, event.getAuthor())).onFailure(e -> ButtonUtility.handleButtonFailure(e, message)).setTimeout(60).start();
}).whenComplete((e, exception) -> {
Throwable cause = exception instanceof CompletionException ? exception.getCause() : exception;
if (cause instanceof CancelException) {
GenericEvent cancelEvent = ((CancelException) cause).getEvent();
if (cancelEvent != null) {
((ButtonClickEvent) cancelEvent).reply("Cancelled " + event.getConfig().getSuccessEmote()).queue();
}
return;
} else if (cause instanceof TimeoutException) {
event.reply("Timed out :stopwatch:").queue();
return;
} else if (ExceptionUtility.sendExceptionally(event, exception)) {
return;
}
event.getMongo().updateGuildById(event.getGuild().getIdLong(), Updates.unset("fakePermissions.holders")).whenComplete((result, databaseException) -> {
if (ExceptionUtility.sendExceptionally(event, databaseException)) {
return;
}
e.reply("All fake permission data has been deleted in this server " + event.getConfig().getSuccessEmote()).queue();
});
});
} else {
IPermissionHolder holder = option.getValue();
boolean role = holder instanceof Role;
event.getMongo().updateGuildById(event.getGuild().getIdLong(), Updates.pull("fakePermissions.holders", Filters.eq("id", holder.getIdLong()))).whenComplete((result, exception) -> {
if (ExceptionUtility.sendExceptionally(event, exception)) {
return;
}
if (result.getModifiedCount() == 0) {
event.replyFailure("That " + (role ? "role" : "user") + " doesn't have any fake permissions").queue();
return;
}
event.replySuccess((role ? ((Role) holder).getAsMention() : "**" + ((Member) holder).getUser().getAsTag() + "**") + " no longer has any fake permissions").queue();
});
}
}
use of net.dv8tion.jda.api.entities.IPermissionHolder in project Sx4 by sx4-discord-bot.
the class FakePermissionsCommand method stats.
@Command(value = "stats", description = "Lists the permissions a role or user has")
@CommandId(173)
@Examples({ "fake permissions stats @Shea#6653", "fake permissions stats @Mods" })
public void stats(Sx4CommandEvent event, @Argument(value = "user | role", endless = true) IPermissionHolder holder) {
boolean role = holder instanceof Role;
List<Document> holders = event.getMongo().getGuildById(event.getGuild().getIdLong(), Projections.include("fakePermissions.holders")).getEmbedded(List.of("fakePermissions", "holders"), Collections.emptyList());
long permissionsRaw = holders.stream().filter(data -> data.getLong("id") == holder.getIdLong()).map(data -> data.getLong("permissions")).findFirst().orElse(0L);
if (permissionsRaw == 0L) {
event.replyFailure("That " + (role ? "role" : "user") + " doesn't have any fake permissions").queue();
return;
}
EmbedBuilder embed = new EmbedBuilder().setDescription(Permission.getPermissions(permissionsRaw).stream().map(Permission::getName).collect(Collectors.joining("\n"))).setAuthor((role ? ((Role) holder).getName() : ((Member) holder).getEffectiveName()) + "'s Fake Permissions", null, role ? event.getGuild().getIconUrl() : ((Member) holder).getUser().getEffectiveAvatarUrl());
event.reply(embed.build()).queue();
}
Aggregations