Search in sources :

Example 1 with BanEvent

use of com.sx4.bot.events.mod.BanEvent in project Sx4 by sx4-discord-bot.

the class BanCommand method onCommand.

public void onCommand(Sx4CommandEvent event, @Argument(value = "user") String userArgument, @Argument(value = "reason", endless = true, nullDefault = true) Reason reason, @Option(value = "days", description = "Set how many days of messages should be deleted from the user") @DefaultNumber(1) @Limit(min = 0, max = 7) int days) {
    SearchUtility.getUser(event.getShardManager(), userArgument).thenAccept(user -> {
        if (user == null) {
            event.replyFailure("I could not find that user").queue();
            return;
        }
        if (user.getIdLong() == event.getSelfUser().getIdLong()) {
            event.replyFailure("You cannot ban me, that is illegal").queue();
            return;
        }
        Member member = event.getGuild().getMember(user);
        if (member != null) {
            if (!event.getMember().canInteract(member)) {
                event.replyFailure("You cannot ban someone higher or equal than your top role").queue();
                return;
            }
            if (!event.getSelfMember().canInteract(member)) {
                event.replyFailure("I cannot ban someone higher or equal than my top role").queue();
                return;
            }
        }
        event.getGuild().retrieveBan(user).submit().whenComplete((ban, exception) -> {
            if (exception instanceof ErrorResponseException && ((ErrorResponseException) exception).getErrorResponse() == ErrorResponse.UNKNOWN_BAN) {
                event.getGuild().ban(user, days).reason(ModUtility.getAuditReason(reason, event.getAuthor())).queue($ -> {
                    event.replySuccess("**" + user.getAsTag() + "** has been banned").queue();
                    event.getBot().getModActionManager().onModAction(new BanEvent(event.getMember(), user, reason, member != null));
                });
            } else {
                event.replyFailure("That user is already banned").queue();
            }
        });
    });
}
Also used : BanEvent(com.sx4.bot.events.mod.BanEvent) ErrorResponseException(net.dv8tion.jda.api.exceptions.ErrorResponseException) Member(net.dv8tion.jda.api.entities.Member)

Example 2 with BanEvent

use of com.sx4.bot.events.mod.BanEvent in project Sx4 by sx4-discord-bot.

the class ModUtility method performAction.

public static CompletableFuture<? extends Action> performAction(Sx4 bot, Action action, Member target, Member moderator, Reason reason) {
    Guild guild = target.getGuild();
    switch(action.getModAction()) {
        case WARN:
            return ModUtility.warn(bot, target, moderator, reason);
        case MUTE:
        case MUTE_EXTEND:
            if (!guild.getSelfMember().hasPermission(Permission.MANAGE_ROLES)) {
                return CompletableFuture.failedFuture(new BotPermissionException(Permission.MANAGE_ROLES));
            }
            return ModUtility.mute(bot, target, moderator, Duration.ofSeconds(((TimeAction) action).getDuration()), action.getModAction().isExtend(), reason);
        case KICK:
            if (!guild.getSelfMember().hasPermission(Permission.KICK_MEMBERS)) {
                return CompletableFuture.failedFuture(new BotPermissionException(Permission.KICK_MEMBERS));
            }
            if (!guild.getSelfMember().canInteract(target)) {
                return CompletableFuture.failedFuture(new BotHierarchyException("kick"));
            }
            if (!moderator.hasPermission(Permission.KICK_MEMBERS)) {
                return CompletableFuture.failedFuture(new AuthorPermissionException(Permission.KICK_MEMBERS));
            }
            return target.kick(ModUtility.getAuditReason(reason, moderator.getUser())).submit().thenApply($ -> {
                bot.getModActionManager().onModAction(new KickEvent(moderator, target.getUser(), reason));
                return action;
            });
        case TEMPORARY_BAN:
            if (!guild.getSelfMember().hasPermission(Permission.BAN_MEMBERS)) {
                return CompletableFuture.failedFuture(new BotPermissionException(Permission.BAN_MEMBERS));
            }
            if (!guild.getSelfMember().canInteract(target)) {
                return CompletableFuture.failedFuture(new BotHierarchyException("ban"));
            }
            if (!moderator.hasPermission(Permission.BAN_MEMBERS)) {
                return CompletableFuture.failedFuture(new AuthorPermissionException(Permission.BAN_MEMBERS));
            }
            long temporaryBanDuration = ((TimeAction) action).getDuration();
            List<Bson> temporaryBanUpdate = List.of(Operators.set("unbanAt", Operators.add(Operators.nowEpochSecond(), temporaryBanDuration)));
            Bson filter = Filters.and(Filters.eq("userId", target.getIdLong()), Filters.eq("guildId", guild.getIdLong()));
            return bot.getMongo().updateTemporaryBan(filter, temporaryBanUpdate, new UpdateOptions().upsert(true)).thenCompose(temporaryBanResult -> target.ban(1).reason(ModUtility.getAuditReason(reason, moderator.getUser())).submit()).thenApply($ -> {
                bot.getModActionManager().onModAction(new TemporaryBanEvent(moderator, target.getUser(), reason, true, temporaryBanDuration));
                bot.getTemporaryBanManager().putBan(guild.getIdLong(), target.getIdLong(), temporaryBanDuration);
                return action;
            });
        case BAN:
            if (!guild.getSelfMember().hasPermission(Permission.BAN_MEMBERS)) {
                return CompletableFuture.failedFuture(new BotPermissionException(Permission.BAN_MEMBERS));
            }
            if (!guild.getSelfMember().canInteract(target)) {
                return CompletableFuture.failedFuture(new BotHierarchyException("ban"));
            }
            if (!moderator.hasPermission(Permission.BAN_MEMBERS)) {
                return CompletableFuture.failedFuture(new AuthorPermissionException(Permission.BAN_MEMBERS));
            }
            return target.ban(1).reason(ModUtility.getAuditReason(reason, moderator.getUser())).submit().thenApply($ -> {
                bot.getModActionManager().onModAction(new BanEvent(moderator, target.getUser(), reason, true));
                return action;
            });
        default:
            return CompletableFuture.completedFuture(null);
    }
}
Also used : Document(org.bson.Document) Operators(com.sx4.bot.database.mongo.model.Operators) BotPermissionException(com.sx4.bot.exceptions.mod.BotPermissionException) com.sx4.bot.entities.mod.action(com.sx4.bot.entities.mod.action) Permission(net.dv8tion.jda.api.Permission) MongoDatabase(com.sx4.bot.database.mongo.MongoDatabase) CompletableFuture(java.util.concurrent.CompletableFuture) MaxRolesException(com.sx4.bot.exceptions.mod.MaxRolesException) Member(net.dv8tion.jda.api.entities.Member) AuthorPermissionException(com.sx4.bot.exceptions.mod.AuthorPermissionException) BotHierarchyException(com.sx4.bot.exceptions.mod.BotHierarchyException) AtomicReference(java.util.concurrent.atomic.AtomicReference) User(net.dv8tion.jda.api.entities.User) Bson(org.bson.conversions.Bson) List(java.util.List) Guild(net.dv8tion.jda.api.entities.Guild) Sx4(com.sx4.bot.core.Sx4) Role(net.dv8tion.jda.api.entities.Role) Duration(java.time.Duration) Reason(com.sx4.bot.entities.mod.Reason) Clock(java.time.Clock) com.sx4.bot.events.mod(com.sx4.bot.events.mod) com.mongodb.client.model(com.mongodb.client.model) Collections(java.util.Collections) Guild(net.dv8tion.jda.api.entities.Guild) Bson(org.bson.conversions.Bson) BotPermissionException(com.sx4.bot.exceptions.mod.BotPermissionException) BotHierarchyException(com.sx4.bot.exceptions.mod.BotHierarchyException) AuthorPermissionException(com.sx4.bot.exceptions.mod.AuthorPermissionException)

Aggregations

Member (net.dv8tion.jda.api.entities.Member)2 com.mongodb.client.model (com.mongodb.client.model)1 Sx4 (com.sx4.bot.core.Sx4)1 MongoDatabase (com.sx4.bot.database.mongo.MongoDatabase)1 Operators (com.sx4.bot.database.mongo.model.Operators)1 Reason (com.sx4.bot.entities.mod.Reason)1 com.sx4.bot.entities.mod.action (com.sx4.bot.entities.mod.action)1 com.sx4.bot.events.mod (com.sx4.bot.events.mod)1 BanEvent (com.sx4.bot.events.mod.BanEvent)1 AuthorPermissionException (com.sx4.bot.exceptions.mod.AuthorPermissionException)1 BotHierarchyException (com.sx4.bot.exceptions.mod.BotHierarchyException)1 BotPermissionException (com.sx4.bot.exceptions.mod.BotPermissionException)1 MaxRolesException (com.sx4.bot.exceptions.mod.MaxRolesException)1 Clock (java.time.Clock)1 Duration (java.time.Duration)1 Collections (java.util.Collections)1 List (java.util.List)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Permission (net.dv8tion.jda.api.Permission)1