Search in sources :

Example 6 with Warn

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

the class WarnCommand method list.

@Command(value = "list", description = "Lists all the warned users in the server and how many warnings they have")
@CommandId(258)
@Examples({ "warn list" })
@BotPermissions(permissions = { Permission.MESSAGE_EMBED_LINKS })
public void list(Sx4CommandEvent event) {
    List<Bson> pipeline = List.of(Aggregates.match(Filters.eq("guildId", event.getGuild().getIdLong())), Aggregates.project(Projections.fields(Projections.include("userId"), Projections.computed("warnings", Operators.cond(Operators.or(Operators.isNull("$reset"), Operators.isNull("$warnings")), Operators.ifNull("$warnings", 0), Operators.max(0, Operators.subtract("$warnings", Operators.multiply(Operators.toInt(Operators.floor(Operators.divide(Operators.subtract(Operators.nowEpochSecond(), "$lastWarning"), "$reset.after"))), "$reset.amount"))))))), Aggregates.match(Filters.ne("warnings", 0)), Aggregates.sort(Sorts.descending("warnings")));
    event.getMongo().aggregateWarnings(pipeline).whenComplete((users, exception) -> {
        if (ExceptionUtility.sendExceptionally(event, exception)) {
            return;
        }
        if (users.isEmpty()) {
            event.replyFailure("There are no users with warnings in this server").queue();
            return;
        }
        PagedResult<Document> paged = new PagedResult<>(event.getBot(), users).setAuthor("Warned Users", null, event.getGuild().getIconUrl()).setIndexed(false).setDisplayFunction(data -> {
            long userId = data.getLong("userId");
            User user = event.getShardManager().getUserById(userId);
            return "`" + (user == null ? "Anonymous#0000 (" + userId + ")" : MarkdownSanitizer.escape(user.getAsTag())) + "` - Warning **#" + data.getInteger("warnings") + "**";
        });
        paged.execute(event);
    });
}
Also used : User(net.dv8tion.jda.api.entities.User) Document(org.bson.Document) Bson(org.bson.conversions.Bson) Command(com.jockie.bot.core.command.Command) Sx4Command(com.sx4.bot.core.Sx4Command)

Example 7 with Warn

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

Aggregations

Document (org.bson.Document)6 Bson (org.bson.conversions.Bson)6 Command (com.jockie.bot.core.command.Command)4 Sx4Command (com.sx4.bot.core.Sx4Command)4 User (net.dv8tion.jda.api.entities.User)4 com.mongodb.client.model (com.mongodb.client.model)3 MongoDatabase (com.sx4.bot.database.mongo.MongoDatabase)3 Operators (com.sx4.bot.database.mongo.model.Operators)3 Reason (com.sx4.bot.entities.mod.Reason)3 Clock (java.time.Clock)3 Duration (java.time.Duration)3 List (java.util.List)3 Permission (net.dv8tion.jda.api.Permission)3 Member (net.dv8tion.jda.api.entities.Member)3 Sx4 (com.sx4.bot.core.Sx4)2 com.sx4.bot.entities.mod.action (com.sx4.bot.entities.mod.action)2 Action (com.sx4.bot.entities.mod.action.Action)2 ModAction (com.sx4.bot.entities.mod.action.ModAction)2 TimeAction (com.sx4.bot.entities.mod.action.TimeAction)2 Warn (com.sx4.bot.entities.mod.action.Warn)2