Search in sources :

Example 6 with GuildWrapper

use of stream.flarebot.flarebot.objects.GuildWrapper in project FlareBot by FlareBot.

the class WarningsCommand 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("stats")) {
            Map.Entry<String, List<String>> highestEntry = Collections.max(guild.getWarningsMap().entrySet(), Comparator.comparingInt(entry -> entry.getValue().size()));
            User mostWarned = GuildUtils.getUser(highestEntry.getKey(), guild.getGuildId(), true);
            channel.sendMessage(new EmbedBuilder().setTitle("Warning stats", null).addField("Total Warnings", String.valueOf(guild.getWarningsMap().values().stream().mapToLong(List::size).sum()), true).addField("Users warned", String.valueOf(guild.getWarningsMap().size()), true).addField("Most warned user", MessageUtils.getTag(mostWarned) + " - " + highestEntry.getValue().size() + " warnings", true).setColor(Color.CYAN).build()).queue();
        } else {
            User user = GuildUtils.getUser(args[0]);
            if (user == null) {
                MessageUtils.sendErrorMessage("That user could not be found!!", channel);
                return;
            }
            StringBuilder sb = new StringBuilder();
            List<String> tmp = guild.getUserWarnings(user);
            List<String> warnings = tmp.subList(Math.max(tmp.size() - 5, 0), tmp.size());
            int i = 1;
            for (String warning : warnings) {
                sb.append(i).append(". ").append(warning.substring(0, Math.min(725, warning.length()))).append(warning.length() > 725 ? "..." : "").append("\n");
                i++;
            }
            EmbedBuilder eb = new EmbedBuilder().setTitle("Warnings for " + user.getName()).addField("Warning count", String.valueOf(tmp.size()), true).addField("Last 5 warnings", "```md\n" + sb.toString().trim() + "\n```", false).setColor(Color.CYAN);
            channel.sendMessage(eb.build()).queue();
        }
    } else {
        MessageUtils.sendUsage(this, channel, sender, args);
    }
}
Also used : Color(java.awt.Color) Member(net.dv8tion.jda.core.entities.Member) TextChannel(net.dv8tion.jda.core.entities.TextChannel) GuildWrapper(stream.flarebot.flarebot.objects.GuildWrapper) Message(net.dv8tion.jda.core.entities.Message) EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) List(java.util.List) Permission(stream.flarebot.flarebot.permissions.Permission) User(net.dv8tion.jda.core.entities.User) Command(stream.flarebot.flarebot.commands.Command) Map(java.util.Map) MessageUtils(stream.flarebot.flarebot.util.MessageUtils) Comparator(java.util.Comparator) CommandType(stream.flarebot.flarebot.commands.CommandType) Collections(java.util.Collections) GuildUtils(stream.flarebot.flarebot.util.general.GuildUtils) EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) User(net.dv8tion.jda.core.entities.User) List(java.util.List) Map(java.util.Map)

Example 7 with GuildWrapper

use of stream.flarebot.flarebot.objects.GuildWrapper in project FlareBot by FlareBot.

the class Events method handleCommand.

private void handleCommand(GuildMessageReceivedEvent event, Command cmd, String[] args) {
    Metrics.commandsReceived.labels(cmd.getClass().getSimpleName()).inc();
    GuildWrapper guild = flareBot.getManager().getGuild(event.getGuild().getId());
    if (cmd.getType().isInternal()) {
        if (GeneralUtils.canRunInternalCommand(cmd.getType(), event.getAuthor())) {
            dispatchCommand(cmd, args, event, guild);
            return;
        } else {
            GeneralUtils.sendImage("https://flarebot.stream/img/trap.jpg", "trap.jpg", event.getAuthor());
            Constants.logEG("It's a trap", cmd, guild.getGuild(), event.getAuthor());
            return;
        }
    }
    if (guild.hasBetaAccess()) {
        if ((guild.getSettings().getChannelBlacklist().contains(event.getChannel().getIdLong()) || guild.getSettings().getUserBlacklist().contains(event.getAuthor().getIdLong())) && !guild.getPermissions().hasPermission(event.getMember(), stream.flarebot.flarebot.permissions.Permission.BLACKLIST_BYPASS))
            return;
    }
    if (guild.isBlocked() && System.currentTimeMillis() > guild.getUnBlockTime() && guild.getUnBlockTime() != -1)
        guild.revokeBlock();
    handleSpamDetection(event, guild);
    if (guild.isBlocked() && !(cmd.getType() == CommandType.SECRET))
        return;
    if (handleMissingPermission(cmd, event))
        return;
    if (!guild.hasBetaAccess() && cmd.isBetaTesterCommand()) {
        if (flareBot.isTestBot())
            LOGGER.error("Guild " + event.getGuild().getId() + " tried to use the beta command '" + cmd.getCommand() + "'!");
        return;
    }
    if (FlareBot.UPDATING.get()) {
        event.getChannel().sendMessage("**Currently updating!**").queue();
        return;
    }
    if (flareBot.getManager().isCommandDisabled(cmd.getCommand())) {
        MessageUtils.sendErrorMessage(flareBot.getManager().getDisabledCommandReason(cmd.getCommand()), event.getChannel(), event.getAuthor());
        return;
    }
    // Internal stuff
    if (event.getGuild().getIdLong() == Constants.OFFICIAL_GUILD && !handleOfficialGuildStuff(event, cmd))
        return;
    dispatchCommand(cmd, args, event, guild);
}
Also used : GuildWrapper(stream.flarebot.flarebot.objects.GuildWrapper)

Example 8 with GuildWrapper

use of stream.flarebot.flarebot.objects.GuildWrapper in project FlareBot by FlareBot.

the class PlayerListener method onTrackEnd.

@Override
public void onTrackEnd(AudioPlayer aplayer, AudioTrack atrack, AudioTrackEndReason reason) {
    GuildWrapper wrapper = FlareBotManager.instance().getGuild(player.getGuildId());
    // No song on next
    if (player.getPlaylist().isEmpty()) {
        FlareBotManager.instance().getLastActive().put(Long.parseLong(player.getGuildId()), System.currentTimeMillis());
    }
    VoteUtil.remove(SkipCommand.getSkipUUID(), wrapper.getGuild());
    if (wrapper.isSongnickEnabled()) {
        if (GuildUtils.canChangeNick(player.getGuildId())) {
            Guild c = wrapper.getGuild();
            if (c == null) {
                wrapper.setSongnick(false);
            } else {
                if (player.getPlaylist().isEmpty())
                    c.getController().setNickname(c.getSelfMember(), null).queue();
            }
        } else {
            if (!GuildUtils.canChangeNick(player.getGuildId())) {
                MessageUtils.sendPM(Getters.getGuildById(player.getGuildId()).getOwner().getUser(), "FlareBot can't change it's nickname so SongNick has been disabled!");
            }
        }
    }
}
Also used : GuildWrapper(stream.flarebot.flarebot.objects.GuildWrapper) Guild(net.dv8tion.jda.core.entities.Guild)

Example 9 with GuildWrapper

use of stream.flarebot.flarebot.objects.GuildWrapper in project FlareBot by FlareBot.

the class HelpCommand method onCommand.

@Override
public void onCommand(User sender, GuildWrapper guild, TextChannel channel, Message message, String[] args, Member member) {
    if (args.length == 1) {
        CommandType type;
        try {
            type = CommandType.valueOf(args[0].toUpperCase());
        } catch (IllegalArgumentException ignored) {
            channel.sendMessage(MessageUtils.getEmbed(sender).setDescription("No such category!").build()).queue();
            return;
        }
        if (GeneralUtils.canRunInternalCommand(type, sender)) {
            List<String> pages = new ArrayList<>();
            List<String> help = type.getCommands().stream().filter(cmd -> getPermissions(channel).hasPermission(member, cmd.getPermission())).map(command -> guild.getPrefix() + command.getCommand() + " - " + command.getDescription() + '\n').collect(Collectors.toList());
            StringBuilder sb = new StringBuilder();
            for (String s : help) {
                if (sb.length() + s.length() > 1024) {
                    pages.add(sb.toString());
                    sb.setLength(0);
                }
                sb.append(s);
            }
            pages.add(sb.toString());
            PagedEmbedBuilder<String> builder = new PagedEmbedBuilder<>(new PaginationList<>(pages));
            builder.setTitle("***FlareBot " + type + " commands!***");
            builder.setColor(Color.CYAN);
            PaginationUtil.sendEmbedPagedMessage(builder.build(), 0, channel, sender, ButtonGroupConstants.HELP);
        } else {
            channel.sendMessage(MessageUtils.getEmbed(sender).setDescription("No such category!").build()).queue();
        }
    } else {
        sendCommands(channel.getGuild(), channel, sender);
    }
}
Also used : Color(java.awt.Color) PaginationUtil(stream.flarebot.flarebot.util.pagination.PaginationUtil) net.dv8tion.jda.core.entities(net.dv8tion.jda.core.entities) GuildWrapper(stream.flarebot.flarebot.objects.GuildWrapper) GeneralUtils(stream.flarebot.flarebot.util.general.GeneralUtils) Collectors(java.util.stream.Collectors) ButtonGroupConstants(stream.flarebot.flarebot.util.buttons.ButtonGroupConstants) ArrayList(java.util.ArrayList) List(java.util.List) Permission(stream.flarebot.flarebot.permissions.Permission) PagedEmbedBuilder(stream.flarebot.flarebot.util.pagination.PagedEmbedBuilder) Command(stream.flarebot.flarebot.commands.Command) FlareBotManager(stream.flarebot.flarebot.FlareBotManager) MessageUtils(stream.flarebot.flarebot.util.MessageUtils) PaginationList(stream.flarebot.flarebot.util.pagination.PaginationList) CommandType(stream.flarebot.flarebot.commands.CommandType) CommandType(stream.flarebot.flarebot.commands.CommandType) PagedEmbedBuilder(stream.flarebot.flarebot.util.pagination.PagedEmbedBuilder) ArrayList(java.util.ArrayList)

Example 10 with GuildWrapper

use of stream.flarebot.flarebot.objects.GuildWrapper in project FlareBot by FlareBot.

the class Events method onGuildMemberJoin.

@Override
public void onGuildMemberJoin(GuildMemberJoinEvent event) {
    if (event.getMember().getUser().isBot() || event.getMember().getUser().isFake())
        return;
    PlayerCache cache = flareBot.getPlayerCache(event.getMember().getUser().getId());
    cache.setLastSeen(LocalDateTime.now());
    GuildWrapper wrapper = FlareBotManager.instance().getGuild(event.getGuild().getId());
    if (wrapper == null)
        return;
    if (wrapper.isBlocked())
        return;
    if (flareBot.getManager().getGuild(event.getGuild().getId()).getWelcome() != null) {
        Welcome welcome = wrapper.getWelcome();
        if ((welcome.getChannelId() != null && Getters.getChannelById(welcome.getChannelId()) != null) || welcome.isDmEnabled()) {
            if (welcome.getChannelId() != null && Getters.getChannelById(welcome.getChannelId()) != null && welcome.isGuildEnabled()) {
                TextChannel channel = Getters.getChannelById(welcome.getChannelId());
                if (channel == null || !channel.canTalk()) {
                    welcome.setGuildEnabled(false);
                    MessageUtils.sendPM(event.getGuild().getOwner().getUser(), "Cannot send welcome messages in " + (channel == null ? welcome.getChannelId() : channel.getAsMention()) + " due to this, welcomes have been disabled!");
                    return;
                }
                if (welcome.isGuildEnabled()) {
                    String guildMsg = VariableUtils.parseVariables(welcome.getRandomGuildMessage(), wrapper, null, event.getUser());
                    // Deprecated values
                    guildMsg = guildMsg.replace("%user%", event.getMember().getUser().getName()).replace("%guild%", event.getGuild().getName()).replace("%mention%", event.getMember().getUser().getAsMention());
                    channel.sendMessage(guildMsg).queue();
                    if (guildMsg.contains("%user%") || guildMsg.contains("%guild%") || guildMsg.contains("%mention%")) {
                        MessageUtils.sendPM(event.getGuild().getOwner().getUser(), "Your guild welcome message contains deprecated variables! " + "Please check the docs at the link below for a list of all the " + "variables you can use!\n" + "https://docs.flarebot.stream/variables");
                    }
                }
            }
            if (welcome.isDmEnabled()) {
                // We can't DM other bots.
                if (event.getMember().getUser().isBot())
                    return;
                String dmMsg = VariableUtils.parseVariables(welcome.getRandomDmMessage(), wrapper, null, event.getUser());
                // Deprecated values
                dmMsg = dmMsg.replace("%user%", event.getMember().getUser().getName()).replace("%guild%", event.getGuild().getName()).replace("%mention%", event.getMember().getUser().getAsMention());
                MessageUtils.sendPM(event.getMember().getUser(), dmMsg);
                if (dmMsg.contains("%user%") || dmMsg.contains("%guild%") || dmMsg.contains("%mention%")) {
                    MessageUtils.sendPM(event.getGuild().getOwner().getUser(), "Your DM welcome message contains deprecated variables! " + "Please check the docs at the link below for a list of all the " + "variables you can use!\n" + "https://docs.flarebot.stream/variables");
                }
            }
        } else
            welcome.setGuildEnabled(false);
    }
    if (event.getMember().getUser().isBot())
        return;
    if (!wrapper.getAutoAssignRoles().isEmpty()) {
        Set<String> autoAssignRoles = wrapper.getAutoAssignRoles();
        List<Role> roles = new ArrayList<>();
        for (String s : autoAssignRoles) {
            Role role = event.getGuild().getRoleById(s);
            if (role != null) {
                roles.add(role);
            } else
                autoAssignRoles.remove(s);
        }
        try {
            event.getGuild().getController().addRolesToMember(event.getMember(), roles).queue((n) -> {
            }, e1 -> handle(e1, event, roles));
            StringBuilder sb = new StringBuilder("```\n");
            for (Role role : roles) {
                sb.append(role.getName()).append(" (").append(role.getId()).append(")\n");
            }
            sb.append("```");
            ModlogHandler.getInstance().postToModlog(wrapper, ModlogEvent.FLAREBOT_AUTOASSIGN_ROLE, event.getUser(), new MessageEmbed.Field("Roles", sb.toString(), false));
        } catch (Exception e1) {
            handle(e1, event, roles);
        }
    }
}
Also used : MessageEmbed(net.dv8tion.jda.core.entities.MessageEmbed) ArrayList(java.util.ArrayList) PlayerCache(stream.flarebot.flarebot.objects.PlayerCache) InsufficientPermissionException(net.dv8tion.jda.core.exceptions.InsufficientPermissionException) GuildWrapper(stream.flarebot.flarebot.objects.GuildWrapper) Role(net.dv8tion.jda.core.entities.Role) TextChannel(net.dv8tion.jda.core.entities.TextChannel) Welcome(stream.flarebot.flarebot.objects.Welcome)

Aggregations

GuildWrapper (stream.flarebot.flarebot.objects.GuildWrapper)23 MessageUtils (stream.flarebot.flarebot.util.MessageUtils)11 TextChannel (net.dv8tion.jda.core.entities.TextChannel)10 CommandType (stream.flarebot.flarebot.commands.CommandType)10 Permission (stream.flarebot.flarebot.permissions.Permission)9 List (java.util.List)8 EmbedBuilder (net.dv8tion.jda.core.EmbedBuilder)8 Member (net.dv8tion.jda.core.entities.Member)8 Message (net.dv8tion.jda.core.entities.Message)8 User (net.dv8tion.jda.core.entities.User)8 Command (stream.flarebot.flarebot.commands.Command)8 Collectors (java.util.stream.Collectors)7 ArrayList (java.util.ArrayList)6 ButtonGroupConstants (stream.flarebot.flarebot.util.buttons.ButtonGroupConstants)6 Track (com.arsenarsen.lavaplayerbridge.player.Track)5 FlareBot (stream.flarebot.flarebot.FlareBot)5 GeneralUtils (stream.flarebot.flarebot.util.general.GeneralUtils)5 Color (java.awt.Color)4 Pattern (java.util.regex.Pattern)4 net.dv8tion.jda.core.entities (net.dv8tion.jda.core.entities)4