Search in sources :

Example 41 with Or

use of com.sx4.bot.entities.argument.Or in project Sx4 by sx4-discord-bot.

the class RoleCommand method remove.

@Command(value = "remove", description = "Remove a role from a member")
@CommandId(251)
@Redirects({ "removerole", "remove role", "rr" })
@Examples({ "role remove @Shea#6653 Role", "role remove Shea 345718366373150720", "role remove @Role" })
@AuthorPermissions(permissions = { Permission.MANAGE_ROLES })
@BotPermissions(permissions = { Permission.MANAGE_ROLES })
public void remove(Sx4CommandEvent event, @Argument(value = "user", nullDefault = true) Member member, @Argument(value = "role", endless = true) Role role) {
    if (role.isManaged()) {
        event.replyFailure("I cannot remove managed roles").queue();
        return;
    }
    if (role.isPublicRole()) {
        event.replyFailure("I cannot remove the @everyone role").queue();
        return;
    }
    if (!event.getMember().canInteract(role)) {
        event.replyFailure("You cannot remove a role higher or equal than your top role").queue();
        return;
    }
    if (!event.getSelfMember().canInteract(role)) {
        event.replyFailure("I cannot remove a role higher or equal than my top role").queue();
        return;
    }
    Member effectiveMember = member == null ? event.getMember() : member;
    event.getGuild().removeRoleFromMember(effectiveMember, role).flatMap($ -> event.replySuccess(role.getAsMention() + " has been removed from **" + effectiveMember.getUser().getAsTag() + "**")).queue();
}
Also used : Command(com.jockie.bot.core.command.Command) Sx4Command(com.sx4.bot.core.Sx4Command) Permission(net.dv8tion.jda.api.Permission) Set(java.util.Set) CompletableFuture(java.util.concurrent.CompletableFuture) Member(net.dv8tion.jda.api.entities.Member) Colour(com.sx4.bot.annotations.argument.Colour) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ModuleCategory(com.sx4.bot.category.ModuleCategory) Alternative(com.sx4.bot.entities.argument.Alternative) List(java.util.List) Role(net.dv8tion.jda.api.entities.Role) Sx4CommandEvent(com.sx4.bot.core.Sx4CommandEvent) Option(com.jockie.bot.core.option.Option) AlternativeOptions(com.sx4.bot.annotations.argument.AlternativeOptions) FutureUtility(com.sx4.bot.utility.FutureUtility) com.sx4.bot.annotations.command(com.sx4.bot.annotations.command) Argument(com.jockie.bot.core.argument.Argument) Limit(com.sx4.bot.annotations.argument.Limit) Member(net.dv8tion.jda.api.entities.Member) Command(com.jockie.bot.core.command.Command) Sx4Command(com.sx4.bot.core.Sx4Command)

Example 42 with Or

use of com.sx4.bot.entities.argument.Or in project Sx4 by sx4-discord-bot.

the class ServerStatsCommand method onCommand.

public void onCommand(Sx4CommandEvent event, @Argument(value = "duration", endless = true, nullDefault = true) Duration duration, @Option(value = "live", description = "Adds the live counting data to the total") boolean live, @Option(value = "reset", description = "Gets the stats since 00:00 UTC") boolean reset) {
    if (duration != null && (duration.toHours() < 1 || duration.toHours() > 168)) {
        event.replyFailure("Time frame cannot be less than 1 hour or more than 7 days").queue();
        return;
    }
    List<Document> data = event.getMongo().getServerStats(Filters.eq("guildId", event.getGuild().getIdLong()), MongoDatabase.EMPTY_DOCUMENT).into(new ArrayList<>());
    if (data.isEmpty()) {
        event.replyFailure("There has been no data recorded for this server yet").queue();
        return;
    }
    Date lastUpdate = event.getBot().getServerStatsManager().getLastUpdate();
    OffsetDateTime currentHour = OffsetDateTime.now(ZoneOffset.UTC).withMinute(0).withSecond(0).withNano(0);
    if (reset) {
        duration = Duration.between(currentHour.withHour(0), currentHour);
    }
    Map<Long, Map<ServerStatsType, Integer>> map = new HashMap<>();
    if (duration == null) {
        Map<ServerStatsType, Integer> defaultStatsDay = new HashMap<>();
        defaultStatsDay.put(ServerStatsType.JOINS, 0);
        defaultStatsDay.put(ServerStatsType.MESSAGES, 0);
        map.put(24L, defaultStatsDay);
        Map<ServerStatsType, Integer> defaultStatsWeek = new HashMap<>();
        defaultStatsWeek.put(ServerStatsType.JOINS, 0);
        defaultStatsWeek.put(ServerStatsType.MESSAGES, 0);
        map.put(168L, defaultStatsWeek);
    } else {
        Map<ServerStatsType, Integer> defaultStats = new HashMap<>();
        defaultStats.put(ServerStatsType.JOINS, 0);
        defaultStats.put(ServerStatsType.MESSAGES, 0);
        map.put(duration.toHours(), defaultStats);
    }
    for (Document stats : data) {
        Date time = stats.getDate("time");
        Duration difference = Duration.between(time.toInstant(), currentHour);
        int joins = stats.getInteger("joins", 0), messages = stats.getInteger("messages", 0);
        if (duration != null && difference.toHours() <= duration.toHours()) {
            this.compute(map, duration.toHours(), joins, messages);
        }
        if (duration == null && difference.toHours() <= 24) {
            this.compute(map, 24, joins, messages);
        }
        if (duration == null && difference.toDays() <= 7) {
            this.compute(map, 168, joins, messages);
        }
        lastUpdate = lastUpdate == null || lastUpdate.getTime() < time.getTime() ? time : lastUpdate;
    }
    EmbedBuilder embed = new EmbedBuilder().setAuthor("Server Stats", null, event.getGuild().getIconUrl()).setFooter("Updated every hour").setTimestamp(lastUpdate.toInstant());
    ServerStatsManager manager = event.getBot().getServerStatsManager();
    for (long key : map.keySet()) {
        Map<ServerStatsType, Integer> stats = map.get(key);
        for (ServerStatsType type : stats.keySet()) {
            embed.addField(StringUtility.title(type.getField()) + " (" + TimeUtility.LONG_TIME_FORMATTER.parse(Duration.of(key, ChronoUnit.HOURS)) + ")", String.format("%,d", stats.get(type) + (live ? manager.getCounter(event.getGuild().getIdLong(), type) : 0)), false);
        }
    }
    event.reply(embed.build()).queue();
}
Also used : ServerStatsType(com.sx4.bot.entities.info.ServerStatsType) Duration(java.time.Duration) ServerStatsManager(com.sx4.bot.managers.ServerStatsManager) Document(org.bson.Document) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) OffsetDateTime(java.time.OffsetDateTime)

Example 43 with Or

use of com.sx4.bot.entities.argument.Or in project Sx4 by sx4-discord-bot.

the class KickCommand 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 kick me, that is illegal").queue();
        return;
    }
    if (!event.getMember().canInteract(member)) {
        event.replyFailure("You cannot kick someone higher or equal than your top role").queue();
        return;
    }
    if (!event.getSelfMember().canInteract(member)) {
        event.replyFailure("I cannot kick someone higher or equal than my top role").queue();
        return;
    }
    member.kick().reason(ModUtility.getAuditReason(reason, event.getAuthor())).queue($ -> {
        event.replySuccess("**" + member.getUser().getAsTag() + "** has been kicked").queue();
        event.getBot().getModActionManager().onModAction(new KickEvent(event.getMember(), member.getUser(), reason));
    });
}
Also used : KickEvent(com.sx4.bot.events.mod.KickEvent)

Aggregations

Sx4Command (com.sx4.bot.core.Sx4Command)36 Command (com.jockie.bot.core.command.Command)25 Document (org.bson.Document)25 Argument (com.jockie.bot.core.argument.Argument)19 ModuleCategory (com.sx4.bot.category.ModuleCategory)19 Sx4CommandEvent (com.sx4.bot.core.Sx4CommandEvent)19 Bson (org.bson.conversions.Bson)17 CommandId (com.sx4.bot.annotations.command.CommandId)16 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)16 Examples (com.sx4.bot.annotations.command.Examples)15 PagedResult (com.sx4.bot.paged.PagedResult)15 Permission (net.dv8tion.jda.api.Permission)15 Member (net.dv8tion.jda.api.entities.Member)13 List (java.util.List)12 CompletionException (java.util.concurrent.CompletionException)10 Option (com.jockie.bot.core.option.Option)8 Collectors (java.util.stream.Collectors)8 MessageBuilder (net.dv8tion.jda.api.MessageBuilder)8 AlternativeOptions (com.sx4.bot.annotations.argument.AlternativeOptions)7 Operators (com.sx4.bot.database.mongo.model.Operators)7