Search in sources :

Example 16 with DBGuild

use of net.kodehawa.mantarobot.db.entities.DBGuild in project MantaroBot by Mantaro.

the class ModerationOptions method onRegistry.

@Subscribe
public void onRegistry(OptionRegistryEvent e) {
    registerOption("localblacklist:add", "Local Blacklist add", "Adds someone to the local blacklist.\n" + "You need to mention the user. You can mention multiple users.\n" + "**Example:** `~>opts localblacklist add @user1 @user2`", "Adds someone to the local blacklist.", (event, args) -> {
        List<User> mentioned = event.getMessage().getMentionedUsers();
        if (mentioned.isEmpty()) {
            event.getChannel().sendMessage(EmoteReference.ERROR + "**You need to specify the users to locally blacklist.**").queue();
            return;
        }
        if (mentioned.contains(event.getAuthor())) {
            event.getChannel().sendMessage(EmoteReference.ERROR + "Why are you trying to blacklist yourself?...").queue();
            return;
        }
        Guild guild = event.getGuild();
        if (mentioned.stream().anyMatch(u -> CommandPermission.ADMIN.test(guild.getMember(u)))) {
            event.getChannel().sendMessage(EmoteReference.ERROR + "One (or more) of the users you're trying to blacklist are admins or Bot Commanders!").queue();
            return;
        }
        DBGuild dbGuild = MantaroData.db().getGuild(guild);
        GuildData guildData = dbGuild.getData();
        List<String> toBlackList = mentioned.stream().map(ISnowflake::getId).collect(Collectors.toList());
        String blacklisted = mentioned.stream().map(user -> user.getName() + "#" + user.getDiscriminator()).collect(Collectors.joining(","));
        guildData.getDisabledUsers().addAll(toBlackList);
        dbGuild.save();
        event.getChannel().sendMessage(EmoteReference.CORRECT + "Locally blacklisted users: **" + blacklisted + "**").queue();
    });
    registerOption("localblacklist:remove", "Local Blacklist remove", "Removes someone from the local blacklist.\n" + "You need to mention the user. You can mention multiple users.\n" + "**Example:** `~>opts localblacklist remove @user1 @user2`", "Removes someone from the local blacklist.", (event, args) -> {
        List<User> mentioned = event.getMessage().getMentionedUsers();
        if (mentioned.isEmpty()) {
            event.getChannel().sendMessage(EmoteReference.ERROR + "**You need to specify the users to locally blacklist.**").queue();
            return;
        }
        DBGuild dbGuild = MantaroData.db().getGuild(event.getGuild());
        GuildData guildData = dbGuild.getData();
        List<String> toUnBlackList = mentioned.stream().map(ISnowflake::getId).collect(Collectors.toList());
        String unBlackListed = mentioned.stream().map(user -> user.getName() + "#" + user.getDiscriminator()).collect(Collectors.joining(","));
        guildData.getDisabledUsers().removeAll(toUnBlackList);
        dbGuild.save();
        event.getChannel().sendMessage(EmoteReference.CORRECT + "Locally unblacklisted users: **" + unBlackListed + "**").queue();
    });
    // region logs
    // region enable
    registerOption("logs:enable", "Enable logs", "Enables logs. You need to use the channel name.\n" + "**Example:** `~>opts logs enable mod-logs`", "Enables logs.", (event, args) -> {
        if (args.length < 1) {
            onHelp(event);
            return;
        }
        String logChannel = args[0];
        DBGuild dbGuild = MantaroData.db().getGuild(event.getGuild());
        GuildData guildData = dbGuild.getData();
        Consumer<TextChannel> consumer = textChannel -> {
            guildData.setGuildLogChannel(textChannel.getId());
            dbGuild.saveAsync();
            event.getChannel().sendMessage(String.format(EmoteReference.MEGA + "Message logging has been enabled with parameters -> ``Channel #%s (%s)``", textChannel.getName(), textChannel.getId())).queue();
        };
        TextChannel channel = Utils.findChannelSelect(event, logChannel, consumer);
        if (channel != null) {
            consumer.accept(channel);
        }
    });
    registerOption("logs:exclude", "Exclude log channel.", "Excludes a channel from logging. You need to use the channel name, *not* the mention.\n" + "**Example:** `~>opts logs exclude staff`", "Excludes a channel from logging.", (event, args) -> {
        if (args.length == 0) {
            onHelp(event);
            return;
        }
        DBGuild dbGuild = MantaroData.db().getGuild(event.getGuild());
        GuildData guildData = dbGuild.getData();
        if (args[0].equals("clearchannels")) {
            guildData.getLogExcludedChannels().clear();
            dbGuild.saveAsync();
            event.getChannel().sendMessage(EmoteReference.OK + "Cleared log exceptions!").queue();
            return;
        }
        if (args[0].equals("remove")) {
            if (args.length < 2) {
                event.getChannel().sendMessage(EmoteReference.ERROR + "Incorrect argument length.").queue();
                return;
            }
            String channel = args[1];
            List<TextChannel> channels = event.getGuild().getTextChannelsByName(channel, true);
            if (channels.size() == 0) {
                event.getChannel().sendMessage(EmoteReference.ERROR + "I didn't find a channel with that name!").queue();
            } else if (channels.size() == 1) {
                TextChannel ch = channels.get(0);
                guildData.getLogExcludedChannels().remove(ch.getId());
                dbGuild.saveAsync();
                event.getChannel().sendMessage(EmoteReference.OK + "Removed logs exception on channel: " + ch.getAsMention()).queue();
            } else {
                DiscordUtils.selectList(event, channels, ch -> String.format("%s (ID: %s)", ch.getName(), ch.getId()), s -> ((SimpleCommand) optsCmd).baseEmbed(event, "Select the Channel:").setDescription(s).build(), ch -> {
                    guildData.getLogExcludedChannels().remove(ch.getId());
                    dbGuild.saveAsync();
                    event.getChannel().sendMessage(EmoteReference.OK + "Removed logs exception on channel: " + ch.getAsMention()).queue();
                });
            }
            return;
        }
        String channel = args[0];
        List<TextChannel> channels = event.getGuild().getTextChannelsByName(channel, true);
        if (channels.size() == 0) {
            event.getChannel().sendMessage(EmoteReference.ERROR + "I didn't find a channel with that name!").queue();
        } else if (channels.size() == 1) {
            TextChannel ch = channels.get(0);
            guildData.getLogExcludedChannels().add(ch.getId());
            dbGuild.saveAsync();
            event.getChannel().sendMessage(EmoteReference.OK + "Added logs exception on channel: " + ch.getAsMention()).queue();
        } else {
            DiscordUtils.selectList(event, channels, ch -> String.format("%s (ID: %s)", ch.getName(), ch.getId()), s -> ((SimpleCommand) optsCmd).baseEmbed(event, "Select the Channel:").setDescription(s).build(), ch -> {
                guildData.getLogExcludedChannels().add(ch.getId());
                dbGuild.saveAsync();
                event.getChannel().sendMessage(EmoteReference.OK + "Added logs exception on channel: " + ch.getAsMention()).queue();
            });
        }
    });
    // endregion
    // region disable
    registerOption("logs:disable", "Disable logs", "Disables logs.\n" + "**Example:** `~>opts logs disable`", "Disables logs.", (event) -> {
        DBGuild dbGuild = MantaroData.db().getGuild(event.getGuild());
        GuildData guildData = dbGuild.getData();
        guildData.setGuildLogChannel(null);
        dbGuild.saveAsync();
        event.getChannel().sendMessage(EmoteReference.MEGA + "Message logging has been disabled.").queue();
    });
// endregion
// endregion
}
Also used : TextChannel(net.dv8tion.jda.core.entities.TextChannel) Option(net.kodehawa.mantarobot.options.annotations.Option) Utils(net.kodehawa.mantarobot.utils.Utils) DiscordUtils(net.kodehawa.mantarobot.utils.DiscordUtils) DBGuild(net.kodehawa.mantarobot.db.entities.DBGuild) Collectors(java.util.stream.Collectors) Consumer(java.util.function.Consumer) Guild(net.dv8tion.jda.core.entities.Guild) List(java.util.List) User(net.dv8tion.jda.core.entities.User) CommandPermission(net.kodehawa.mantarobot.core.modules.commands.base.CommandPermission) ISnowflake(net.dv8tion.jda.core.entities.ISnowflake) GuildData(net.kodehawa.mantarobot.db.entities.helpers.GuildData) EmoteReference(net.kodehawa.mantarobot.utils.commands.EmoteReference) MantaroData(net.kodehawa.mantarobot.data.MantaroData) Subscribe(com.google.common.eventbus.Subscribe) SimpleCommand(net.kodehawa.mantarobot.core.modules.commands.SimpleCommand) OptionType(net.kodehawa.mantarobot.options.core.OptionType) OptionRegistryEvent(net.kodehawa.mantarobot.options.event.OptionRegistryEvent) OptionHandler(net.kodehawa.mantarobot.options.core.OptionHandler) OptsCmd.optsCmd(net.kodehawa.mantarobot.commands.OptsCmd.optsCmd) GuildData(net.kodehawa.mantarobot.db.entities.helpers.GuildData) TextChannel(net.dv8tion.jda.core.entities.TextChannel) User(net.dv8tion.jda.core.entities.User) DBGuild(net.kodehawa.mantarobot.db.entities.DBGuild) SimpleCommand(net.kodehawa.mantarobot.core.modules.commands.SimpleCommand) DBGuild(net.kodehawa.mantarobot.db.entities.DBGuild) Guild(net.dv8tion.jda.core.entities.Guild) Subscribe(com.google.common.eventbus.Subscribe)

Example 17 with DBGuild

use of net.kodehawa.mantarobot.db.entities.DBGuild in project MantaroBot by Mantaro.

the class MusicOptions method onRegistry.

@Subscribe
public void onRegistry(OptionRegistryEvent e) {
    registerOption("fairqueue:max", "Fair queue maximum", "Sets the maximum fairqueue value (max amount of the same song any user can add).\n" + "Example: `~>opts fairqueue max 5`", "Sets the maximum fairqueue value.", (event, args) -> {
        DBGuild dbGuild = MantaroData.db().getGuild(event.getGuild());
        GuildData guildData = dbGuild.getData();
        if (args.length == 0) {
            event.getChannel().sendMessage(EmoteReference.ERROR + "You need to specify a positive integer.").queue();
            return;
        }
        String much = args[0];
        final int fq;
        try {
            fq = Integer.parseInt(much);
        } catch (Exception ex) {
            event.getChannel().sendMessage(EmoteReference.ERROR + "Not a valid number").queue();
            return;
        }
        guildData.setMaxFairQueue(fq);
        dbGuild.save();
        event.getChannel().sendMessage(EmoteReference.CORRECT + "Set max fair queue size to " + fq).queue();
    });
    registerOption("musicannounce:toggle", "Music announce toggle", "Toggles whether the bot will announce the new song playing or no.", event -> {
        DBGuild dbGuild = MantaroData.db().getGuild(event.getGuild());
        GuildData guildData = dbGuild.getData();
        boolean t1 = guildData.isMusicAnnounce();
        guildData.setMusicAnnounce(!t1);
        event.getChannel().sendMessage(EmoteReference.CORRECT + "Set music announce to " + "**" + !t1 + "**").queue();
        dbGuild.save();
    });
    registerOption("music:channel", "Music VC lock", "Locks the bot to a VC. You need the VC name.\n" + "Example: `~>opts music channel Music`", "Locks the music feature to the specified VC.", (event, args) -> {
        if (args.length == 0) {
            OptsCmd.onHelp(event);
            return;
        }
        String channelName = String.join(" ", args);
        DBGuild dbGuild = MantaroData.db().getGuild(event.getGuild());
        GuildData guildData = dbGuild.getData();
        Consumer<VoiceChannel> consumer = voiceChannel -> {
            guildData.setMusicChannel(voiceChannel.getId());
            dbGuild.save();
            event.getChannel().sendMessage(EmoteReference.OK + "Music Channel set to: " + voiceChannel.getName()).queue();
        };
        VoiceChannel channel = Utils.findVoiceChannelSelect(event, channelName, consumer);
        if (channel != null) {
            consumer.accept(channel);
        }
    });
    registerOption("music:queuelimit", "Music queue limit", "Sets a custom queue limit.\n" + "Example: `~>opts music queuelimit 90`", "Sets a custom queue limit.", (event, args) -> {
        if (args.length == 0) {
            OptsCmd.onHelp(event);
            return;
        }
        boolean isNumber = args[0].matches("^[0-9]*$");
        if (!isNumber) {
            event.getChannel().sendMessage(EmoteReference.ERROR + "That's not a valid number!").queue();
            return;
        }
        DBGuild dbGuild = MantaroData.db().getGuild(event.getGuild());
        GuildData guildData = dbGuild.getData();
        try {
            int finalSize = Integer.parseInt(args[0]);
            int applySize = finalSize >= 300 ? 300 : finalSize;
            guildData.setMusicQueueSizeLimit((long) applySize);
            dbGuild.save();
            event.getChannel().sendMessage(String.format(EmoteReference.MEGA + "The queue limit on this server is now " + "**%d** songs.", applySize)).queue();
        } catch (NumberFormatException ex) {
            event.getChannel().sendMessage(EmoteReference.ERROR + "You're trying to set too high of a number (which won't" + " be applied anyway), silly").queue();
        }
    });
    registerOption("music:clearchannel", "Music channel clear", "Clears the specific music channel.", (event) -> {
        DBGuild dbGuild = MantaroData.db().getGuild(event.getGuild());
        GuildData guildData = dbGuild.getData();
        guildData.setMusicChannel(null);
        dbGuild.save();
        event.getChannel().sendMessage(EmoteReference.CORRECT + "I can play music on all channels now").queue();
    });
}
Also used : VoiceChannel(net.dv8tion.jda.core.entities.VoiceChannel) Option(net.kodehawa.mantarobot.options.annotations.Option) Utils(net.kodehawa.mantarobot.utils.Utils) DBGuild(net.kodehawa.mantarobot.db.entities.DBGuild) Consumer(java.util.function.Consumer) OptsCmd(net.kodehawa.mantarobot.commands.OptsCmd) Slf4j(lombok.extern.slf4j.Slf4j) GuildData(net.kodehawa.mantarobot.db.entities.helpers.GuildData) EmoteReference(net.kodehawa.mantarobot.utils.commands.EmoteReference) MantaroData(net.kodehawa.mantarobot.data.MantaroData) Subscribe(com.google.common.eventbus.Subscribe) OptionType(net.kodehawa.mantarobot.options.core.OptionType) OptionRegistryEvent(net.kodehawa.mantarobot.options.event.OptionRegistryEvent) OptionHandler(net.kodehawa.mantarobot.options.core.OptionHandler) GuildData(net.kodehawa.mantarobot.db.entities.helpers.GuildData) DBGuild(net.kodehawa.mantarobot.db.entities.DBGuild) VoiceChannel(net.dv8tion.jda.core.entities.VoiceChannel) Subscribe(com.google.common.eventbus.Subscribe)

Example 18 with DBGuild

use of net.kodehawa.mantarobot.db.entities.DBGuild in project MantaroBot by Mantaro.

the class GameLobby method startFirstGame.

public void startFirstGame() {
    LOBBYS.put(event.getChannel(), this);
    if (gamesToPlay.getFirst().onStart(this)) {
        gamesToPlay.getFirst().call(this, players);
        DBGuild dbGuild = MantaroData.db().getGuild(guild);
        dbGuild.getData().setGameTimeoutExpectedAt(String.valueOf(System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(75)));
        dbGuild.saveAsync();
    } else {
        LOBBYS.remove(getChannel());
        gamesToPlay.clear();
    }
}
Also used : DBGuild(net.kodehawa.mantarobot.db.entities.DBGuild)

Example 19 with DBGuild

use of net.kodehawa.mantarobot.db.entities.DBGuild in project MantaroBot by Mantaro.

the class ImageboardUtils method getImage.

public static void getImage(ImageBoard<?> api, ImageRequestType type, boolean nsfwOnly, String imageboard, String[] args, String content, GuildMessageReceivedEvent event) {
    Rating rating = Rating.SAFE;
    boolean needRating = args.length >= 3;
    final TextChannel channel = event.getChannel();
    final Player player = MantaroData.db().getPlayer(event.getAuthor());
    final PlayerData playerData = player.getData();
    if (needRating && !nsfwOnly)
        rating = Rating.lookupFromString(args[2]);
    if (nsfwOnly)
        rating = Rating.EXPLICIT;
    if (rating == null) {
        channel.sendMessage(EmoteReference.ERROR + "You provided an invalid rating (Available types: questionable, explicit, safe)!").queue();
        return;
    }
    final Rating finalRating = rating;
    if (!nsfwCheck(event, nsfwOnly, false, finalRating)) {
        channel.sendMessage(EmoteReference.ERROR + "Cannot send a NSFW image in a non-nsfw channel :(").queue();
        return;
    }
    int page = Math.max(1, r.nextInt(25));
    String queryRating = nsfwOnly ? null : rating.getLongName();
    switch(type) {
        case GET:
            try {
                String arguments = content.replace("get ", "");
                String[] argumentsSplit = arguments.split(" ");
                api.get(page, queryRating).async(requestedImages -> {
                    if (isListNull(requestedImages, event))
                        return;
                    try {
                        int number;
                        List<BoardImage> images = (List<BoardImage>) requestedImages;
                        if (!nsfwOnly)
                            images = requestedImages.stream().filter(data -> data.getRating().equals(finalRating)).collect(Collectors.toList());
                        if (images.isEmpty()) {
                            channel.sendMessage(EmoteReference.SAD + "There are no images matching your search criteria...").queue();
                            return;
                        }
                        try {
                            number = Integer.parseInt(argumentsSplit[0]);
                        } catch (Exception e) {
                            number = r.nextInt(images.size());
                        }
                        BoardImage image = images.get(number);
                        String tags = image.getTags().stream().collect(Collectors.joining(", "));
                        if (foundMinorTags(event, tags, image.getRating())) {
                            return;
                        }
                        imageEmbed(image.getURL(), String.valueOf(image.getWidth()), String.valueOf(image.getHeight()), tags, image.getRating(), imageboard, channel);
                        if (image.getRating().equals(Rating.EXPLICIT)) {
                            if (playerData.addBadgeIfAbsent(Badge.LEWDIE)) {
                                player.saveAsync();
                            }
                            TextChannelGround.of(event).dropItemWithChance(13, 3);
                        }
                    } catch (Exception e) {
                        event.getChannel().sendMessage(EmoteReference.ERROR + "**There aren't any more images or no results found**! Please try with a lower " + "number or another search.").queue();
                    }
                }, failure -> event.getChannel().sendMessage(EmoteReference.SAD + "There was an error while looking for an image...").queue());
            } catch (NumberFormatException ne) {
                channel.sendMessage(EmoteReference.ERROR + "Wrong argument type. Check ~>help " + imageboard).queue(message -> message.delete().queueAfter(10, TimeUnit.SECONDS));
            } catch (Exception e) {
                event.getChannel().sendMessage(EmoteReference.SAD + "There was an error while looking an image...").queue();
            }
            break;
        case TAGS:
            try {
                String sNoArgs = content.replace("tags ", "");
                String[] arguments = sNoArgs.split(" ");
                String tags = arguments[0];
                DBGuild dbGuild = MantaroData.db().getGuild(event.getGuild());
                if (dbGuild.getData().getBlackListedImageTags().contains(tags.toLowerCase())) {
                    event.getChannel().sendMessage(EmoteReference.ERROR + "This image tag has been blacklisted here by an administrator.").queue();
                    return;
                }
                api.search(tags, queryRating).async(requestedImages -> {
                    // account for this
                    if (isListNull(requestedImages, event))
                        return;
                    try {
                        List<BoardImage> filter = (List<BoardImage>) requestedImages;
                        if (!nsfwOnly)
                            filter = requestedImages.stream().filter(data -> data.getRating().equals(finalRating)).collect(Collectors.toList());
                        if (filter.isEmpty()) {
                            channel.sendMessage(EmoteReference.SAD + "There are no images matching your search criteria...").queue();
                            return;
                        }
                        int number;
                        try {
                            number = Integer.parseInt(arguments[1]);
                        } catch (Exception e) {
                            number = r.nextInt(filter.size() > 0 ? filter.size() - 1 : filter.size());
                        }
                        BoardImage image = filter.get(number);
                        String imageTags = image.getTags().stream().collect(Collectors.joining(", "));
                        if (foundMinorTags(event, imageTags, image.getRating())) {
                            return;
                        }
                        imageEmbed(image.getURL(), String.valueOf(image.getWidth()), String.valueOf(image.getHeight()), imageTags, image.getRating(), imageboard, channel);
                        if (image.getRating().equals(Rating.EXPLICIT)) {
                            if (playerData.addBadgeIfAbsent(Badge.LEWDIE)) {
                                player.saveAsync();
                            }
                            TextChannelGround.of(event).dropItemWithChance(13, 3);
                        }
                    } catch (Exception e) {
                        event.getChannel().sendMessage(EmoteReference.ERROR + "**There aren't any more images or no results found**! Please try with a lower " + "number or another search.").queue();
                    }
                }, failure -> event.getChannel().sendMessage(EmoteReference.SAD + "There was an error while looking for this tag...").queue());
            } catch (NumberFormatException numberEx) {
                channel.sendMessage(EmoteReference.ERROR + "Wrong argument type. Check ~>help " + imageboard).queue(message -> message.delete().queueAfter(10, TimeUnit.SECONDS));
            } catch (Exception exception) {
                event.getChannel().sendMessage(EmoteReference.SAD + "There was an error while looking for this tag...").queue();
            }
            break;
        case RANDOM:
            api.get(page, queryRating).async(requestedImages -> {
                try {
                    if (isListNull(requestedImages, event))
                        return;
                    List<BoardImage> filter = (List<BoardImage>) requestedImages;
                    if (!nsfwOnly)
                        filter = requestedImages.stream().filter(data -> data.getRating().equals(finalRating)).collect(Collectors.toList());
                    if (filter.isEmpty()) {
                        channel.sendMessage(EmoteReference.SAD + "There are no images matching your search criteria...").queue();
                        return;
                    }
                    int number = r.nextInt(filter.size());
                    BoardImage image = filter.get(number);
                    String tags = image.getTags().stream().collect(Collectors.joining(", "));
                    imageEmbed(image.getURL(), String.valueOf(image.getWidth()), String.valueOf(image.getHeight()), tags, image.getRating(), imageboard, channel);
                    if (image.getRating().equals(Rating.EXPLICIT)) {
                        if (playerData.addBadgeIfAbsent(Badge.LEWDIE)) {
                            player.saveAsync();
                        }
                        TextChannelGround.of(event).dropItemWithChance(13, 3);
                    }
                } catch (Exception e) {
                    event.getChannel().sendMessage(EmoteReference.SAD + "There was an unknown error while looking for a random image...").queue();
                }
            }, failure -> event.getChannel().sendMessage(EmoteReference.SAD + "There was an error while looking for a random image...").queue());
            break;
    }
}
Also used : Badge(net.kodehawa.mantarobot.commands.currency.profile.Badge) Player(net.kodehawa.mantarobot.db.entities.Player) PlayerData(net.kodehawa.mantarobot.db.entities.helpers.PlayerData) TextChannel(net.dv8tion.jda.core.entities.TextChannel) Random(java.util.Random) DBGuild(net.kodehawa.mantarobot.db.entities.DBGuild) Collectors(java.util.stream.Collectors) EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) TimeUnit(java.util.concurrent.TimeUnit) ImageBoard(net.kodehawa.lib.imageboards.ImageBoard) GuildMessageReceivedEvent(net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent) List(java.util.List) Rating(net.kodehawa.lib.imageboards.entities.Rating) EmoteReference(net.kodehawa.mantarobot.utils.commands.EmoteReference) BoardImage(net.kodehawa.lib.imageboards.entities.BoardImage) MantaroData(net.kodehawa.mantarobot.data.MantaroData) TextChannelGround(net.kodehawa.mantarobot.commands.currency.TextChannelGround) Player(net.kodehawa.mantarobot.db.entities.Player) BoardImage(net.kodehawa.lib.imageboards.entities.BoardImage) Rating(net.kodehawa.lib.imageboards.entities.Rating) TextChannel(net.dv8tion.jda.core.entities.TextChannel) DBGuild(net.kodehawa.mantarobot.db.entities.DBGuild) List(java.util.List) PlayerData(net.kodehawa.mantarobot.db.entities.helpers.PlayerData)

Example 20 with DBGuild

use of net.kodehawa.mantarobot.db.entities.DBGuild in project MantaroBot by Mantaro.

the class MuteTask method handle.

public void handle() {
    try {
        MantaroObj data = MantaroData.db().getMantaroData();
        Map<Long, Pair<String, Long>> mutes = data.getMutes();
        log.debug("Checking mutes... data size {}", mutes.size());
        for (Map.Entry<Long, Pair<String, Long>> entry : mutes.entrySet()) {
            try {
                log.trace("Iteration");
                Long id = entry.getKey();
                Pair<String, Long> pair = entry.getValue();
                String guildId = pair.getLeft();
                long maxTime = pair.getRight();
                if (MantaroBot.getInstance().getShardForGuild(guildId) == null) {
                    continue;
                }
                Guild guild = MantaroBot.getInstance().getGuildById(guildId);
                DBGuild dbGuild = MantaroData.db().getGuild(guildId);
                GuildData guildData = dbGuild.getData();
                if (guild == null) {
                    data.getMutes().remove(id);
                    data.saveAsync();
                    log.debug("Removed {} because guild == null", id);
                    continue;
                } else if (guild.getMemberById(id) == null) {
                    data.getMutes().remove(id);
                    data.saveAsync();
                    log.debug("Removed {} because member == null", id);
                    continue;
                }
                // Please hold me.
                if (guild.getRoleById(guildData.getMutedRole()) == null) {
                    data.getMutes().remove(id);
                    data.saveAsync();
                    log.debug("Removed {} because role == null", id);
                } else {
                    if (System.currentTimeMillis() > maxTime) {
                        log.debug("Unmuted {} because time ran out", id);
                        data.getMutes().remove(id);
                        data.save();
                        guild.getController().removeRolesFromMember(guild.getMemberById(id), guild.getRoleById(guildData.getMutedRole())).queue();
                        guildData.setCases(guildData.getCases() + 1);
                        dbGuild.saveAsync();
                        ModLog.log(guild.getSelfMember(), MantaroBot.getInstance().getUserById(id), "Mute timeout expired", ModLog.ModAction.UNMUTE, guildData.getCases());
                    }
                }
            } catch (Exception ignored) {
            }
        }
    } catch (Exception ignored) {
    }
}
Also used : GuildData(net.kodehawa.mantarobot.db.entities.helpers.GuildData) MantaroObj(net.kodehawa.mantarobot.db.entities.MantaroObj) Guild(net.dv8tion.jda.core.entities.Guild) DBGuild(net.kodehawa.mantarobot.db.entities.DBGuild) DBGuild(net.kodehawa.mantarobot.db.entities.DBGuild) Map(java.util.Map) Pair(net.kodehawa.mantarobot.utils.Pair)

Aggregations

DBGuild (net.kodehawa.mantarobot.db.entities.DBGuild)30 MantaroData (net.kodehawa.mantarobot.data.MantaroData)18 GuildData (net.kodehawa.mantarobot.db.entities.helpers.GuildData)18 EmoteReference (net.kodehawa.mantarobot.utils.commands.EmoteReference)18 Utils (net.kodehawa.mantarobot.utils.Utils)15 Subscribe (com.google.common.eventbus.Subscribe)14 GuildMessageReceivedEvent (net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent)14 List (java.util.List)12 EmbedBuilder (net.dv8tion.jda.core.EmbedBuilder)11 SimpleCommand (net.kodehawa.mantarobot.core.modules.commands.SimpleCommand)11 Slf4j (lombok.extern.slf4j.Slf4j)10 Category (net.kodehawa.mantarobot.core.modules.commands.base.Category)10 Permission (net.dv8tion.jda.core.Permission)9 PermissionException (net.dv8tion.jda.core.exceptions.PermissionException)9 DiscordUtils (net.kodehawa.mantarobot.utils.DiscordUtils)9 Collectors (java.util.stream.Collectors)8 MantaroBot (net.kodehawa.mantarobot.MantaroBot)8 CommandRegistry (net.kodehawa.mantarobot.core.CommandRegistry)8 Module (net.kodehawa.mantarobot.core.modules.Module)8 TimeUnit (java.util.concurrent.TimeUnit)7