Search in sources :

Example 26 with TextChannel

use of net.dv8tion.jda.core.entities.TextChannel in project FlareBot by FlareBot.

the class SaveCommand 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);
        return;
    }
    String name = MessageUtils.getMessage(args, 0);
    if (name.length() > 20) {
        MessageUtils.sendErrorMessage("Name can only be a maximum of 20 characters!", channel);
        return;
    }
    if (!FlareBot.instance().getMusicManager().hasPlayer(channel.getGuild().getId())) {
        MessageUtils.sendErrorMessage("Your playlist is empty!", channel);
        return;
    }
    Queue<Track> playlist = FlareBot.instance().getMusicManager().getPlayer(guild.getGuildId()).getPlaylist();
    Track currentlyPlaying = FlareBot.instance().getMusicManager().getPlayer(guild.getGuildId()).getPlayingTrack();
    channel.sendTyping().complete();
    List<String> tracks = playlist.stream().map(track -> track.getTrack().getIdentifier()).collect(Collectors.toList());
    if (currentlyPlaying != null) {
        tracks.add(currentlyPlaying.getTrack().getIdentifier());
    }
    if (tracks.isEmpty()) {
        MessageUtils.sendErrorMessage("Your playlist is empty!", channel);
        return;
    }
    FlareBot.instance().getManager().savePlaylist(this, channel, sender.getId(), this.getPermissions(channel).hasPermission(member, Permission.SAVE_OVERWRITE), name, tracks);
}
Also used : Track(com.arsenarsen.lavaplayerbridge.player.Track) Member(net.dv8tion.jda.core.entities.Member) TextChannel(net.dv8tion.jda.core.entities.TextChannel) FlareBot(stream.flarebot.flarebot.FlareBot) GuildWrapper(stream.flarebot.flarebot.objects.GuildWrapper) Collectors(java.util.stream.Collectors) Message(net.dv8tion.jda.core.entities.Message) List(java.util.List) Permission(stream.flarebot.flarebot.permissions.Permission) User(net.dv8tion.jda.core.entities.User) Command(stream.flarebot.flarebot.commands.Command) MessageUtils(stream.flarebot.flarebot.util.MessageUtils) Queue(java.util.Queue) CommandType(stream.flarebot.flarebot.commands.CommandType) Track(com.arsenarsen.lavaplayerbridge.player.Track)

Example 27 with TextChannel

use of net.dv8tion.jda.core.entities.TextChannel in project FlareBot by FlareBot.

the class LockChatCommand method onCommand.

@Override
public void onCommand(User sender, GuildWrapper guild, TextChannel channel, Message message, String[] args, Member member) {
    if (!guild.getGuild().getSelfMember().hasPermission(Permission.MANAGE_ROLES)) {
        MessageUtils.sendErrorMessage("I can't lock the chat due to lack of permissions! " + "I need the `Manage Roles` permission", channel);
        return;
    }
    String reason = null;
    long time = -1;
    @Nonnull AtomicReference<TextChannel> tc = new AtomicReference<>(channel);
    if (args.length >= 1) {
        TextChannel tmp = ParseUtils.parseChannel(guild.getGuild(), args[0], false);
        if (tmp != null)
            tc.set(tmp);
        if (tmp == null || args.length >= 2) {
            Long l = GeneralUtils.parseTime(tmp == null ? args[0] : args[1]);
            if (l == null) {
                MessageUtils.sendErrorMessage("Invalid time format! Please use something like `1h10m`", channel);
                return;
            }
            time = l;
        }
        if (tmp != null && time > 0)
            reason = MessageUtils.getMessage(args, 2);
        else if ((tmp == null && time > 0) || (tmp != null && time == -1))
            reason = MessageUtils.getMessage(args, 1);
        else
            reason = MessageUtils.getMessage(args, 0);
        if (reason.isEmpty())
            reason = null;
    }
    PermissionOverride everyoneOvr = tc.get().getPermissionOverride(guild.getGuild().getPublicRole());
    boolean locking = !everyoneOvr.getDenied().contains(Permission.MESSAGE_WRITE);
    EnumSet<Permission> perm = EnumSet.of(Permission.MESSAGE_WRITE);
    EnumSet<Permission> empty = EnumSet.noneOf(Permission.class);
    tc.get().getPermissionOverride(guild.getGuild().getPublicRole()).getManager().deny(locking ? perm : empty).clear(locking ? empty : perm).reason(reason + "\nDone by: " + sender.getIdLong()).queue();
    tc.get().putPermissionOverride(guild.getGuild().getSelfMember()).setPermissions(locking ? perm : empty, empty).reason(reason + "\nDone by: " + sender.getIdLong()).queue();
    if (tc.get().getIdLong() != channel.getIdLong())
        channel.sendMessage(new EmbedBuilder().setColor(locking ? ColorUtils.RED : ColorUtils.GREEN).setDescription(tc.get().getAsMention() + " has been " + (locking ? "locked" : "unlocked") + "!").build()).queue();
    if (guild.getGuild().getSelfMember().hasPermission(tc.get(), Permission.MESSAGE_WRITE))
        channel.sendMessage(new EmbedBuilder().setColor(locking ? ColorUtils.RED : ColorUtils.GREEN).setDescription("The chat has been " + (locking ? "locked" : "unlocked") + " by a staff member" + (locking && time > 0 ? " for " + FormatUtils.formatTime(time, TimeUnit.MILLISECONDS, true, false) : "") + "!" + (reason != null ? "\nReason: " + reason : "")).build()).queue();
    if (locking && time > 0) {
        new FlareBotTask("ChannelUnlock-" + tc.get().getIdLong()) {

            @Override
            public void run() {
                tc.get().getPermissionOverride(guild.getGuild().getPublicRole()).getManager().clear(Permission.MESSAGE_WRITE).queue();
                if (guild.getGuild().getSelfMember().hasPermission(tc.get(), Permission.MESSAGE_WRITE))
                    channel.sendMessage(new EmbedBuilder().setColor(ColorUtils.GREEN).setDescription("The chat has been unlocked").build()).queue();
            }
        }.delay(time);
    }
}
Also used : PermissionOverride(net.dv8tion.jda.core.entities.PermissionOverride) Nonnull(javax.annotation.Nonnull) AtomicReference(java.util.concurrent.atomic.AtomicReference) TextChannel(net.dv8tion.jda.core.entities.TextChannel) EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) Permission(net.dv8tion.jda.core.Permission) FlareBotTask(stream.flarebot.flarebot.scheduler.FlareBotTask) PermissionOverride(net.dv8tion.jda.core.entities.PermissionOverride) PermissionOverride(net.dv8tion.jda.core.entities.PermissionOverride)

Example 28 with TextChannel

use of net.dv8tion.jda.core.entities.TextChannel 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 29 with TextChannel

use of net.dv8tion.jda.core.entities.TextChannel in project FlareBot by FlareBot.

the class SettingsCommand 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")) {
            channel.sendMessage(MessageUtils.getEmbed(sender).setTitle("Guild Settings").setDescription(String.format("`delete-commands` - %b\n" + "`blacklisted-channels` - %s\n" + "`blaclisted-users` - %s", guild.getSettings().shouldDeleteCommands(), getBlacklistedChannels(guild), getBlacklistedUsers(guild))).build()).queue();
        } else if (args[0].equalsIgnoreCase("change")) {
            if (args.length >= 3) {
                if (args[1].equalsIgnoreCase("delete-commands")) {
                    if (args[2].equalsIgnoreCase("true") || args[2].equalsIgnoreCase("yes") || args[2].equalsIgnoreCase("y")) {
                        guild.getSettings().setDeleteCommands(true);
                        MessageUtils.sendSuccessMessage("FlareBot will now delete the commands sent by users!", channel);
                    } else if (args[2].equalsIgnoreCase("false") || args[2].equalsIgnoreCase("no") || args[2].equalsIgnoreCase("n")) {
                        guild.getSettings().setDeleteCommands(false);
                        MessageUtils.sendWarningMessage("FlareBot will no longer delete the commands sent by users!", channel);
                    } else
                        MessageUtils.sendWarningMessage("Invalid value! Expecting `yes` or `no`", channel);
                } else
                    MessageUtils.sendWarningMessage("Invalid option or invalid option which can be changed with " + "the `change` argument. Do `{%}usage " + getCommand() + "` and see if you can find " + "the argument required!", channel);
            } else
                MessageUtils.sendUsage(this, channel, sender, args);
        } else if (args[0].equalsIgnoreCase("blacklist")) {
            if (args.length >= 3) {
                if (args[1].equalsIgnoreCase("list")) {
                    if (args[2].equalsIgnoreCase("channels") || args[2].equalsIgnoreCase("channel"))
                        MessageUtils.sendInfoMessage(getBlacklistedChannels(guild), channel);
                    else if (args[2].equalsIgnoreCase("users") || args[2].equalsIgnoreCase("user"))
                        MessageUtils.sendInfoMessage(getBlacklistedUsers(guild), channel);
                    else
                        MessageUtils.sendWarningMessage("Invalid blacklist item!", channel);
                } else if (args[1].equalsIgnoreCase("add")) {
                    TextChannel tc = GuildUtils.getChannel(args[2], guild);
                    if (tc != null) {
                        guild.getSettings().getChannelBlacklist().add(tc.getIdLong());
                        MessageUtils.sendSuccessMessage("Added " + tc.getAsMention() + " to the blacklist!", channel);
                        return;
                    }
                    User user = GuildUtils.getUser(args[2], guild.getGuildId());
                    if (user != null) {
                        guild.getSettings().getUserBlacklist().add(user.getIdLong());
                        MessageUtils.sendSuccessMessage("Added " + user.getAsMention() + " to the blacklist!", channel);
                        return;
                    }
                    MessageUtils.sendWarningMessage("Invalid channel or user! Try the ID if you're sure the " + "entity is valid", channel);
                } else if (args[1].equalsIgnoreCase("remove")) {
                    TextChannel tc = GuildUtils.getChannel(args[2], guild);
                    if (tc != null) {
                        if (!guild.getSettings().getChannelBlacklist().contains(tc.getIdLong())) {
                            MessageUtils.sendWarningMessage("That channel is not blacklisted!", channel);
                            return;
                        }
                        guild.getSettings().getChannelBlacklist().remove(tc.getIdLong());
                        MessageUtils.sendSuccessMessage("Removed " + tc.getAsMention() + " from the blacklist!", channel);
                        return;
                    }
                    User user = GuildUtils.getUser(args[2], guild.getGuildId());
                    if (user != null) {
                        if (!guild.getSettings().getUserBlacklist().contains(user.getIdLong())) {
                            MessageUtils.sendWarningMessage("That user is not blacklisted!", channel);
                            return;
                        }
                        guild.getSettings().getUserBlacklist().remove(user.getIdLong());
                        MessageUtils.sendSuccessMessage("Removed " + user.getAsMention() + " from the blacklist!", channel);
                        return;
                    }
                    MessageUtils.sendWarningMessage("Invalid channel or user! Try the ID if you're sure the " + "entity is valid", channel);
                } else
                    MessageUtils.sendUsage(this, channel, sender, args);
            } else
                MessageUtils.sendUsage(this, channel, sender, args);
        } else
            MessageUtils.sendUsage(this, channel, sender, args);
    } else
        MessageUtils.sendUsage(this, channel, sender, args);
}
Also used : TextChannel(net.dv8tion.jda.core.entities.TextChannel) User(net.dv8tion.jda.core.entities.User)

Example 30 with TextChannel

use of net.dv8tion.jda.core.entities.TextChannel in project FlareBot by FlareBot.

the class GuildUtils method getRole.

/**
 * Gets a {@link Role} that matches a string. Case doesn't matter.
 *
 * @param s       The String to get a role from
 * @param guildId The id of the {@link Guild} to get the role from
 * @param channel The channel to send an error message to if anything goes wrong.
 * @return null if the role doesn't, otherwise a list of roles matching the string
 */
public static Role getRole(String s, String guildId, TextChannel channel) {
    Guild guild = Getters.getGuildById(guildId);
    Role role = guild.getRoles().stream().filter(r -> r.getName().equalsIgnoreCase(s)).findFirst().orElse(null);
    if (role != null)
        return role;
    try {
        role = guild.getRoleById(Long.parseLong(s.replaceAll("[^0-9]", "")));
        if (role != null)
            return role;
    } catch (NumberFormatException | NullPointerException ignored) {
    }
    if (channel != null) {
        if (guild.getRolesByName(s, true).isEmpty()) {
            String closest = null;
            int distance = LEVENSHTEIN_DISTANCE;
            for (Role role1 : guild.getRoles().stream().filter(role1 -> FlareBotManager.instance().getGuild(guildId).getSelfAssignRoles().contains(role1.getId())).collect(Collectors.toList())) {
                int currentDistance = StringUtils.getLevenshteinDistance(role1.getName(), s);
                if (currentDistance < distance) {
                    distance = currentDistance;
                    closest = role1.getName();
                }
            }
            MessageUtils.sendErrorMessage("That role does not exist! " + (closest != null ? "Maybe you mean `" + closest + "`" : ""), channel);
            return null;
        } else {
            return guild.getRolesByName(s, true).get(0);
        }
    }
    return null;
}
Also used : Role(net.dv8tion.jda.core.entities.Role) Role(net.dv8tion.jda.core.entities.Role) Member(net.dv8tion.jda.core.entities.Member) TextChannel(net.dv8tion.jda.core.entities.TextChannel) FlareBot(stream.flarebot.flarebot.FlareBot) PlayerManager(com.arsenarsen.lavaplayerbridge.PlayerManager) StringUtils(org.apache.commons.lang3.StringUtils) GuildWrapper(stream.flarebot.flarebot.objects.GuildWrapper) Collectors(java.util.stream.Collectors) Getters(stream.flarebot.flarebot.Getters) Objects(java.util.Objects) Guild(net.dv8tion.jda.core.entities.Guild) List(java.util.List) Permission(net.dv8tion.jda.core.Permission) User(net.dv8tion.jda.core.entities.User) Emote(net.dv8tion.jda.core.entities.Emote) FlareBotManager(stream.flarebot.flarebot.FlareBotManager) Constants(stream.flarebot.flarebot.util.Constants) MessageUtils(stream.flarebot.flarebot.util.MessageUtils) Pattern(java.util.regex.Pattern) Guild(net.dv8tion.jda.core.entities.Guild)

Aggregations

TextChannel (net.dv8tion.jda.core.entities.TextChannel)91 EmbedBuilder (net.dv8tion.jda.core.EmbedBuilder)27 Guild (net.dv8tion.jda.core.entities.Guild)22 User (net.dv8tion.jda.core.entities.User)20 Member (net.dv8tion.jda.core.entities.Member)18 List (java.util.List)17 Message (net.dv8tion.jda.core.entities.Message)17 ArrayList (java.util.ArrayList)14 VoiceChannel (net.dv8tion.jda.core.entities.VoiceChannel)13 GuildWrapper (stream.flarebot.flarebot.objects.GuildWrapper)13 MessageUtils (stream.flarebot.flarebot.util.MessageUtils)13 Collectors (java.util.stream.Collectors)11 CommandType (stream.flarebot.flarebot.commands.CommandType)8 Role (net.dv8tion.jda.core.entities.Role)7 FlareBot (stream.flarebot.flarebot.FlareBot)7 Track (com.arsenarsen.lavaplayerbridge.player.Track)6 MessageEmbed (net.dv8tion.jda.core.entities.MessageEmbed)6 MantaroData (net.kodehawa.mantarobot.data.MantaroData)6 DBGuild (net.kodehawa.mantarobot.db.entities.DBGuild)6 EmoteReference (net.kodehawa.mantarobot.utils.commands.EmoteReference)6