Search in sources :

Example 6 with IPermissionHolder

use of net.dv8tion.jda.api.entities.IPermissionHolder in project Sx4 by sx4-discord-bot.

the class WhitelistCommand method remove.

@Command(value = "remove", description = "Remove a role/user from being whitelisted from a specified command/module in a channel")
@CommandId(185)
@Examples({ "whitelist remove #general @Shea#6653 fish", "whitelist 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.whitelisted")), Collections.EMPTY_LIST), longArray)), Operators.cond(Operators.and(Operators.isEmpty(Operators.ifNull(Operators.first(Operators.map("$$holder", "$$this.blacklisted")), Collections.EMPTY_LIST)), Operators.bitSetIsEmpty("$$result")), Collections.EMPTY_LIST, List.of(Operators.cond(Operators.bitSetIsEmpty("$$result"), Operators.removeObject(Operators.first("$$holder"), "whitelisted"), Operators.mergeObjects(Operators.first("$$holder"), new Document("whitelisted", "$$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 whitelisted for that " + (role ? "role" : "user") + " in those channels").queue();
            return;
        }
        event.replySuccess((commands.size() == 1 ? "That command is" : "Those commands are") + " no longer whitelisted for that " + (role ? "role" : "user") + " in **" + result.getModifiedCount() + "** channel" + (result.getModifiedCount() == 1 ? "" : "s")).queue();
    });
}
Also used : HolderType(com.sx4.bot.entities.settings.HolderType) Document(org.bson.Document) AuthorPermissions(com.sx4.bot.annotations.command.AuthorPermissions) Operators(com.sx4.bot.database.mongo.model.Operators) java.util(java.util) Command(com.jockie.bot.core.command.Command) Sx4Command(com.sx4.bot.core.Sx4Command) Permission(net.dv8tion.jda.api.Permission) CommandId(com.sx4.bot.annotations.command.CommandId) TextChannel(net.dv8tion.jda.api.entities.TextChannel) PagedResult(com.sx4.bot.paged.PagedResult) Collectors(java.util.stream.Collectors) User(net.dv8tion.jda.api.entities.User) Bson(org.bson.conversions.Bson) ModuleCategory(com.sx4.bot.category.ModuleCategory) Alternative(com.sx4.bot.entities.argument.Alternative) Examples(com.sx4.bot.annotations.command.Examples) IPermissionHolder(net.dv8tion.jda.api.entities.IPermissionHolder) Role(net.dv8tion.jda.api.entities.Role) Sx4CommandEvent(com.sx4.bot.core.Sx4CommandEvent) AlternativeOptions(com.sx4.bot.annotations.argument.AlternativeOptions) ExceptionUtility(com.sx4.bot.utility.ExceptionUtility) com.mongodb.client.model(com.mongodb.client.model) Argument(com.jockie.bot.core.argument.Argument) Document(org.bson.Document) Bson(org.bson.conversions.Bson) Role(net.dv8tion.jda.api.entities.Role) TextChannel(net.dv8tion.jda.api.entities.TextChannel) AuthorPermissions(com.sx4.bot.annotations.command.AuthorPermissions) Command(com.jockie.bot.core.command.Command) Sx4Command(com.sx4.bot.core.Sx4Command) CommandId(com.sx4.bot.annotations.command.CommandId) Examples(com.sx4.bot.annotations.command.Examples)

Example 7 with IPermissionHolder

use of net.dv8tion.jda.api.entities.IPermissionHolder in project Sx4 by sx4-discord-bot.

the class WhitelistCommand method add.

@Command(value = "add", description = "Add a role/user to be whitelisted from a specified command/module in a channel")
@CommandId(184)
@Examples({ "whitelist add #general @Shea#6653 fish", "whitelist 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("whitelisted", 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("whitelisted", Operators.bitSetOr(longArray, Operators.ifNull(Operators.first(Operators.map("$$holder", "$$this.whitelisted")), 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 whitelisted for that " + (role ? "role" : "user") + " in those channels").queue();
            return;
        }
        event.replySuccess((commands.size() == 1 ? "That command is" : "Those commands are") + " now whitelisted for that " + (role ? "role" : "user") + " in **" + result.getModifiedCount() + "** extra channel" + (result.getModifiedCount() == 1 ? "" : "s")).queue();
    });
}
Also used : HolderType(com.sx4.bot.entities.settings.HolderType) Document(org.bson.Document) AuthorPermissions(com.sx4.bot.annotations.command.AuthorPermissions) Operators(com.sx4.bot.database.mongo.model.Operators) java.util(java.util) Command(com.jockie.bot.core.command.Command) Sx4Command(com.sx4.bot.core.Sx4Command) Permission(net.dv8tion.jda.api.Permission) CommandId(com.sx4.bot.annotations.command.CommandId) TextChannel(net.dv8tion.jda.api.entities.TextChannel) PagedResult(com.sx4.bot.paged.PagedResult) Collectors(java.util.stream.Collectors) User(net.dv8tion.jda.api.entities.User) Bson(org.bson.conversions.Bson) ModuleCategory(com.sx4.bot.category.ModuleCategory) Alternative(com.sx4.bot.entities.argument.Alternative) Examples(com.sx4.bot.annotations.command.Examples) IPermissionHolder(net.dv8tion.jda.api.entities.IPermissionHolder) Role(net.dv8tion.jda.api.entities.Role) Sx4CommandEvent(com.sx4.bot.core.Sx4CommandEvent) AlternativeOptions(com.sx4.bot.annotations.argument.AlternativeOptions) ExceptionUtility(com.sx4.bot.utility.ExceptionUtility) com.mongodb.client.model(com.mongodb.client.model) Argument(com.jockie.bot.core.argument.Argument) Document(org.bson.Document) Bson(org.bson.conversions.Bson) Role(net.dv8tion.jda.api.entities.Role) TextChannel(net.dv8tion.jda.api.entities.TextChannel) AuthorPermissions(com.sx4.bot.annotations.command.AuthorPermissions) Command(com.jockie.bot.core.command.Command) Sx4Command(com.sx4.bot.core.Sx4Command) CommandId(com.sx4.bot.annotations.command.CommandId) Examples(com.sx4.bot.annotations.command.Examples)

Aggregations

Permission (net.dv8tion.jda.api.Permission)7 IPermissionHolder (net.dv8tion.jda.api.entities.IPermissionHolder)7 Role (net.dv8tion.jda.api.entities.Role)7 User (net.dv8tion.jda.api.entities.User)7 Argument (com.jockie.bot.core.argument.Argument)6 Command (com.jockie.bot.core.command.Command)6 AlternativeOptions (com.sx4.bot.annotations.argument.AlternativeOptions)6 AuthorPermissions (com.sx4.bot.annotations.command.AuthorPermissions)6 CommandId (com.sx4.bot.annotations.command.CommandId)6 Examples (com.sx4.bot.annotations.command.Examples)6 ModuleCategory (com.sx4.bot.category.ModuleCategory)6 Sx4Command (com.sx4.bot.core.Sx4Command)6 Sx4CommandEvent (com.sx4.bot.core.Sx4CommandEvent)6 Operators (com.sx4.bot.database.mongo.model.Operators)6 Alternative (com.sx4.bot.entities.argument.Alternative)6 HolderType (com.sx4.bot.entities.settings.HolderType)6 PagedResult (com.sx4.bot.paged.PagedResult)6 ExceptionUtility (com.sx4.bot.utility.ExceptionUtility)6 Collectors (java.util.stream.Collectors)6 Document (org.bson.Document)6