Search in sources :

Example 1 with EnumOptions

use of com.sx4.bot.annotations.argument.EnumOptions 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)

Aggregations

Command (com.jockie.bot.core.command.Command)1 AuthorPermissions (com.sx4.bot.annotations.command.AuthorPermissions)1 CommandId (com.sx4.bot.annotations.command.CommandId)1 Examples (com.sx4.bot.annotations.command.Examples)1 Sx4Command (com.sx4.bot.core.Sx4Command)1 ModAction (com.sx4.bot.entities.mod.action.ModAction)1 Duration (java.time.Duration)1 Document (org.bson.Document)1 Bson (org.bson.conversions.Bson)1