Search in sources :

Example 11 with Action

use of com.sx4.bot.entities.mod.action.Action in project Sx4 by sx4-discord-bot.

the class UserInfoCommand method onCommand.

public void onCommand(Sx4CommandEvent event, @Argument(value = "user", nullDefault = true) @UserId long userId) {
    RestAction<User> action = userId == 0L ? new CompletedRestAction<>(event.getJDA(), event.getAuthor()) : event.getJDA().retrieveUserById(userId);
    action.flatMap(user -> {
        EmbedBuilder embed = new EmbedBuilder().setDescription(event.getConfig().getUserFlagEmotes(user.getFlags())).setAuthor(user.getAsTag(), user.getEffectiveAvatarUrl(), user.getEffectiveAvatarUrl()).setThumbnail(user.getEffectiveAvatarUrl()).addField("Joined Discord", user.getTimeCreated().format(TimeUtility.DEFAULT_FORMATTER), true).addField("User ID", user.getId(), true).addField("Bot", user.isBot() ? "Yes" : "No", false);
        return event.reply(embed.build());
    }).onErrorFlatMap(exception -> {
        if (exception instanceof ErrorResponseException && ((ErrorResponseException) exception).getErrorResponse() == ErrorResponse.UNKNOWN_USER) {
            return event.replyFailure("I could not find that user");
        }
        return null;
    }).queue();
}
Also used : UserId(com.sx4.bot.annotations.argument.UserId) ErrorResponseException(net.dv8tion.jda.api.exceptions.ErrorResponseException) CompletedRestAction(net.dv8tion.jda.internal.requests.CompletedRestAction) Sx4Command(com.sx4.bot.core.Sx4Command) Permission(net.dv8tion.jda.api.Permission) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) ErrorResponse(net.dv8tion.jda.api.requests.ErrorResponse) User(net.dv8tion.jda.api.entities.User) ModuleCategory(com.sx4.bot.category.ModuleCategory) Sx4CommandEvent(com.sx4.bot.core.Sx4CommandEvent) TimeUtility(com.sx4.bot.utility.TimeUtility) RestAction(net.dv8tion.jda.api.requests.RestAction) Argument(com.jockie.bot.core.argument.Argument) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) User(net.dv8tion.jda.api.entities.User) ErrorResponseException(net.dv8tion.jda.api.exceptions.ErrorResponseException)

Example 12 with Action

use of com.sx4.bot.entities.mod.action.Action in project Sx4 by sx4-discord-bot.

the class SelfRoleCommand method onCommand.

public void onCommand(Sx4CommandEvent event, @Argument(value = "role", endless = true) Role role) {
    Document selfRole = event.getMongo().getSelfRole(Filters.eq("roleId", role.getIdLong()), Projections.include("_id"));
    if (selfRole == null) {
        event.replyFailure("That role is not a self role").queue();
        return;
    }
    boolean hasRole = event.getMember().getRoles().contains(role);
    RestAction<Void> action;
    if (hasRole) {
        action = event.getGuild().removeRoleFromMember(event.getMember(), role);
    } else {
        action = event.getGuild().addRoleToMember(event.getMember(), role);
    }
    action.flatMap($ -> event.replySuccess("You " + (hasRole ? "no longer" : "now") + " have " + role.getAsMention())).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) Role(net.dv8tion.jda.api.entities.Role) 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) RestAction(net.dv8tion.jda.api.requests.RestAction) Argument(com.jockie.bot.core.argument.Argument) 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) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) ModuleCategory(com.sx4.bot.category.ModuleCategory) List(java.util.List) Examples(com.sx4.bot.annotations.command.Examples) ExceptionUtility(com.sx4.bot.utility.ExceptionUtility) ErrorCategory(com.mongodb.ErrorCategory) Document(org.bson.Document)

Example 13 with Action

use of com.sx4.bot.entities.mod.action.Action in project Sx4 by sx4-discord-bot.

the class WarnCommand method onCommand.

public void onCommand(Sx4CommandEvent event, @Argument(value = "user") Member member, @Argument(value = "reason", endless = true, nullDefault = true) Reason reason) {
    if (member.getIdLong() == event.getSelfUser().getIdLong()) {
        event.replyFailure("You cannot warn me, that is illegal").queue();
        return;
    }
    if (!event.getMember().canInteract(member)) {
        event.replyFailure("You cannot warn someone higher or equal than your top role").queue();
        return;
    }
    ModUtility.warn(event.getBot(), member, event.getMember(), reason).whenComplete((warning, exception) -> {
        Throwable cause = exception instanceof CompletionException ? exception.getCause() : exception;
        if (cause != null) {
            event.replyFailure(cause.getMessage()).queue();
            return;
        }
        Warn warn = warning.getWarning();
        Action action = warn.getAction();
        event.replyFormat("**%s** has received a %s%s (%s warning) " + event.getConfig().getSuccessEmote(), member.getUser().getAsTag(), action.getModAction().getName().toLowerCase(), action instanceof TimeAction ? " for " + TimeUtility.LONG_TIME_FORMATTER.parse(((TimeAction) action).getDuration()) : "", NumberUtility.getSuffixed(warn.getNumber())).queue();
    });
}
Also used : ModAction(com.sx4.bot.entities.mod.action.ModAction) TimeAction(com.sx4.bot.entities.mod.action.TimeAction) Action(com.sx4.bot.entities.mod.action.Action) CompletionException(java.util.concurrent.CompletionException) TimeAction(com.sx4.bot.entities.mod.action.TimeAction) Warn(com.sx4.bot.entities.mod.action.Warn)

Example 14 with Action

use of com.sx4.bot.entities.mod.action.Action 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

Sx4Command (com.sx4.bot.core.Sx4Command)9 Permission (net.dv8tion.jda.api.Permission)8 Document (org.bson.Document)8 Bson (org.bson.conversions.Bson)8 User (net.dv8tion.jda.api.entities.User)7 Argument (com.jockie.bot.core.argument.Argument)6 Command (com.jockie.bot.core.command.Command)6 CommandId (com.sx4.bot.annotations.command.CommandId)6 ModuleCategory (com.sx4.bot.category.ModuleCategory)6 Sx4CommandEvent (com.sx4.bot.core.Sx4CommandEvent)6 com.mongodb.client.model (com.mongodb.client.model)5 Examples (com.sx4.bot.annotations.command.Examples)5 Operators (com.sx4.bot.database.mongo.model.Operators)5 List (java.util.List)5 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)5 AuthorPermissions (com.sx4.bot.annotations.command.AuthorPermissions)4 MongoDatabase (com.sx4.bot.database.mongo.MongoDatabase)4 Action (com.sx4.bot.entities.mod.action.Action)4 PagedResult (com.sx4.bot.paged.PagedResult)4 Reason (com.sx4.bot.entities.mod.Reason)3