Search in sources :

Example 6 with DBGuild

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

the class ModerationCmds method onPostLoad.

@Command
public static void onPostLoad(PostLoadEvent e) {
    OptsCmd.registerOption("modlog:blacklist", event -> {
        List<User> mentioned = event.getMessage().getMentionedUsers();
        if (mentioned.isEmpty()) {
            event.getChannel().sendMessage(EmoteReference.ERROR + "**You need to specify the users to locally blacklist from mod logs.**").queue();
            return;
        }
        DBGuild dbGuild = MantaroData.db().getGuild(event.getGuild());
        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.getModlogBlacklistedPeople().addAll(toBlackList);
        dbGuild.save();
        event.getChannel().sendMessage(EmoteReference.CORRECT + "Locally blacklisted users from mod-log: **" + blacklisted + "**").queue();
    });
    OptsCmd.registerOption("modlog:whitelist", event -> {
        List<User> mentioned = event.getMessage().getMentionedUsers();
        if (mentioned.isEmpty()) {
            event.getChannel().sendMessage(EmoteReference.ERROR + "**You need to specify the users to locally un-blacklist from mod logs.**").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.getModlogBlacklistedPeople().removeAll(toUnBlacklist);
        dbGuild.save();
        event.getChannel().sendMessage(EmoteReference.CORRECT + "Locally un-blacklisted users from mod-log: **" + unBlacklisted + "**").queue();
    });
    OptsCmd.registerOption("linkprotection:toggle", event -> {
        DBGuild dbGuild = MantaroData.db().getGuild(event.getGuild());
        GuildData guildData = dbGuild.getData();
        boolean toggler = guildData.isLinkProtection();
        guildData.setLinkProtection(!toggler);
        event.getChannel().sendMessage(EmoteReference.CORRECT + "Set link protection to " + "`" + !toggler + "`").queue();
        dbGuild.save();
    });
    OptsCmd.registerOption("slowmode:toggle", event -> {
        DBGuild dbGuild = MantaroData.db().getGuild(event.getGuild());
        GuildData guildData = dbGuild.getData();
        boolean toggler = guildData.isSlowMode();
        guildData.setSlowMode(!toggler);
        event.getChannel().sendMessage(EmoteReference.CORRECT + "Set slowmode chat to " + "`" + !toggler + "`").queue();
        dbGuild.save();
    });
    OptsCmd.registerOption("antispam:toggle", event -> {
        DBGuild dbGuild = MantaroData.db().getGuild(event.getGuild());
        GuildData guildData = dbGuild.getData();
        boolean toggler = guildData.isAntiSpam();
        guildData.setAntiSpam(!toggler);
        event.getChannel().sendMessage(EmoteReference.CORRECT + "Set anti-spam chat mode to " + "`" + !toggler + "`").queue();
        dbGuild.save();
    });
    OptsCmd.registerOption("linkprotection:channel:allow", (event, args) -> {
        if (args.length == 0) {
            OptsCmd.onHelp(event);
            return;
        }
        DBGuild dbGuild = MantaroData.db().getGuild(event.getGuild());
        GuildData guildData = dbGuild.getData();
        String channelName = args[0];
        List<TextChannel> textChannels = event.getGuild().getTextChannels().stream().filter(textChannel -> textChannel.getName().contains(channelName)).collect(Collectors.toList());
        if (textChannels.isEmpty()) {
            event.getChannel().sendMessage(EmoteReference.ERROR + "There were no channels matching your search.").queue();
        }
        if (textChannels.size() <= 1) {
            guildData.getLinkProtectionAllowedChannels().add(textChannels.get(0).getId());
            dbGuild.save();
            event.getChannel().sendMessage(EmoteReference.CORRECT + textChannels.get(0).getAsMention() + " can now be used to post discord invites.").queue();
            return;
        }
        DiscordUtils.selectList(event, textChannels, textChannel -> String.format("%s (ID: %s)", textChannel.getName(), textChannel.getId()), s -> OptsCmd.getOpts().baseEmbed(event, "Select the Channel:").setDescription(s).build(), textChannel -> {
            guildData.getLinkProtectionAllowedChannels().add(textChannel.getId());
            dbGuild.save();
            event.getChannel().sendMessage(EmoteReference.OK + textChannel.getAsMention() + " can now be used to send discord invites.").queue();
        });
    });
    OptsCmd.registerOption("linkprotection:channel:disallow", (event, args) -> {
        if (args.length == 0) {
            OptsCmd.onHelp(event);
            return;
        }
        DBGuild dbGuild = MantaroData.db().getGuild(event.getGuild());
        GuildData guildData = dbGuild.getData();
        String channelName = args[0];
        List<TextChannel> textChannels = event.getGuild().getTextChannels().stream().filter(textChannel -> textChannel.getName().contains(channelName)).collect(Collectors.toList());
        if (textChannels.isEmpty()) {
            event.getChannel().sendMessage(EmoteReference.ERROR + "There were no channels matching your search.").queue();
        }
        if (textChannels.size() <= 1) {
            guildData.getLinkProtectionAllowedChannels().remove(textChannels.get(0).getId());
            dbGuild.save();
            event.getChannel().sendMessage(EmoteReference.CORRECT + textChannels.get(0).getAsMention() + " cannot longer be used to post discord invites.").queue();
            return;
        }
        DiscordUtils.selectList(event, textChannels, textChannel -> String.format("%s (ID: %s)", textChannel.getName(), textChannel.getId()), s -> OptsCmd.getOpts().baseEmbed(event, "Select the Channel:").setDescription(s).build(), textChannel -> {
            guildData.getLinkProtectionAllowedChannels().remove(textChannel.getId());
            dbGuild.save();
            event.getChannel().sendMessage(EmoteReference.OK + textChannel.getAsMention() + " cannot longer be used to send discord invites.").queue();
        });
    });
}
Also used : SimpleCommand(net.kodehawa.mantarobot.modules.commands.SimpleCommand) StringUtils(net.kodehawa.mantarobot.utils.StringUtils) Utils(net.kodehawa.mantarobot.utils.Utils) Module(net.kodehawa.mantarobot.modules.Module) DiscordUtils(net.kodehawa.mantarobot.utils.DiscordUtils) AudioCmdUtils(net.kodehawa.mantarobot.commands.music.AudioCmdUtils) MantaroBot(net.kodehawa.mantarobot.MantaroBot) PermissionException(net.dv8tion.jda.core.exceptions.PermissionException) GuildMessageReceivedEvent(net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent) Permission(net.dv8tion.jda.core.Permission) DBGuild(net.kodehawa.mantarobot.data.entities.DBGuild) Map(java.util.Map) ManagedDatabase(net.kodehawa.mantarobot.data.db.ManagedDatabase) CommandRegistry(net.kodehawa.mantarobot.modules.CommandRegistry) Command(net.kodehawa.mantarobot.modules.Command) TextChannelGround(net.kodehawa.mantarobot.commands.currency.TextChannelGround) PostLoadEvent(net.kodehawa.mantarobot.modules.events.PostLoadEvent) net.dv8tion.jda.core.entities(net.dv8tion.jda.core.entities) Category(net.kodehawa.mantarobot.modules.commands.base.Category) Collectors(java.util.stream.Collectors) Slf4j(lombok.extern.slf4j.Slf4j) ModLog(net.kodehawa.mantarobot.commands.moderation.ModLog) List(java.util.List) OffsetDateTime(java.time.OffsetDateTime) GuildData(net.kodehawa.mantarobot.data.entities.helpers.GuildData) EmoteReference(net.kodehawa.mantarobot.utils.commands.EmoteReference) CommandPermission(net.kodehawa.mantarobot.modules.commands.CommandPermission) MantaroData(net.kodehawa.mantarobot.data.MantaroData) Optional(java.util.Optional) Queue(java.util.Queue) GuildData(net.kodehawa.mantarobot.data.entities.helpers.GuildData) DBGuild(net.kodehawa.mantarobot.data.entities.DBGuild) SimpleCommand(net.kodehawa.mantarobot.modules.commands.SimpleCommand) Command(net.kodehawa.mantarobot.modules.Command)

Example 7 with DBGuild

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

the class ModerationCmds method tempban.

@Command
public static void tempban(CommandRegistry cr) {
    cr.register("tempban", new SimpleCommand(Category.MODERATION) {

        @Override
        protected void call(GuildMessageReceivedEvent event, String content, String[] args) {
            String reason = content;
            Guild guild = event.getGuild();
            User author = event.getAuthor();
            TextChannel channel = event.getChannel();
            Message receivedMessage = event.getMessage();
            if (!guild.getMember(author).hasPermission(net.dv8tion.jda.core.Permission.BAN_MEMBERS)) {
                channel.sendMessage(EmoteReference.ERROR + "Cannot ban: You have no Ban Members permission.").queue();
                return;
            }
            if (event.getMessage().getMentionedUsers().isEmpty()) {
                event.getChannel().sendMessage(EmoteReference.ERROR + "You need to mention an user!").queue();
                return;
            }
            for (User user : event.getMessage().getMentionedUsers()) {
                reason = reason.replaceAll("(\\s+)?<@!?" + user.getId() + ">(\\s+)?", "");
            }
            int index = reason.indexOf("time:");
            if (index < 0) {
                event.getChannel().sendMessage(EmoteReference.ERROR + "You cannot temp ban an user without giving me the time!").queue();
                return;
            }
            String time = reason.substring(index);
            reason = reason.replace(time, "").trim();
            time = time.replaceAll("time:(\\s+)?", "");
            if (reason.isEmpty()) {
                event.getChannel().sendMessage(EmoteReference.ERROR + "You cannot temp ban someone without a reason.!").queue();
                return;
            }
            if (time.isEmpty()) {
                event.getChannel().sendMessage(EmoteReference.ERROR + "You cannot temp ban someone without giving me the time!").queue();
                return;
            }
            final DBGuild db = MantaroData.db().getGuild(event.getGuild());
            long l = AudioCmdUtils.parseTime(time);
            String finalReason = reason;
            String sTime = StringUtils.parseTime(l);
            receivedMessage.getMentionedUsers().forEach(user -> guild.getController().ban(user, 7).queue(success -> {
                user.openPrivateChannel().complete().sendMessage(EmoteReference.MEGA + "You were **temporarly banned** by " + event.getAuthor().getName() + "#" + event.getAuthor().getDiscriminator() + " with reason: " + finalReason + ".").queue();
                db.getData().setCases(db.getData().getCases() + 1);
                db.saveAsync();
                channel.sendMessage(EmoteReference.ZAP + "You will be missed... or not " + user.getName()).queue();
                ModLog.log(event.getMember(), user, finalReason, ModLog.ModAction.TEMP_BAN, db.getData().getCases(), sTime);
                MantaroBot.getTempBanManager().addTempban(guild.getId() + ":" + user.getId(), l + System.currentTimeMillis());
                TextChannelGround.of(event).dropItemWithChance(1, 2);
            }, error -> {
                if (error instanceof PermissionException) {
                    channel.sendMessage(EmoteReference.ERROR + "Error banning " + user.getName() + ": " + "(I need the permission " + ((PermissionException) error).getPermission() + ")").queue();
                } else {
                    channel.sendMessage(EmoteReference.ERROR + "I encountered an unknown error while banning " + user.getName() + ": " + "<" + error.getClass().getSimpleName() + ">: " + error.getMessage()).queue();
                    log.warn("Encountered an unexpected error while trying to ban someone.", error);
                }
            }));
        }

        @Override
        public MessageEmbed help(GuildMessageReceivedEvent event) {
            return helpEmbed(event, "Tempban Command").setDescription("**Temporarily bans an user**").addField("Usage", "`~>tempban <user> <reason> time:<time>`", false).addField("Example", "`~>tempban @Kodehawa example time:1d`", false).addField("Extended usage", "`time` - can be used with the following parameters: " + "d (days), s (second), m (minutes), h (hour). **For example time:1d1h will give a day and an hour.**", false).build();
        }
    });
}
Also used : SimpleCommand(net.kodehawa.mantarobot.modules.commands.SimpleCommand) StringUtils(net.kodehawa.mantarobot.utils.StringUtils) Utils(net.kodehawa.mantarobot.utils.Utils) Module(net.kodehawa.mantarobot.modules.Module) DiscordUtils(net.kodehawa.mantarobot.utils.DiscordUtils) AudioCmdUtils(net.kodehawa.mantarobot.commands.music.AudioCmdUtils) MantaroBot(net.kodehawa.mantarobot.MantaroBot) PermissionException(net.dv8tion.jda.core.exceptions.PermissionException) GuildMessageReceivedEvent(net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent) Permission(net.dv8tion.jda.core.Permission) DBGuild(net.kodehawa.mantarobot.data.entities.DBGuild) Map(java.util.Map) ManagedDatabase(net.kodehawa.mantarobot.data.db.ManagedDatabase) CommandRegistry(net.kodehawa.mantarobot.modules.CommandRegistry) Command(net.kodehawa.mantarobot.modules.Command) TextChannelGround(net.kodehawa.mantarobot.commands.currency.TextChannelGround) PostLoadEvent(net.kodehawa.mantarobot.modules.events.PostLoadEvent) net.dv8tion.jda.core.entities(net.dv8tion.jda.core.entities) Category(net.kodehawa.mantarobot.modules.commands.base.Category) Collectors(java.util.stream.Collectors) Slf4j(lombok.extern.slf4j.Slf4j) ModLog(net.kodehawa.mantarobot.commands.moderation.ModLog) List(java.util.List) OffsetDateTime(java.time.OffsetDateTime) GuildData(net.kodehawa.mantarobot.data.entities.helpers.GuildData) EmoteReference(net.kodehawa.mantarobot.utils.commands.EmoteReference) CommandPermission(net.kodehawa.mantarobot.modules.commands.CommandPermission) MantaroData(net.kodehawa.mantarobot.data.MantaroData) Optional(java.util.Optional) Queue(java.util.Queue) PermissionException(net.dv8tion.jda.core.exceptions.PermissionException) DBGuild(net.kodehawa.mantarobot.data.entities.DBGuild) DBGuild(net.kodehawa.mantarobot.data.entities.DBGuild) SimpleCommand(net.kodehawa.mantarobot.modules.commands.SimpleCommand) GuildMessageReceivedEvent(net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent) SimpleCommand(net.kodehawa.mantarobot.modules.commands.SimpleCommand) Command(net.kodehawa.mantarobot.modules.Command)

Example 8 with DBGuild

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

the class UtilsCmds method dateGMT.

static String dateGMT(Guild guild, String tz) {
    DateFormat format = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss");
    DBGuild dbGuild = MantaroData.db().getGuild(guild.getId());
    GuildData guildData = dbGuild.getData();
    if (guildData.getTimeDisplay() == 1) {
        format = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss a");
    }
    System.out.println(TimeZone.getTimeZone(tz));
    return format.format(new Date());
}
Also used : GuildData(net.kodehawa.mantarobot.data.entities.helpers.GuildData) DBGuild(net.kodehawa.mantarobot.data.entities.DBGuild) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) SimpleDateFormat(java.text.SimpleDateFormat)

Example 9 with DBGuild

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

the class UtilsCmds method onPostLoad.

@Command
public static void onPostLoad(PostLoadEvent e) {
    OptsCmd.registerOption("birthday:enable", (event, args) -> {
        if (args.length < 2) {
            OptsCmd.onHelp(event);
            return;
        }
        DBGuild dbGuild = MantaroData.db().getGuild(event.getGuild());
        GuildData guildData = dbGuild.getData();
        try {
            String channel = args[0];
            String role = args[1];
            boolean isId = channel.matches("^[0-9]*$");
            String channelId = isId ? channel : event.getGuild().getTextChannelsByName(channel, true).get(0).getId();
            String roleId = event.getGuild().getRolesByName(role.replace(channelId, ""), true).get(0).getId();
            guildData.setBirthdayChannel(channelId);
            guildData.setBirthdayRole(roleId);
            dbGuild.save();
            event.getChannel().sendMessage(String.format(EmoteReference.MEGA + "Birthday logging enabled on this server with parameters -> " + "Channel: ``#%s (%s)`` and role: ``%s (%s)``", channel, channelId, role, roleId)).queue();
        } catch (Exception ex) {
            if (ex instanceof IndexOutOfBoundsException) {
                event.getChannel().sendMessage(EmoteReference.ERROR + "I didn't find a channel or role!\n " + "**Remember, you don't have to mention neither the role or the channel, rather just type its " + "name, order is <channel> <role>, without the leading \"<>\".**").queue();
                return;
            }
            event.getChannel().sendMessage(EmoteReference.ERROR + "You supplied invalid arguments for this command " + EmoteReference.SAD).queue();
            OptsCmd.onHelp(event);
        }
    });
    OptsCmd.registerOption("birthday:disable", (event) -> {
        DBGuild dbGuild = MantaroData.db().getGuild(event.getGuild());
        GuildData guildData = dbGuild.getData();
        guildData.setBirthdayChannel(null);
        guildData.setBirthdayRole(null);
        dbGuild.save();
        event.getChannel().sendMessage(EmoteReference.MEGA + "Birthday logging has been disabled on this server").queue();
    });
}
Also used : GuildData(net.kodehawa.mantarobot.data.entities.helpers.GuildData) DBGuild(net.kodehawa.mantarobot.data.entities.DBGuild) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) SimpleCommand(net.kodehawa.mantarobot.modules.commands.SimpleCommand) Command(net.kodehawa.mantarobot.modules.Command)

Example 10 with DBGuild

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

the class ImageActionCmd method call.

@Override
protected void call(GuildMessageReceivedEvent event, String content) {
    String random = random(images);
    try {
        if (mentions(event).isEmpty()) {
            event.getChannel().sendMessage(EmoteReference.ERROR + "You need to mention a user").queue();
            return;
        }
        DBGuild dbGuild = MantaroData.db().getGuild(event.getGuild());
        GuildData guildData = dbGuild.getData();
        MessageBuilder toSend = new MessageBuilder().append(String.format(format, mentions(event), event.getAuthor().getAsMention()));
        if (!guildData.isNoMentionsAction() && swapNames) {
            toSend = new MessageBuilder().append(String.format(format, event.getAuthor().getAsMention(), mentions(event)));
        }
        if (guildData.isNoMentionsAction() && swapNames) {
            toSend = new MessageBuilder().append(String.format(format, "**" + noMentions(event) + "**", "**" + event.getMember().getEffectiveName() + "**"));
        }
        if (swapNames && guildData.isNoMentionsAction()) {
            toSend = new MessageBuilder().append(String.format(format, "**" + event.getMember().getEffectiveName() + "**", "**" + noMentions(event) + "**"));
        }
        if (isLonely(event)) {
            toSend = new MessageBuilder().append("**").append(lonelyLine).append("**");
        }
        event.getChannel().sendFile(CACHE.getInput(random), imageName, toSend.build()).queue();
    } catch (Exception e) {
        event.getChannel().sendMessage(EmoteReference.ERROR + "I'd like to know what happened, but I couldn't send the image.").queue();
        log.error("Error while performing Action Command ``" + name + "``. The image ``" + random + "`` threw an Exception.", e);
    }
}
Also used : GuildData(net.kodehawa.mantarobot.data.entities.helpers.GuildData) DBGuild(net.kodehawa.mantarobot.data.entities.DBGuild) MessageBuilder(net.dv8tion.jda.core.MessageBuilder)

Aggregations

DBGuild (net.kodehawa.mantarobot.data.entities.DBGuild)23 GuildData (net.kodehawa.mantarobot.data.entities.helpers.GuildData)18 Command (net.kodehawa.mantarobot.modules.Command)10 SimpleCommand (net.kodehawa.mantarobot.modules.commands.SimpleCommand)10 GuildMessageReceivedEvent (net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent)8 MantaroData (net.kodehawa.mantarobot.data.MantaroData)8 EmoteReference (net.kodehawa.mantarobot.utils.commands.EmoteReference)8 EmbedBuilder (net.dv8tion.jda.core.EmbedBuilder)7 Utils (net.kodehawa.mantarobot.utils.Utils)7 TimeUnit (java.util.concurrent.TimeUnit)6 Collectors (java.util.stream.Collectors)6 Slf4j (lombok.extern.slf4j.Slf4j)6 MantaroBot (net.kodehawa.mantarobot.MantaroBot)6 Permission (net.dv8tion.jda.core.Permission)5 net.dv8tion.jda.core.entities (net.dv8tion.jda.core.entities)5 PermissionException (net.dv8tion.jda.core.exceptions.PermissionException)5 CommandRegistry (net.kodehawa.mantarobot.modules.CommandRegistry)5 Module (net.kodehawa.mantarobot.modules.Module)5 CommandPermission (net.kodehawa.mantarobot.modules.commands.CommandPermission)5 Category (net.kodehawa.mantarobot.modules.commands.base.Category)5