Search in sources :

Example 16 with CommandId

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

the class GiveawayCommand method end.

@Command(value = "end", description = "Ends an active giveaway early")
@CommandId(50)
@Examples({ "giveaway end 727224132202397726" })
@AuthorPermissions(permissions = { Permission.MANAGE_SERVER })
public void end(Sx4CommandEvent event, @Argument(value = "message id") MessageArgument messageArgument) {
    Document data = event.getMongo().getGiveawayById(messageArgument.getMessageId());
    if (data == null) {
        event.replyFailure("There is no giveaway with that id").queue();
        return;
    }
    if (data.containsKey("winners")) {
        event.replyFailure("That giveaway has already ended").queue();
        return;
    }
    event.getBot().getGiveawayManager().endGiveaway(data, true);
}
Also used : Document(org.bson.Document) ReturnDocument(com.mongodb.client.model.ReturnDocument) 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 17 with CommandId

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

the class LeaverCommand method preview.

@Command(value = "preview", description = "Preview your leaver message")
@CommandId(195)
@Examples({ "leaver preview" })
public void preview(Sx4CommandEvent event) {
    Document data = event.getMongo().getGuildById(event.getGuild().getIdLong(), Projections.include("leaver.message", "leaver.enabled"));
    Document leaver = data.get("leaver", MongoDatabase.EMPTY_DOCUMENT);
    if (!leaver.get("enabled", false)) {
        event.replyFailure("Leaver is not enabled").queue();
        return;
    }
    WebhookMessageBuilder builder;
    try {
        builder = LeaverUtility.getLeaverMessage(leaver.get("message", LeaverManager.DEFAULT_MESSAGE), event.getMember());
    } catch (IllegalArgumentException e) {
        event.replyFailure(e.getMessage()).queue();
        return;
    }
    MessageUtility.fromWebhookMessage(event.getTextChannel(), builder.build()).queue();
}
Also used : WebhookMessageBuilder(club.minnced.discord.webhook.send.WebhookMessageBuilder) Document(org.bson.Document) ReturnDocument(com.mongodb.client.model.ReturnDocument) Command(com.jockie.bot.core.command.Command) Sx4Command(com.sx4.bot.core.Sx4Command)

Example 18 with CommandId

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

the class LeaverCommand method toggle.

@Command(value = "toggle", description = "Toggle the state of leaver")
@CommandId(189)
@Examples({ "leaver toggle" })
@AuthorPermissions(permissions = { Permission.MANAGE_SERVER })
public void toggle(Sx4CommandEvent event) {
    List<Bson> update = List.of(Operators.set("leaver.enabled", Operators.cond("$leaver.enabled", Operators.REMOVE, true)));
    FindOneAndUpdateOptions options = new FindOneAndUpdateOptions().returnDocument(ReturnDocument.AFTER).projection(Projections.include("leaver.enabled")).upsert(true);
    event.getMongo().findAndUpdateGuildById(event.getGuild().getIdLong(), update, options).whenComplete((data, exception) -> {
        if (ExceptionUtility.sendExceptionally(event, exception)) {
            return;
        }
        event.replySuccess("Leaver is now " + (data.getEmbedded(List.of("leaver", "enabled"), false) ? "enabled" : "disabled")).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 19 with CommandId

use of com.sx4.bot.annotations.command.CommandId 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) TextChannel channel) {
    TextChannel effectiveChannel = channel == null ? event.getTextChannel() : 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) 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) TextChannel(net.dv8tion.jda.api.entities.TextChannel) 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) 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) TextChannel(net.dv8tion.jda.api.entities.TextChannel) CompletionException(java.util.concurrent.CompletionException) Document(org.bson.Document) Bson(org.bson.conversions.Bson) Command(com.jockie.bot.core.command.Command) Sx4Command(com.sx4.bot.core.Sx4Command)

Example 20 with CommandId

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

the class MuteCommand method defaultTime.

@Command(value = "default time", aliases = { "default duration" }, description = "Sets the default time to be used when a duration argument isn't given")
@CommandId(342)
@Examples({ "mute default time 10m", "mute default time 5d", "mute default time 1h 30m" })
@AuthorPermissions(permissions = { Permission.MANAGE_SERVER })
public void defaultTime(Sx4CommandEvent event, @Argument(value = "duration", endless = true) Duration duration) {
    long seconds = duration.toSeconds();
    Bson update = seconds == ModUtility.DEFAULT_MUTE_DURATION ? Updates.unset("mute.defaultTime") : Updates.set("mute.defaultTime", seconds);
    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 default time was already set to that").queue();
            return;
        }
        event.replySuccess("Your mute default time has been set to **" + TimeUtility.LONG_TIME_FORMATTER.parse(seconds) + "**").queue();
    });
}
Also used : 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)146 Sx4Command (com.sx4.bot.core.Sx4Command)146 CommandId (com.sx4.bot.annotations.command.CommandId)103 Examples (com.sx4.bot.annotations.command.Examples)101 Document (org.bson.Document)100 Bson (org.bson.conversions.Bson)87 PagedResult (com.sx4.bot.paged.PagedResult)69 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)69 Argument (com.jockie.bot.core.argument.Argument)50 ModuleCategory (com.sx4.bot.category.ModuleCategory)50 Sx4CommandEvent (com.sx4.bot.core.Sx4CommandEvent)50 User (net.dv8tion.jda.api.entities.User)50 AuthorPermissions (com.sx4.bot.annotations.command.AuthorPermissions)48 Permission (net.dv8tion.jda.api.Permission)48 BotPermissions (com.sx4.bot.annotations.command.BotPermissions)42 Operators (com.sx4.bot.database.mongo.model.Operators)40 ExceptionUtility (com.sx4.bot.utility.ExceptionUtility)40 CompletionException (java.util.concurrent.CompletionException)38 TextChannel (net.dv8tion.jda.api.entities.TextChannel)38 AlternativeOptions (com.sx4.bot.annotations.argument.AlternativeOptions)37