Search in sources :

Example 1 with PagedTableBuilder

use of stream.flarebot.flarebot.util.pagination.PagedTableBuilder in project FlareBot by FlareBot.

the class ShardInfoCommand method onCommand.

@Override
public void onCommand(User sender, GuildWrapper guild, TextChannel channel, Message message, String[] args, Member member) {
    PagedTableBuilder tb = new PagedTableBuilder();
    tb.addColumn("Shard ID");
    tb.addColumn("Status");
    tb.addColumn("Ping");
    tb.addColumn("Guild Count");
    tb.addColumn("Connected VCs");
    List<JDA> shards = new ArrayList<>(Getters.getShards());
    Collections.reverse(shards);
    for (JDA jda : shards) {
        List<String> row = new ArrayList<>();
        row.add(ShardUtils.getDisplayShardId(jda) + (ShardUtils.getShardId(channel.getJDA()) == ShardUtils.getShardId(jda) ? " (You)" : ""));
        row.add(WordUtils.capitalizeFully(jda.getStatus().toString().replace("_", " ")));
        row.add(String.valueOf(jda.getPing()));
        row.add(String.valueOf(jda.getGuilds().size()));
        row.add(String.valueOf(jda.getVoiceChannels().stream().filter(vc -> vc.getMembers().contains(vc.getGuild().getSelfMember())).count()));
        tb.addRow(row);
    }
    PaginationUtil.sendPagedMessage(channel, tb.build(), 0, sender, ButtonGroupConstants.SHARDINFO);
}
Also used : PagedTableBuilder(stream.flarebot.flarebot.util.pagination.PagedTableBuilder) WordUtils(org.apache.commons.lang3.text.WordUtils) PaginationUtil(stream.flarebot.flarebot.util.pagination.PaginationUtil) Member(net.dv8tion.jda.core.entities.Member) TextChannel(net.dv8tion.jda.core.entities.TextChannel) ShardUtils(stream.flarebot.flarebot.util.ShardUtils) PagedTableBuilder(stream.flarebot.flarebot.util.pagination.PagedTableBuilder) GuildWrapper(stream.flarebot.flarebot.objects.GuildWrapper) Message(net.dv8tion.jda.core.entities.Message) Getters(stream.flarebot.flarebot.Getters) ButtonGroupConstants(stream.flarebot.flarebot.util.buttons.ButtonGroupConstants) ArrayList(java.util.ArrayList) List(java.util.List) Permission(stream.flarebot.flarebot.permissions.Permission) User(net.dv8tion.jda.core.entities.User) Command(stream.flarebot.flarebot.commands.Command) JDA(net.dv8tion.jda.core.JDA) CommandType(stream.flarebot.flarebot.commands.CommandType) Collections(java.util.Collections) JDA(net.dv8tion.jda.core.JDA) ArrayList(java.util.ArrayList)

Example 2 with PagedTableBuilder

use of stream.flarebot.flarebot.util.pagination.PagedTableBuilder in project FlareBot by FlareBot.

the class ReportsCommand method onCommand.

@Override
public void onCommand(User sender, GuildWrapper guild, TextChannel channel, Message message, String[] args, Member member) {
    if (args.length == 0) {
        MessageUtils.sendUsage(this, channel, sender, args);
    } else {
        if (args[0].equalsIgnoreCase("list")) {
            if (args.length <= 2) {
                if (getPermissions(channel).hasPermission(member, Permission.REPORTS_LIST)) {
                    if (guild.getReportManager().getReports().isEmpty()) {
                        MessageUtils.sendInfoMessage("No Reports for this guild!", channel, sender);
                        return;
                    }
                    PagedTableBuilder tb = new PagedTableBuilder();
                    ArrayList<String> header = new ArrayList<>();
                    header.add("Id");
                    header.add("Reported");
                    header.add("Time");
                    header.add("Status");
                    tb.setColumns(header);
                    List<Report> reports = guild.getReportManager().getReports();
                    for (Report report : reports) {
                        ArrayList<String> row = new ArrayList<>();
                        row.add(String.valueOf(report.getId()));
                        row.add(MessageUtils.getTag(Getters.getUserById(String.valueOf(report.getReportedId()))));
                        row.add(report.getTime().toLocalDateTime().atOffset(ZoneOffset.UTC).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) + " GMT/BST");
                        row.add(report.getStatus().getMessage());
                        tb.addRow(row);
                    }
                    tb.setRowCount(10);
                    int page = 0;
                    if (args.length == 2) {
                        page = GeneralUtils.getInt(args[1], 0);
                    }
                    PaginationUtil.sendPagedMessage(channel, tb.build(), page, sender, ButtonGroupConstants.REPORTS_LIST);
                } else {
                    MessageUtils.sendErrorMessage("You need the permission `" + Permission.REPORTS_LIST + "`", channel);
                }
            } else {
                MessageUtils.sendUsage(this, channel, sender, args);
            }
        } else if (args[0].equalsIgnoreCase("view")) {
            if (args.length == 2) {
                int id;
                try {
                    id = Integer.valueOf(args[1]);
                } catch (Exception e) {
                    MessageUtils.sendErrorMessage("Invalid report number: " + args[1] + ".", channel);
                    return;
                }
                Report report = guild.getReportManager().getReport(id);
                if (report == null) {
                    MessageUtils.sendErrorMessage("That report doesn't exist.", channel);
                    return;
                }
                if (getPermissions(channel).hasPermission(member, Permission.REPORTS_VIEW) || report.getReporterId().equals(sender.getId())) {
                    channel.sendMessage(GeneralUtils.getReportEmbed(sender, report).build()).queue();
                } else {
                    MessageUtils.sendErrorMessage("You need the permission `" + Permission.REPORTS_VIEW + "` to do this! Or you need to be the creator of the report", channel);
                }
            } else {
                MessageUtils.sendUsage(this, channel, sender, args);
            }
        } else if (args[0].equalsIgnoreCase("status")) {
            if (args.length >= 3) {
                if (getPermissions(channel).hasPermission(member, Permission.REPORTS_STATUS)) {
                    int id;
                    try {
                        id = Integer.valueOf(args[1]);
                    } catch (Exception e) {
                        MessageUtils.sendErrorMessage("Invalid report number: " + args[1] + ".", channel);
                        return;
                    }
                    ReportStatus status;
                    try {
                        status = ReportStatus.valueOf(MessageUtils.getMessage(args, 2).toUpperCase().replace(" ", "_"));
                    } catch (IllegalArgumentException e) {
                        EmbedBuilder errorBuilder = new EmbedBuilder();
                        errorBuilder.setDescription("Invalid status: `" + args[2] + "`");
                        errorBuilder.addField("Statuses", "**" + Arrays.stream(ReportStatus.values()).map(ReportStatus::getMessage).collect(Collectors.joining("**\n**")) + "**", false);
                        MessageUtils.sendErrorMessage(errorBuilder, channel);
                        return;
                    }
                    Report report = guild.getReportManager().getReport(id);
                    if (report == null) {
                        MessageUtils.sendErrorMessage("Invalid report ID!", channel);
                        return;
                    }
                    if (report.getStatus() == status) {
                        MessageUtils.sendInfoMessage("Current status is: **" + status.getMessage() + "**", channel, sender);
                    } else {
                        ReportStatus old = report.getStatus();
                        report.setStatus(status);
                        MessageUtils.sendSuccessMessage(String.format("Changed status of Report with ID: **%d** to **%s**", id, status.getMessage()), channel, sender);
                        ModlogHandler.getInstance().postToModlog(guild, ModlogEvent.REPORT_EDITED, null, sender, null, new MessageEmbed.Field("Report ID", String.valueOf(id), true), new MessageEmbed.Field("Old Status", old.getMessage(), true), new MessageEmbed.Field("New Status", report.getStatus().getMessage(), true), new MessageEmbed.Field("Responsible moderator", sender.getAsMention(), true));
                    }
                } else {
                    MessageUtils.sendErrorMessage("You need the permission `" + Permission.REPORTS_STATUS + "` to do this.", channel);
                }
            } else {
                MessageUtils.sendUsage(this, channel, sender, args);
            }
        } else {
            MessageUtils.sendUsage(this, channel, sender, args);
        }
    }
}
Also used : Report(stream.flarebot.flarebot.objects.Report) ArrayList(java.util.ArrayList) PagedTableBuilder(stream.flarebot.flarebot.util.pagination.PagedTableBuilder) EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) ReportStatus(stream.flarebot.flarebot.objects.ReportStatus)

Example 3 with PagedTableBuilder

use of stream.flarebot.flarebot.util.pagination.PagedTableBuilder in project FlareBot by FlareBot.

the class ModlogCommand method onCommand.

@Override
public void onCommand(User sender, GuildWrapper guild, TextChannel channel, Message message, String[] args, Member member) {
    if (args.length >= 1) {
        if (args[0].equalsIgnoreCase("list")) {
            int page = 1;
            if (args.length == 2) {
                page = GeneralUtils.getInt(args[1], 1);
            }
            Set<ModlogAction> actions = guild.getModeration().getEnabledActions();
            List<ModlogAction> events = new ArrayList<>(actions);
            if (events.isEmpty()) {
                MessageUtils.sendErrorMessage("No Events are enabled", channel);
                return;
            }
            PagedTableBuilder tb = new PagedTableBuilder();
            tb.addColumn("Event");
            tb.addColumn("Compact");
            tb.addColumn("Channel");
            tb.setRowCount(10);
            for (ModlogAction modlogAction : events) {
                if (guild.getModeration().isEventEnabled(guild, modlogAction.getEvent())) {
                    List<String> part = new ArrayList<>();
                    part.add(modlogAction.getEvent().getName());
                    part.add(String.valueOf(modlogAction.isCompacted()));
                    part.add(modlogAction.getModlogChannel(guild.getGuild()).getName());
                    tb.addRow(part);
                }
            }
            PaginationUtil.sendPagedMessage(channel, tb.build(), page - 1, sender, ButtonGroupConstants.MODLOG_LIST);
            return;
        }
        if (args[0].equalsIgnoreCase("features")) {
            int page = 1;
            if (args.length == 2) {
                page = GeneralUtils.getInt(args[1], 1);
            }
            listEvents(channel, page, sender);
            return;
        }
    }
    if (args.length >= 2) {
        String eventArgument = MessageUtils.getMessage(args, 1, args[0].equalsIgnoreCase("enable") ? Math.max(2, args.length - 1) : args.length);
        ModlogEvent event = ModlogEvent.getEvent(eventArgument);
        boolean all = false;
        boolean defaultEvents = false;
        if (event == null) {
            if (eventArgument.equalsIgnoreCase("all"))
                all = true;
            else if (eventArgument.equalsIgnoreCase("default"))
                defaultEvents = true;
            else {
                MessageUtils.sendErrorMessage("Invalid Event: `" + eventArgument + "`\n" + "For a list of all events do `{%}modlog features`, " + "for a list of all enabled events do `{%}modlog list`.", channel);
                return;
            }
        }
        Moderation moderation = guild.getModeration();
        if (args[0].equalsIgnoreCase("enable")) {
            TextChannel tc = GuildUtils.getChannel(args[args.length - 1], guild);
            if (tc == null) {
                MessageUtils.sendErrorMessage("I cannot find the channel `" + args[args.length - 1] + "` try to mention the channel " + "or use the channel ID", channel);
                return;
            }
            long channelId = tc.getIdLong();
            if (!guild.getGuild().getSelfMember().hasPermission(Permission.VIEW_AUDIT_LOGS)) {
                MessageUtils.sendErrorMessage("I don't have permission to view audit logs so you can't use Modlog events!", channel);
                return;
            }
            if (all) {
                moderation.enableAllEvents(guild, channelId);
                MessageUtils.sendSuccessMessage("Successfully enabled **all** events in " + tc.getAsMention(), channel, sender);
                return;
            } else if (defaultEvents) {
                moderation.enableDefaultEvents(guild, channelId);
                MessageUtils.sendSuccessMessage("Successfully enabled **default** events in " + tc.getAsMention(), channel, sender);
                return;
            } else {
                if (moderation.enableEvent(guild, channelId, event)) {
                    MessageUtils.sendSuccessMessage("Successfully enabled event `" + WordUtils.capitalize(event.getTitle().toLowerCase().replaceAll("_", " ")) + "`\nThis event will be displayed in the " + tc.getAsMention() + " channel.", channel, sender);
                    return;
                } else {
                    MessageUtils.sendErrorMessage("Error enabling event, I couldn't find that channel!", channel, sender);
                    return;
                }
            }
        }
        if (args[0].equalsIgnoreCase("disable")) {
            if (all) {
                moderation.disableAllEvents();
                MessageUtils.sendSuccessMessage("Successfully disabled **all** events", channel, sender);
                return;
            } else if (defaultEvents) {
                moderation.disableDefaultEvents();
                MessageUtils.sendSuccessMessage("Successfully disabled all **default** events", channel, sender);
                return;
            } else {
                if (moderation.isEventEnabled(guild, event)) {
                    moderation.disableEvent(event);
                    MessageUtils.sendSuccessMessage("Successfully disabled event `" + WordUtils.capitalize(event.getTitle()) + "`", channel, sender);
                    return;
                } else {
                    MessageUtils.sendErrorMessage("Error disabling event (Probably already disabled)", channel, sender);
                    return;
                }
            }
        }
        if (args[0].equalsIgnoreCase("compact")) {
            if (all) {
                int compact = 0;
                int uncompact = 0;
                for (ModlogEvent modlogEvent : ModlogEvent.values) {
                    if (moderation.isEventEnabled(guild, modlogEvent)) {
                        if (moderation.isEventCompacted(modlogEvent)) {
                            compact++;
                        } else {
                            uncompact++;
                        }
                    }
                }
                for (ModlogEvent modlogEvents : ModlogEvent.values) {
                    moderation.setEventCompact(modlogEvents, uncompact >= compact);
                }
                MessageUtils.sendSuccessMessage((uncompact >= compact ? "Compacted" : "Un-compacted") + " all the modlog events", channel, sender);
                return;
            } else if (defaultEvents) {
                int compact = 0;
                int uncompact = 0;
                for (ModlogEvent modlogEvent : ModlogEvent.values) {
                    if (modlogEvent.isDefaultEvent() && moderation.isEventEnabled(guild, modlogEvent)) {
                        if (moderation.isEventCompacted(modlogEvent))
                            compact++;
                        else
                            uncompact++;
                    }
                }
                for (ModlogEvent modlogEvent : ModlogEvent.values) {
                    if (modlogEvent.isDefaultEvent())
                        moderation.setEventCompact(modlogEvent, compact >= uncompact);
                }
                MessageUtils.sendSuccessMessage((compact >= uncompact ? "Un-compacted" : "Compacted") + " all the default modlog events", channel, sender);
            } else {
                if (moderation.isEventEnabled(guild, event)) {
                    boolean compact = moderation.setEventCompact(event, !moderation.isEventCompacted(event));
                    if (compact) {
                        MessageUtils.sendSuccessMessage("Compacted event `" + WordUtils.capitalize(event.getTitle()) + "`", channel, sender);
                        return;
                    } else {
                        MessageUtils.sendSuccessMessage("Un-compacted event `" + WordUtils.capitalize(event.getTitle()) + "`", channel, sender);
                        return;
                    }
                } else {
                    MessageUtils.sendErrorMessage("You can't compact an event that isn't enabled!", channel);
                    return;
                }
            }
        }
    }
    MessageUtils.sendUsage(this, channel, member.getUser(), args);
}
Also used : Moderation(stream.flarebot.flarebot.mod.Moderation) ModlogAction(stream.flarebot.flarebot.mod.modlog.ModlogAction) ArrayList(java.util.ArrayList) ModlogEvent(stream.flarebot.flarebot.mod.modlog.ModlogEvent) PagedTableBuilder(stream.flarebot.flarebot.util.pagination.PagedTableBuilder) TextChannel(net.dv8tion.jda.core.entities.TextChannel)

Aggregations

ArrayList (java.util.ArrayList)3 PagedTableBuilder (stream.flarebot.flarebot.util.pagination.PagedTableBuilder)3 TextChannel (net.dv8tion.jda.core.entities.TextChannel)2 Collections (java.util.Collections)1 List (java.util.List)1 EmbedBuilder (net.dv8tion.jda.core.EmbedBuilder)1 JDA (net.dv8tion.jda.core.JDA)1 Member (net.dv8tion.jda.core.entities.Member)1 Message (net.dv8tion.jda.core.entities.Message)1 User (net.dv8tion.jda.core.entities.User)1 WordUtils (org.apache.commons.lang3.text.WordUtils)1 Getters (stream.flarebot.flarebot.Getters)1 Command (stream.flarebot.flarebot.commands.Command)1 CommandType (stream.flarebot.flarebot.commands.CommandType)1 Moderation (stream.flarebot.flarebot.mod.Moderation)1 ModlogAction (stream.flarebot.flarebot.mod.modlog.ModlogAction)1 ModlogEvent (stream.flarebot.flarebot.mod.modlog.ModlogEvent)1 GuildWrapper (stream.flarebot.flarebot.objects.GuildWrapper)1 Report (stream.flarebot.flarebot.objects.Report)1 ReportStatus (stream.flarebot.flarebot.objects.ReportStatus)1