Search in sources :

Example 21 with UserId

use of com.sx4.bot.annotations.argument.UserId in project Sx4 by sx4-discord-bot.

the class MuteCommand method list.

@Command(value = "list", description = "Lists all the currently muted users in the server")
@CommandId(343)
@Redirects({ "muted list" })
@Examples({ "mute list" })
public void list(Sx4CommandEvent event) {
    List<Document> mutes = event.getMongo().getMutes(Filters.eq("guildId", event.getGuild().getIdLong()), Projections.include("unmuteAt", "userId")).into(new ArrayList<>());
    if (mutes.isEmpty()) {
        event.replyFailure("There is no one muted in this server").queue();
        return;
    }
    mutes.sort(Comparator.comparingLong(d -> d.getLong("unmuteAt")));
    PagedResult<Document> paged = new PagedResult<>(event.getBot(), mutes).setAuthor("Muted Users", null, event.getGuild().getIconUrl()).setIndexed(false).setSelect().setDisplayFunction(data -> {
        User user = event.getShardManager().getUserById(data.getLong("userId"));
        return (user == null ? "Anonymous#0000" : user.getAsTag()) + " - " + TimeUtility.LONG_TIME_FORMATTER.parse(data.getLong("unmuteAt") - Clock.systemUTC().instant().getEpochSecond());
    });
    paged.execute(event);
}
Also used : Document(org.bson.Document) Command(com.jockie.bot.core.command.Command) Permission(net.dv8tion.jda.api.Permission) CommandId(com.sx4.bot.annotations.command.CommandId) ModAction(com.sx4.bot.entities.mod.action.ModAction) Member(net.dv8tion.jda.api.entities.Member) TimedArgument(com.sx4.bot.entities.argument.TimedArgument) PagedResult(com.sx4.bot.paged.PagedResult) User(net.dv8tion.jda.api.entities.User) ArrayList(java.util.ArrayList) Bson(org.bson.conversions.Bson) Redirects(com.sx4.bot.annotations.command.Redirects) Alternative(com.sx4.bot.entities.argument.Alternative) Role(net.dv8tion.jda.api.entities.Role) Sx4CommandEvent(com.sx4.bot.core.Sx4CommandEvent) TimeUtility(com.sx4.bot.utility.TimeUtility) Duration(java.time.Duration) Option(com.jockie.bot.core.option.Option) AlternativeOptions(com.sx4.bot.annotations.argument.AlternativeOptions) Reason(com.sx4.bot.entities.mod.Reason) com.mongodb.client.model(com.mongodb.client.model) Argument(com.jockie.bot.core.argument.Argument) AuthorPermissions(com.sx4.bot.annotations.command.AuthorPermissions) Operators(com.sx4.bot.database.mongo.model.Operators) Sx4Command(com.sx4.bot.core.Sx4Command) ModUtility(com.sx4.bot.utility.ModUtility) EnumOptions(com.sx4.bot.annotations.argument.EnumOptions) CompletionException(java.util.concurrent.CompletionException) ModuleCategory(com.sx4.bot.category.ModuleCategory) List(java.util.List) Examples(com.sx4.bot.annotations.command.Examples) Clock(java.time.Clock) ExceptionUtility(com.sx4.bot.utility.ExceptionUtility) Comparator(java.util.Comparator) User(net.dv8tion.jda.api.entities.User) Document(org.bson.Document) PagedResult(com.sx4.bot.paged.PagedResult) Redirects(com.sx4.bot.annotations.command.Redirects) 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 22 with UserId

use of com.sx4.bot.annotations.argument.UserId in project Sx4 by sx4-discord-bot.

the class TemporaryBanCommand method onCommand.

public void onCommand(Sx4CommandEvent event, @Argument(value = "user") String userArgument, @Argument(value = "time", nullDefault = true) Duration time, @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;
        }
        Guild guild = event.getGuild();
        Member member = guild.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) {
                Document data = event.getMongo().getGuildById(guild.getIdLong(), Projections.include("temporaryBan.defaultTime")).get("temporaryBan", MongoDatabase.EMPTY_DOCUMENT);
                long duration = time == null ? data.get("defaultTime", ModUtility.DEFAULT_TEMPORARY_BAN_DURATION) : time.toSeconds();
                List<Bson> update = List.of(Operators.set("unbanAt", Operators.add(Operators.nowEpochSecond(), duration)));
                Bson filter = Filters.and(Filters.eq("userId", event.getMember().getIdLong()), Filters.eq("guildId", event.getGuild().getIdLong()));
                event.getMongo().updateTemporaryBan(filter, update, new UpdateOptions().upsert(true)).whenComplete((result, resultException) -> {
                    if (ExceptionUtility.sendExceptionally(event, resultException)) {
                        return;
                    }
                    event.getGuild().ban(user, days).reason(ModUtility.getAuditReason(reason, event.getAuthor())).queue($ -> {
                        event.replySuccess("**" + user.getAsTag() + "** has been temporarily banned for " + TimeUtility.LONG_TIME_FORMATTER.parse(duration)).queue();
                        event.getBot().getModActionManager().onModAction(new TemporaryBanEvent(event.getMember(), user, reason, member != null, duration));
                        event.getBot().getTemporaryBanManager().putBan(event.getGuild().getIdLong(), user.getIdLong(), duration);
                    });
                });
            } else {
                if (ExceptionUtility.sendExceptionally(event, exception)) {
                    return;
                }
                event.replyFailure("That user is already banned").queue();
            }
        });
    });
}
Also used : ErrorResponseException(net.dv8tion.jda.api.exceptions.ErrorResponseException) Guild(net.dv8tion.jda.api.entities.Guild) Document(org.bson.Document) TemporaryBanEvent(com.sx4.bot.events.mod.TemporaryBanEvent) Member(net.dv8tion.jda.api.entities.Member) UpdateOptions(com.mongodb.client.model.UpdateOptions) Bson(org.bson.conversions.Bson)

Example 23 with UserId

use of com.sx4.bot.annotations.argument.UserId in project Sx4 by sx4-discord-bot.

the class TemporaryBanCommand method list.

@Command(value = "list", description = "Lists all the users who have temporary bans")
@CommandId(345)
@Examples({ "temporary ban list" })
public void list(Sx4CommandEvent event) {
    List<Document> bans = event.getMongo().getTemporaryBans(Filters.eq("guildId", event.getGuild().getIdLong()), Projections.include("unbanAt", "userId")).into(new ArrayList<>());
    if (bans.isEmpty()) {
        event.replyFailure("There is no one with a temporary ban in this server").queue();
        return;
    }
    bans.sort(Comparator.comparingLong(d -> d.getLong("unbanAt")));
    PagedResult<Document> paged = new PagedResult<>(event.getBot(), bans).setAuthor("Muted Users", null, event.getGuild().getIconUrl()).setIndexed(false).setSelect().setDisplayFunction(data -> {
        User user = event.getShardManager().getUserById(data.getLong("userId"));
        return (user == null ? "Anonymous#0000" : user.getAsTag()) + " - " + TimeUtility.LONG_TIME_FORMATTER.parse(data.getLong("unbanAt") - Clock.systemUTC().instant().getEpochSecond());
    });
    paged.execute(event);
}
Also used : Document(org.bson.Document) Command(com.jockie.bot.core.command.Command) Permission(net.dv8tion.jda.api.Permission) Projections(com.mongodb.client.model.Projections) MongoDatabase(com.sx4.bot.database.mongo.MongoDatabase) CommandId(com.sx4.bot.annotations.command.CommandId) Member(net.dv8tion.jda.api.entities.Member) ErrorResponse(net.dv8tion.jda.api.requests.ErrorResponse) PagedResult(com.sx4.bot.paged.PagedResult) User(net.dv8tion.jda.api.entities.User) ArrayList(java.util.ArrayList) Filters(com.mongodb.client.model.Filters) Bson(org.bson.conversions.Bson) Guild(net.dv8tion.jda.api.entities.Guild) Sx4CommandEvent(com.sx4.bot.core.Sx4CommandEvent) TimeUtility(com.sx4.bot.utility.TimeUtility) Duration(java.time.Duration) Option(com.jockie.bot.core.option.Option) TemporaryBanEvent(com.sx4.bot.events.mod.TemporaryBanEvent) Reason(com.sx4.bot.entities.mod.Reason) UpdateOptions(com.mongodb.client.model.UpdateOptions) Argument(com.jockie.bot.core.argument.Argument) Limit(com.sx4.bot.annotations.argument.Limit) AuthorPermissions(com.sx4.bot.annotations.command.AuthorPermissions) Operators(com.sx4.bot.database.mongo.model.Operators) ErrorResponseException(net.dv8tion.jda.api.exceptions.ErrorResponseException) Sx4Command(com.sx4.bot.core.Sx4Command) ModUtility(com.sx4.bot.utility.ModUtility) SearchUtility(com.sx4.bot.utility.SearchUtility) Updates(com.mongodb.client.model.Updates) ModuleCategory(com.sx4.bot.category.ModuleCategory) List(java.util.List) DefaultNumber(com.sx4.bot.annotations.argument.DefaultNumber) Examples(com.sx4.bot.annotations.command.Examples) Clock(java.time.Clock) ExceptionUtility(com.sx4.bot.utility.ExceptionUtility) Comparator(java.util.Comparator) User(net.dv8tion.jda.api.entities.User) Document(org.bson.Document) PagedResult(com.sx4.bot.paged.PagedResult) 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 UserId

use of com.sx4.bot.annotations.argument.UserId in project Sx4 by sx4-discord-bot.

the class WarnCommand method view.

@Command(value = "view", description = "View the amount of warnings a specific user is on")
@CommandId(259)
@Examples({ "warn view @Shea#6653", "warn view Shea", "warn view 402557516728369153" })
@Redirects({ "warnings", "warns" })
public void view(Sx4CommandEvent event, @Argument(value = "user", endless = true) Member member) {
    List<Bson> pipeline = List.of(Aggregates.match(Filters.and(Filters.eq("userId", member.getIdLong()), Filters.eq("guildId", event.getGuild().getIdLong()))), Aggregates.project(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")))))));
    event.getMongo().aggregateWarnings(pipeline).whenComplete((documents, exception) -> {
        if (ExceptionUtility.sendExceptionally(event, exception)) {
            return;
        }
        Document data = documents.isEmpty() ? MongoDatabase.EMPTY_DOCUMENT : documents.get(0);
        int warnings = data.getInteger("warnings", 0);
        event.reply("**" + member.getUser().getAsTag() + "** is currently on **" + warnings + "** warning" + (warnings == 1 ? "" : "s")).queue();
    });
}
Also used : Document(org.bson.Document) Bson(org.bson.conversions.Bson) Command(com.jockie.bot.core.command.Command) Sx4Command(com.sx4.bot.core.Sx4Command)

Example 25 with UserId

use of com.sx4.bot.annotations.argument.UserId in project Sx4 by sx4-discord-bot.

the class WarnCommand method set.

@Command(value = "set", description = "Set the amount of warns a user has")
@CommandId(242)
@Redirects({ "set warns", "set warnings" })
@Examples({ "warn set @Shea#6653 2", "warn set Shea#6653 0", "warn set 402557516728369153 1" })
@AuthorPermissions(permissions = { Permission.MANAGE_SERVER })
public void set(Sx4CommandEvent event, @Argument(value = "user") Member member, @Argument(value = "warnings") int warnings) {
    if (member.canInteract(event.getMember())) {
        event.replyFailure("You cannot change the amount of warnings of someone higher or equal than your top role").queue();
        return;
    }
    Document warnData = event.getMongo().getGuildById(event.getGuild().getIdLong(), Projections.include("warn")).get("warn", MongoDatabase.EMPTY_DOCUMENT);
    boolean punishments = warnData.get("punishments", true);
    int maxWarning = punishments ? warnData.getList("config", Document.class, Warn.DEFAULT_CONFIG).stream().map(d -> d.getInteger("number")).max(Integer::compareTo).get() : Integer.MAX_VALUE;
    if (warnings > maxWarning) {
        event.replyFailure("The max amount of warnings you can give is **" + maxWarning + "**").queue();
        return;
    }
    Bson update = warnings == 0 ? Updates.unset("warnings") : Updates.combine(Updates.set("warnings", warnings), Updates.set("lastWarning", Clock.systemUTC().instant().getEpochSecond()));
    Bson filter = Filters.and(Filters.eq("userId", member.getIdLong()), Filters.eq("guildId", event.getGuild().getIdLong()));
    event.getMongo().updateWarnings(filter, update, new UpdateOptions().upsert(true)).whenComplete((result, exception) -> {
        if (ExceptionUtility.sendExceptionally(event, exception)) {
            return;
        }
        if (result.getModifiedCount() == 0 && result.getUpsertedId() == null) {
            event.replyFailure("That user already had that amount of warnings").queue();
            return;
        }
        event.replySuccess("That user now has **" + warnings + "** warning" + (warnings == 1 ? "" : "s")).queue();
    });
}
Also used : Document(org.bson.Document) MarkdownSanitizer(net.dv8tion.jda.api.utils.MarkdownSanitizer) Command(com.jockie.bot.core.command.Command) Permission(net.dv8tion.jda.api.Permission) MongoDatabase(com.sx4.bot.database.mongo.MongoDatabase) ModAction(com.sx4.bot.entities.mod.action.ModAction) Member(net.dv8tion.jda.api.entities.Member) TimedArgument(com.sx4.bot.entities.argument.TimedArgument) PagedResult(com.sx4.bot.paged.PagedResult) User(net.dv8tion.jda.api.entities.User) ArrayList(java.util.ArrayList) TimeAction(com.sx4.bot.entities.mod.action.TimeAction) Bson(org.bson.conversions.Bson) Alternative(com.sx4.bot.entities.argument.Alternative) Sx4CommandEvent(com.sx4.bot.core.Sx4CommandEvent) TimeUtility(com.sx4.bot.utility.TimeUtility) Duration(java.time.Duration) AlternativeOptions(com.sx4.bot.annotations.argument.AlternativeOptions) Reason(com.sx4.bot.entities.mod.Reason) com.mongodb.client.model(com.mongodb.client.model) com.sx4.bot.annotations.command(com.sx4.bot.annotations.command) Warn(com.sx4.bot.entities.mod.action.Warn) Argument(com.jockie.bot.core.argument.Argument) Limit(com.sx4.bot.annotations.argument.Limit) Action(com.sx4.bot.entities.mod.action.Action) Operators(com.sx4.bot.database.mongo.model.Operators) Sx4Command(com.sx4.bot.core.Sx4Command) NumberUtility(com.sx4.bot.utility.NumberUtility) ModUtility(com.sx4.bot.utility.ModUtility) CompletionException(java.util.concurrent.CompletionException) ModuleCategory(com.sx4.bot.category.ModuleCategory) List(java.util.List) Clock(java.time.Clock) ExceptionUtility(com.sx4.bot.utility.ExceptionUtility) Comparator(java.util.Comparator) Document(org.bson.Document) Bson(org.bson.conversions.Bson) Command(com.jockie.bot.core.command.Command) Sx4Command(com.sx4.bot.core.Sx4Command)

Aggregations

Document (org.bson.Document)46 Bson (org.bson.conversions.Bson)44 Sx4Command (com.sx4.bot.core.Sx4Command)36 Command (com.jockie.bot.core.command.Command)28 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)26 User (net.dv8tion.jda.api.entities.User)26 CommandId (com.sx4.bot.annotations.command.CommandId)25 Examples (com.sx4.bot.annotations.command.Examples)23 Operators (com.sx4.bot.database.mongo.model.Operators)23 List (java.util.List)21 ExceptionUtility (com.sx4.bot.utility.ExceptionUtility)20 Permission (net.dv8tion.jda.api.Permission)20 Member (net.dv8tion.jda.api.entities.Member)20 ModuleCategory (com.sx4.bot.category.ModuleCategory)18 Sx4CommandEvent (com.sx4.bot.core.Sx4CommandEvent)18 PagedResult (com.sx4.bot.paged.PagedResult)14 Argument (com.jockie.bot.core.argument.Argument)13 com.mongodb.client.model (com.mongodb.client.model)13 ArrayList (java.util.ArrayList)13 BotPermissions (com.sx4.bot.annotations.command.BotPermissions)12