Search in sources :

Example 56 with AuthorPermissions

use of com.sx4.bot.annotations.command.AuthorPermissions in project Sx4 by sx4-discord-bot.

the class LeaverCommand method channel.

@Command(value = "channel", description = "Sets the channel where leaver messages are sent to")
@CommandId(190)
@Examples({ "leaver channel", "leaver channel #leaves", "leaver channel reset" })
@AuthorPermissions(permissions = { Permission.MANAGE_SERVER })
public void channel(Sx4CommandEvent event, @Argument(value = "channel | reset", endless = true, nullDefault = true) @AlternativeOptions("reset") Alternative<BaseGuildMessageChannel> option) {
    MessageChannel messageChannel = event.getChannel();
    if (option == null && !(messageChannel instanceof BaseGuildMessageChannel)) {
        event.replyFailure("You cannot use this channel type").queue();
        return;
    }
    BaseGuildMessageChannel channel = option == null ? (BaseGuildMessageChannel) messageChannel : option.isAlternative() ? null : option.getValue();
    List<Bson> update = List.of(Operators.set("leaver.channelId", channel == null ? Operators.REMOVE : channel.getIdLong()), Operators.unset("leaver.webhook.id"), Operators.unset("leaver.webhook.token"));
    FindOneAndUpdateOptions options = new FindOneAndUpdateOptions().upsert(true).projection(Projections.include("leaver.webhook.token", "leaver.webhook.id", "leaver.channelId")).returnDocument(ReturnDocument.BEFORE);
    event.getMongo().findAndUpdateGuildById(event.getGuild().getIdLong(), update, options).whenComplete((data, exception) -> {
        if (ExceptionUtility.sendExceptionally(event, exception)) {
            return;
        }
        long channelId = data == null ? 0L : data.getEmbedded(List.of("leaver", "channelId"), 0L);
        event.getBot().getLeaverManager().removeWebhook(channelId);
        if ((channel == null ? 0L : channel.getIdLong()) == channelId) {
            event.replyFailure("The leaver channel is already " + (channel == null ? "unset" : "set to " + channel.getAsMention())).queue();
            return;
        }
        BaseGuildMessageChannel oldChannel = channelId == 0L ? null : event.getGuild().getChannelById(BaseGuildMessageChannel.class, channelId);
        long webhookId = data == null ? 0L : data.getEmbedded(List.of("leaver", "webhook", "id"), 0L);
        if (oldChannel != null && webhookId != 0L) {
            oldChannel.deleteWebhookById(Long.toString(webhookId)).queue(null, ErrorResponseException.ignore(ErrorResponse.UNKNOWN_WEBHOOK));
        }
        event.replySuccess("The leaver channel has been " + (channel == null ? "unset" : "set to " + channel.getAsMention())).queue();
    });
}
Also used : FindOneAndUpdateOptions(com.mongodb.client.model.FindOneAndUpdateOptions) Bson(org.bson.conversions.Bson) Command(com.jockie.bot.core.command.Command) Sx4Command(com.sx4.bot.core.Sx4Command)

Example 57 with AuthorPermissions

use of com.sx4.bot.annotations.command.AuthorPermissions in project Sx4 by sx4-discord-bot.

the class LoggerCommand method toggle.

@Command(value = "toggle", aliases = { "enable", "disable" }, description = "Toggles the state of a logger")
@CommandId(56)
@AuthorPermissions(permissions = { Permission.MANAGE_SERVER })
@Examples({ "logger toggle #logs", "logger toggle" })
public void toggle(Sx4CommandEvent event, @Argument(value = "channel", endless = true, nullDefault = true) BaseGuildMessageChannel channel) {
    MessageChannel messageChannel = event.getChannel();
    if (channel == null && !(messageChannel instanceof BaseGuildMessageChannel)) {
        event.replyFailure("You cannot use this channel type").queue();
        return;
    }
    BaseGuildMessageChannel effectiveChannel = channel == null ? (BaseGuildMessageChannel) messageChannel : channel;
    List<Bson> guildPipeline = List.of(Aggregates.project(Projections.fields(Projections.computed("premium", Operators.lt(Operators.nowEpochSecond(), Operators.ifNull("$premium.endAt", 0L))), Projections.computed("guildId", "$_id"))), Aggregates.match(Filters.eq("guildId", event.getGuild().getIdLong())));
    List<Bson> pipeline = List.of(Aggregates.match(Filters.and(Filters.eq("guildId", event.getGuild().getIdLong()), Filters.exists("enabled", false))), Aggregates.project(Projections.include("channelId")), Aggregates.group(null, Accumulators.push("loggers", Operators.ROOT)), Aggregates.unionWith("guilds", guildPipeline), Aggregates.group(null, Accumulators.max("loggers", "$loggers"), Accumulators.max("premium", "$premium")), Aggregates.project(Projections.fields(Projections.computed("premium", Operators.ifNull("$premium", false)), Projections.computed("count", Operators.size(Operators.ifNull("$loggers", Collections.EMPTY_LIST))), Projections.computed("disabled", Operators.isEmpty(Operators.filter(Operators.ifNull("$loggers", Collections.EMPTY_LIST), Operators.eq("$$this.channelId", effectiveChannel.getIdLong())))))));
    event.getMongo().aggregateLoggers(pipeline).thenCompose(documents -> {
        Document data = documents.isEmpty() ? null : documents.get(0);
        boolean disabled = data == null || data.getBoolean("disabled");
        int count = data == null ? 0 : data.getInteger("count");
        if (data != null && disabled && count >= 3 && !data.getBoolean("premium")) {
            throw new IllegalArgumentException("You need to have Sx4 premium to have more than 3 enabled loggers, you can get premium at <https://www.patreon.com/Sx4>");
        }
        if (count >= 25) {
            throw new IllegalArgumentException("You can not have any more than 25 enabled loggers");
        }
        List<Bson> update = List.of(Operators.set("enabled", Operators.cond(Operators.exists("$enabled"), Operators.REMOVE, false)));
        FindOneAndUpdateOptions options = new FindOneAndUpdateOptions().returnDocument(ReturnDocument.AFTER).projection(Projections.include("enabled"));
        return event.getMongo().findAndUpdateLogger(Filters.eq("channelId", effectiveChannel.getIdLong()), update, options);
    }).whenComplete((data, exception) -> {
        Throwable cause = exception instanceof CompletionException ? exception.getCause() : exception;
        if (cause instanceof IllegalArgumentException) {
            event.replyFailure(cause.getMessage()).queue();
            return;
        }
        if (ExceptionUtility.sendExceptionally(event, exception)) {
            return;
        }
        if (data == null) {
            event.replyFailure("There is not a logger in that channel").queue();
            return;
        }
        event.replySuccess("The logger in " + effectiveChannel.getAsMention() + " is now **" + (data.get("enabled", true) ? "enabled" : "disabled") + "**").queue();
    });
}
Also used : Document(org.bson.Document) MessageChannel(net.dv8tion.jda.api.entities.MessageChannel) java.util(java.util) Command(com.jockie.bot.core.command.Command) MongoWriteException(com.mongodb.MongoWriteException) Permission(net.dv8tion.jda.api.Permission) MongoDatabase(com.sx4.bot.database.mongo.MongoDatabase) CompletableFuture(java.util.concurrent.CompletableFuture) ErrorResponse(net.dv8tion.jda.api.requests.ErrorResponse) PagedResult(com.sx4.bot.paged.PagedResult) Bson(org.bson.conversions.Bson) Sx4CommandEvent(com.sx4.bot.core.Sx4CommandEvent) com.mongodb.client.model(com.mongodb.client.model) com.sx4.bot.annotations.command(com.sx4.bot.annotations.command) LoggerCategory(com.sx4.bot.entities.management.LoggerCategory) Argument(com.jockie.bot.core.argument.Argument) LoggerEvent(com.sx4.bot.entities.management.LoggerEvent) Operators(com.sx4.bot.database.mongo.model.Operators) ErrorResponseException(net.dv8tion.jda.api.exceptions.ErrorResponseException) BaseGuildMessageChannel(net.dv8tion.jda.api.entities.BaseGuildMessageChannel) Sx4Command(com.sx4.bot.core.Sx4Command) CompletionException(java.util.concurrent.CompletionException) LoggerUtility(com.sx4.bot.utility.LoggerUtility) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) ModuleCategory(com.sx4.bot.category.ModuleCategory) MessageBuilder(net.dv8tion.jda.api.MessageBuilder) ImageUrl(com.sx4.bot.annotations.argument.ImageUrl) ExceptionUtility(com.sx4.bot.utility.ExceptionUtility) ErrorCategory(com.mongodb.ErrorCategory) MessageChannel(net.dv8tion.jda.api.entities.MessageChannel) BaseGuildMessageChannel(net.dv8tion.jda.api.entities.BaseGuildMessageChannel) CompletionException(java.util.concurrent.CompletionException) BaseGuildMessageChannel(net.dv8tion.jda.api.entities.BaseGuildMessageChannel) Document(org.bson.Document) Bson(org.bson.conversions.Bson) Command(com.jockie.bot.core.command.Command) Sx4Command(com.sx4.bot.core.Sx4Command)

Example 58 with AuthorPermissions

use of com.sx4.bot.annotations.command.AuthorPermissions in project Sx4 by sx4-discord-bot.

the class LoggerCommand method add.

@Command(value = "add", description = "Adds a logger to a certain channel")
@CommandId(54)
@AuthorPermissions(permissions = { Permission.MANAGE_SERVER })
@Examples({ "logger add #logs", "logger add" })
public void add(Sx4CommandEvent event, @Argument(value = "channel", endless = true, nullDefault = true) BaseGuildMessageChannel channel) {
    MessageChannel messageChannel = event.getChannel();
    if (channel == null && !(messageChannel instanceof BaseGuildMessageChannel)) {
        event.replyFailure("You cannot use this channel type").queue();
        return;
    }
    BaseGuildMessageChannel effectiveChannel = channel == null ? (BaseGuildMessageChannel) messageChannel : channel;
    List<Bson> guildPipeline = List.of(Aggregates.project(Projections.fields(Projections.computed("premium", Operators.lt(Operators.nowEpochSecond(), Operators.ifNull("$premium.endAt", 0L))), Projections.computed("guildId", "$_id"))), Aggregates.match(Filters.eq("guildId", event.getGuild().getIdLong())));
    List<Bson> pipeline = List.of(Aggregates.match(Filters.and(Filters.eq("guildId", event.getGuild().getIdLong()), Filters.exists("enabled", false))), Aggregates.limit(25), Aggregates.group(null, Accumulators.sum("count", 1)), Aggregates.unionWith("guilds", guildPipeline), Aggregates.group(null, Accumulators.max("count", "$count"), Accumulators.max("premium", "$premium")), Aggregates.project(Projections.fields(Projections.computed("premium", Operators.ifNull("$premium", false)), Projections.computed("count", Operators.ifNull("$count", 0)))));
    event.getMongo().aggregateLoggers(pipeline).thenCompose(documents -> {
        Document counter = documents.isEmpty() ? null : documents.get(0);
        int count = counter == null ? 0 : counter.getInteger("count");
        if (counter != null && count >= 3 && !counter.getBoolean("premium")) {
            event.replyFailure("You need to have Sx4 premium to have more than 3 enabled loggers, you can get premium at <https://www.patreon.com/Sx4>").queue();
            return CompletableFuture.completedFuture(null);
        }
        if (count == 25) {
            event.replyFailure("You can not have any more than 25 loggers").queue();
            return CompletableFuture.completedFuture(null);
        }
        Document data = new Document("channelId", effectiveChannel.getIdLong()).append("guildId", event.getGuild().getIdLong());
        return event.getMongo().insertLogger(data);
    }).whenComplete((result, exception) -> {
        Throwable cause = exception instanceof CompletionException ? exception.getCause() : exception;
        if (cause instanceof MongoWriteException && ((MongoWriteException) cause).getError().getCategory() == ErrorCategory.DUPLICATE_KEY) {
            event.replyFailure("You already have a logger setup in " + effectiveChannel.getAsMention()).queue();
            return;
        }
        if (ExceptionUtility.sendExceptionally(event, exception) || result == null) {
            return;
        }
        event.replySuccess("You now have a logger setup in " + effectiveChannel.getAsMention()).queue();
    });
}
Also used : Document(org.bson.Document) MessageChannel(net.dv8tion.jda.api.entities.MessageChannel) java.util(java.util) Command(com.jockie.bot.core.command.Command) MongoWriteException(com.mongodb.MongoWriteException) Permission(net.dv8tion.jda.api.Permission) MongoDatabase(com.sx4.bot.database.mongo.MongoDatabase) CompletableFuture(java.util.concurrent.CompletableFuture) ErrorResponse(net.dv8tion.jda.api.requests.ErrorResponse) PagedResult(com.sx4.bot.paged.PagedResult) Bson(org.bson.conversions.Bson) Sx4CommandEvent(com.sx4.bot.core.Sx4CommandEvent) com.mongodb.client.model(com.mongodb.client.model) com.sx4.bot.annotations.command(com.sx4.bot.annotations.command) LoggerCategory(com.sx4.bot.entities.management.LoggerCategory) Argument(com.jockie.bot.core.argument.Argument) LoggerEvent(com.sx4.bot.entities.management.LoggerEvent) Operators(com.sx4.bot.database.mongo.model.Operators) ErrorResponseException(net.dv8tion.jda.api.exceptions.ErrorResponseException) BaseGuildMessageChannel(net.dv8tion.jda.api.entities.BaseGuildMessageChannel) Sx4Command(com.sx4.bot.core.Sx4Command) CompletionException(java.util.concurrent.CompletionException) LoggerUtility(com.sx4.bot.utility.LoggerUtility) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) ModuleCategory(com.sx4.bot.category.ModuleCategory) MessageBuilder(net.dv8tion.jda.api.MessageBuilder) ImageUrl(com.sx4.bot.annotations.argument.ImageUrl) ExceptionUtility(com.sx4.bot.utility.ExceptionUtility) ErrorCategory(com.mongodb.ErrorCategory) MessageChannel(net.dv8tion.jda.api.entities.MessageChannel) BaseGuildMessageChannel(net.dv8tion.jda.api.entities.BaseGuildMessageChannel) MongoWriteException(com.mongodb.MongoWriteException) CompletionException(java.util.concurrent.CompletionException) BaseGuildMessageChannel(net.dv8tion.jda.api.entities.BaseGuildMessageChannel) Document(org.bson.Document) Bson(org.bson.conversions.Bson) Command(com.jockie.bot.core.command.Command) Sx4Command(com.sx4.bot.core.Sx4Command)

Example 59 with AuthorPermissions

use of com.sx4.bot.annotations.command.AuthorPermissions in project Sx4 by sx4-discord-bot.

the class LoggerCommand method avatar.

@Command(value = "avatar", description = "Set the avatar of the webhook that sends logs")
@CommandId(424)
@Examples({ "logger avatar #logs Shea#6653", "logger avatar https://i.imgur.com/i87lyNO.png" })
@Premium
@AuthorPermissions(permissions = { Permission.MANAGE_SERVER })
public void avatar(Sx4CommandEvent event, @Argument(value = "channel", nullDefault = true) BaseGuildMessageChannel channel, @Argument(value = "avatar", endless = true, acceptEmpty = true) @ImageUrl String url) {
    MessageChannel messageChannel = event.getChannel();
    if (channel == null && !(messageChannel instanceof BaseGuildMessageChannel)) {
        event.replyFailure("You cannot use this channel type").queue();
        return;
    }
    BaseGuildMessageChannel effectiveChannel = channel == null ? (BaseGuildMessageChannel) messageChannel : channel;
    event.getMongo().updateLogger(Filters.eq("channelId", effectiveChannel.getIdLong()), Updates.set("webhook.avatar", url)).whenComplete((result, exception) -> {
        if (ExceptionUtility.sendExceptionally(event, exception)) {
            return;
        }
        if (result.getModifiedCount() == 0) {
            event.replyFailure("Your webhook avatar for that logger was already set to that").queue();
            return;
        }
        event.replySuccess("Your webhook avatar has been updated for that logger, this only works with premium <https://patreon.com/Sx4>").queue();
    });
}
Also used : MessageChannel(net.dv8tion.jda.api.entities.MessageChannel) BaseGuildMessageChannel(net.dv8tion.jda.api.entities.BaseGuildMessageChannel) BaseGuildMessageChannel(net.dv8tion.jda.api.entities.BaseGuildMessageChannel) Command(com.jockie.bot.core.command.Command) Sx4Command(com.sx4.bot.core.Sx4Command)

Example 60 with AuthorPermissions

use of com.sx4.bot.annotations.command.AuthorPermissions in project Sx4 by sx4-discord-bot.

the class MuteCommand method role.

@Command(value = "role", description = "Set the mute role to a custom one")
@CommandId(340)
@Examples({ "mute role @Muted", "mute role Muted", "mute role reset" })
@AuthorPermissions(permissions = { Permission.MANAGE_SERVER })
public void role(Sx4CommandEvent event, @Argument(value = "role | reset", endless = true) Alternative<Role> option) {
    Role role = option.getValue();
    Bson update = option.isAlternative() ? Updates.unset("mute.roleId") : Updates.set("mute.roleId", role.getIdLong());
    event.getMongo().updateGuildById(event.getGuild().getIdLong(), update).whenComplete((result, exception) -> {
        if (ExceptionUtility.sendExceptionally(event, exception)) {
            return;
        }
        if (result.getModifiedCount() == 0 && result.getUpsertedId() == null) {
            event.replyFailure("Your mute role was already " + (option.isAlternative() ? "unset" : "set to that")).queue();
            return;
        }
        event.replySuccess("Your mute role has been " + (option.isAlternative() ? "unset" : "set to " + role.getAsMention())).queue();
    });
}
Also used : Role(net.dv8tion.jda.api.entities.Role) Bson(org.bson.conversions.Bson) 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

Command (com.jockie.bot.core.command.Command)88 Sx4Command (com.sx4.bot.core.Sx4Command)88 Bson (org.bson.conversions.Bson)52 AuthorPermissions (com.sx4.bot.annotations.command.AuthorPermissions)48 CommandId (com.sx4.bot.annotations.command.CommandId)48 Examples (com.sx4.bot.annotations.command.Examples)48 Document (org.bson.Document)48 Argument (com.jockie.bot.core.argument.Argument)26 ModuleCategory (com.sx4.bot.category.ModuleCategory)26 Sx4CommandEvent (com.sx4.bot.core.Sx4CommandEvent)26 Permission (net.dv8tion.jda.api.Permission)26 TextChannel (net.dv8tion.jda.api.entities.TextChannel)26 PagedResult (com.sx4.bot.paged.PagedResult)24 BaseGuildMessageChannel (net.dv8tion.jda.api.entities.BaseGuildMessageChannel)24 Operators (com.sx4.bot.database.mongo.model.Operators)23 ExceptionUtility (com.sx4.bot.utility.ExceptionUtility)22 CompletionException (java.util.concurrent.CompletionException)22 MessageChannel (net.dv8tion.jda.api.entities.MessageChannel)20 MongoWriteException (com.mongodb.MongoWriteException)19 com.mongodb.client.model (com.mongodb.client.model)19