Search in sources :

Example 21 with Alternative

use of com.sx4.bot.entities.argument.Alternative in project Sx4 by sx4-discord-bot.

the class StarboardCommand method channel.

@Command(value = "channel", description = "Sets the channel for starboard messages to be sent in")
@CommandId(198)
@Examples({ "starboard channel", "starboard channel #starboard", "starboard channel reset" })
@AuthorPermissions(permissions = { Permission.MANAGE_SERVER })
public void channel(Sx4CommandEvent event, @Argument(value = "channel | reset", endless = true, nullDefault = true) @AlternativeOptions("reset") Alternative<TextChannel> option) {
    TextChannel channel = option == null ? event.getTextChannel() : option.getValue();
    List<Bson> update = List.of(Operators.set("starboard.channelId", channel == null ? Operators.REMOVE : channel.getIdLong()), Operators.unset("starboard.webhook.id"), Operators.unset("starboard.webhook.token"));
    FindOneAndUpdateOptions options = new FindOneAndUpdateOptions().returnDocument(ReturnDocument.BEFORE).projection(Projections.include("starboard.webhook.id", "starboard.channelId")).upsert(true);
    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("starboard", "channelId"), 0L);
        event.getBot().getStarboardManager().removeWebhook(channelId);
        if ((channel == null ? 0L : channel.getIdLong()) == channelId) {
            event.replyFailure("The starboard channel is already " + (channel == null ? "unset" : "set to " + channel.getAsMention())).queue();
            return;
        }
        TextChannel oldChannel = channelId == 0L ? null : event.getGuild().getTextChannelById(channelId);
        long webhookId = data == null ? 0L : data.getEmbedded(List.of("starboard", "webhook", "id"), 0L);
        if (oldChannel != null && webhookId != 0L) {
            oldChannel.deleteWebhookById(Long.toString(webhookId)).queue(null, ErrorResponseException.ignore(ErrorResponse.UNKNOWN_WEBHOOK));
        }
        event.replySuccess("The starboard channel has been " + (channel == null ? "unset" : "set to " + channel.getAsMention())).queue();
    });
}
Also used : TextChannel(net.dv8tion.jda.api.entities.TextChannel) Bson(org.bson.conversions.Bson) Command(com.jockie.bot.core.command.Command) Sx4Command(com.sx4.bot.core.Sx4Command)

Example 22 with Alternative

use of com.sx4.bot.entities.argument.Alternative in project Sx4 by sx4-discord-bot.

the class TemplateCommand method delete.

@Command(value = "delete", aliases = { "remove" }, description = "Deletes a template from the current server")
@CommandId(256)
@Examples({ "template delete 6006ff6b94c9ed0f764ada83", "template delete all" })
@AuthorPermissions(permissions = { Permission.MANAGE_SERVER })
public void delete(Sx4CommandEvent event, @Argument(value = "id | all") @AlternativeOptions("all") Alternative<ObjectId> option) {
    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** the templates in this server?").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().deleteManyTemplates(Filters.eq("guildId", event.getGuild().getIdLong())).whenComplete((result, databaseException) -> {
                if (ExceptionUtility.sendExceptionally(event, databaseException)) {
                    return;
                }
                if (result.getDeletedCount() == 0) {
                    e.reply("There are no templates in this server " + event.getConfig().getFailureEmote()).queue();
                    return;
                }
                e.reply("All templates have been deleted in this server " + event.getConfig().getSuccessEmote()).queue();
            });
        });
    } else {
        event.getMongo().deleteTemplateById(option.getValue()).whenComplete((result, exception) -> {
            if (ExceptionUtility.sendExceptionally(event, exception)) {
                return;
            }
            if (result.getDeletedCount() == 0) {
                event.replyFailure("I could not find that template").queue();
                return;
            }
            event.replySuccess("That template has been deleted").queue();
        });
    }
}
Also used : Document(org.bson.Document) CancelException(com.sx4.bot.waiter.exception.CancelException) Command(com.jockie.bot.core.command.Command) ButtonClickEvent(net.dv8tion.jda.api.events.interaction.ButtonClickEvent) MongoWriteException(com.mongodb.MongoWriteException) Permission(net.dv8tion.jda.api.Permission) Projections(com.mongodb.client.model.Projections) CommandId(com.sx4.bot.annotations.command.CommandId) PagedResult(com.sx4.bot.paged.PagedResult) ArrayList(java.util.ArrayList) Filters(com.mongodb.client.model.Filters) Alternative(com.sx4.bot.entities.argument.Alternative) ButtonUtility(com.sx4.bot.utility.ButtonUtility) Sx4CommandEvent(com.sx4.bot.core.Sx4CommandEvent) Button(net.dv8tion.jda.api.interactions.components.Button) AlternativeOptions(com.sx4.bot.annotations.argument.AlternativeOptions) Waiter(com.sx4.bot.waiter.Waiter) GenericEvent(net.dv8tion.jda.api.events.GenericEvent) BotPermissions(com.sx4.bot.annotations.command.BotPermissions) Argument(com.jockie.bot.core.argument.Argument) Limit(com.sx4.bot.annotations.argument.Limit) AuthorPermissions(com.sx4.bot.annotations.command.AuthorPermissions) Sx4Command(com.sx4.bot.core.Sx4Command) CompletionException(java.util.concurrent.CompletionException) TimeoutException(com.sx4.bot.waiter.exception.TimeoutException) ModuleCategory(com.sx4.bot.category.ModuleCategory) List(java.util.List) Examples(com.sx4.bot.annotations.command.Examples) ObjectId(org.bson.types.ObjectId) ExceptionUtility(com.sx4.bot.utility.ExceptionUtility) ErrorCategory(com.mongodb.ErrorCategory) Button(net.dv8tion.jda.api.interactions.components.Button) GenericEvent(net.dv8tion.jda.api.events.GenericEvent) CompletionException(java.util.concurrent.CompletionException) CancelException(com.sx4.bot.waiter.exception.CancelException) Waiter(com.sx4.bot.waiter.Waiter) TimeoutException(com.sx4.bot.waiter.exception.TimeoutException) 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 23 with Alternative

use of com.sx4.bot.entities.argument.Alternative in project Sx4 by sx4-discord-bot.

the class MuteCommand method leaveAction.

@Command(value = "leave action", aliases = { "leaveaction" }, description = "Set an action to occur when a user leaves and rejoins while muted")
@CommandId(451)
@Examples({ "mute leave action BAN", "mute leave action MUTE_EXTEND 24h", "mute leave action reset" })
@AuthorPermissions(permissions = { Permission.MANAGE_SERVER })
public void leaveAction(Sx4CommandEvent event, @Argument(value = "action | reset", endless = true) @AlternativeOptions({ "reset" }) @EnumOptions(value = { "KICK", "UNBAN", "UNMUTE" }, exclude = true) Alternative<TimedArgument<ModAction>> option) {
    Bson update;
    if (option.isAlternative()) {
        update = Updates.unset("mute.leaveAction");
    } else {
        TimedArgument<ModAction> timedAction = option.getValue();
        ModAction action = timedAction.getArgument();
        Document modAction = new Document("type", action.getType());
        if (action.isTimed()) {
            Duration duration = timedAction.getDuration();
            if (duration == null) {
                event.replyFailure("You need to provide a duration for this mod action").queue();
                return;
            }
            modAction.append("duration", duration.toSeconds());
        }
        update = Updates.set("mute.leaveAction", modAction);
    }
    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 leave action was already " + (option.isAlternative() ? "unset" : "set to that")).queue();
            return;
        }
        event.replySuccess("Your leave action has been " + (option.isAlternative() ? "unset" : "updated")).queue();
    });
}
Also used : ModAction(com.sx4.bot.entities.mod.action.ModAction) Duration(java.time.Duration) Document(org.bson.Document) 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)

Example 24 with Alternative

use of com.sx4.bot.entities.argument.Alternative 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)24 Sx4Command (com.sx4.bot.core.Sx4Command)24 Bson (org.bson.conversions.Bson)21 CommandId (com.sx4.bot.annotations.command.CommandId)16 Examples (com.sx4.bot.annotations.command.Examples)16 Document (org.bson.Document)15 Argument (com.jockie.bot.core.argument.Argument)14 AlternativeOptions (com.sx4.bot.annotations.argument.AlternativeOptions)14 ModuleCategory (com.sx4.bot.category.ModuleCategory)14 Sx4CommandEvent (com.sx4.bot.core.Sx4CommandEvent)14 Alternative (com.sx4.bot.entities.argument.Alternative)14 PagedResult (com.sx4.bot.paged.PagedResult)13 Permission (net.dv8tion.jda.api.Permission)13 Waiter (com.sx4.bot.waiter.Waiter)12 CancelException (com.sx4.bot.waiter.exception.CancelException)12 TimeoutException (com.sx4.bot.waiter.exception.TimeoutException)12 List (java.util.List)12 CompletionException (java.util.concurrent.CompletionException)12 GenericEvent (net.dv8tion.jda.api.events.GenericEvent)12 ButtonClickEvent (net.dv8tion.jda.api.events.interaction.ButtonClickEvent)12