Search in sources :

Example 11 with GuildData

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

the class MantaroListener method onMessage.

private void onMessage(GuildMessageReceivedEvent event) {
    //Moderation features
    DBGuild dbGuild = MantaroData.db().getGuild(event.getGuild());
    GuildData guildData = dbGuild.getData();
    //This is a pretty lazy check.
    if (!guildData.getMutedTimelyUsers().isEmpty()) {
        guildData.getMutedTimelyUsers().forEach((id, maxTime) -> {
            System.out.println(System.currentTimeMillis() > maxTime);
            if (System.currentTimeMillis() > maxTime) {
                try {
                    guildData.getMutedTimelyUsers().remove(id);
                    dbGuild.saveAsync();
                    event.getGuild().getController().removeRolesFromMember(event.getGuild().getMemberById(id), event.getGuild().getRoleById(guildData.getMutedRole())).queue();
                    guildData.setCases(guildData.getCases() + 1);
                    dbGuild.save();
                    ModLog.log(event.getMember(), MantaroBot.getInstance().getUserById(id), "Mute timeout expired", ModLog.ModAction.UNMUTE, guildData.getCases());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    //link protection
    if (guildData.isLinkProtection() && !guildData.getLinkProtectionAllowedChannels().contains(event.getChannel().getId())) {
        if (DISCORD_INVITE.matcher(event.getMessage().getContent()).find() && !event.getMember().hasPermission(Permission.ADMINISTRATOR) && !event.getMember().hasPermission(Permission.MANAGE_SERVER)) {
            Member bot = event.getGuild().getSelfMember();
            if (bot.hasPermission(Permission.MESSAGE_MANAGE) || bot.hasPermission(Permission.ADMINISTRATOR)) {
                User author = event.getAuthor();
                //Ignore myself.
                if (event.getAuthor().getId().equals(event.getJDA().getSelfUser().getId())) {
                    return;
                }
                //Ignore log channel.
                if (guildData.getGuildLogChannel() != null && event.getChannel().getId().equals(guildData.getGuildLogChannel())) {
                    return;
                }
                //Yes, I know the check previously done is redundant, but in case someone decides to change the law of nature, it should do	.
                event.getMessage().delete().queue();
                event.getChannel().sendMessage(EmoteReference.ERROR + "**You cannot advertise here.** Deleted invite link sent by **" + author.getName() + "#" + author.getDiscriminator() + "**.").queue();
            } else {
                event.getChannel().sendMessage(EmoteReference.ERROR + "I cannot remove the invite link because I don't have permission to delete messages!").queue();
            }
        }
    }
    //Slow mode
    if (guildData.isSlowMode()) {
        if (!slowModeLimiter.process(event.getAuthor().getId())) {
            Member bot = event.getGuild().getSelfMember();
            if (bot.hasPermission(Permission.MESSAGE_MANAGE) || bot.hasPermission(Permission.ADMINISTRATOR) && !event.getMember().hasPermission(Permission.ADMINISTRATOR) && !event.getMember().hasPermission(Permission.MANAGE_SERVER)) {
                event.getMessage().delete().queue();
            } else {
                event.getChannel().sendMessage(EmoteReference.ERROR + "I cannot engage slow mode because I don't have permission to delete messages!").queue();
                guildData.setSlowMode(false);
                dbGuild.save();
                event.getChannel().sendMessage(EmoteReference.WARNING + "**Disabled slowmode due to a lack of permissions.**").queue();
            }
        }
    }
    //Anti-spam. Allows 2 messages every 3 seconds.
    if (guildData.isAntiSpam()) {
        if (!spamModeLimiter.process(event.getAuthor().getId())) {
            Member bot = event.getGuild().getSelfMember();
            if (bot.hasPermission(Permission.MESSAGE_MANAGE) || bot.hasPermission(Permission.ADMINISTRATOR) && !event.getMember().hasPermission(Permission.ADMINISTRATOR) && !event.getMember().hasPermission(Permission.MANAGE_SERVER)) {
                event.getMessage().delete().queue();
            } else {
                event.getChannel().sendMessage(EmoteReference.ERROR + "I cannot engage anti-spam mode because I don't have permission to delete messages!").queue();
                guildData.setAntiSpam(false);
                dbGuild.save();
                event.getChannel().sendMessage(EmoteReference.WARNING + "**Disabled anti-spam mode due to a lack of permissions.**").queue();
            }
        }
    }
    //Birthday role checker.
    try {
        Role birthdayRole = event.getGuild().getRoleById(MantaroData.db().getGuild(event.getGuild()).getData().getBirthdayRole());
        UserData user = MantaroData.db().getUser(event.getMember()).getData();
        if (birthdayRole != null && user.getBirthday() != null) {
            TextChannel channel = event.getGuild().getTextChannelById(MantaroData.db().getGuild(event.getGuild()).getData().getBirthdayChannel());
            Calendar cal = Calendar.getInstance();
            SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
            if (user.getBirthday().substring(0, 5).equals(dateFormat.format(cal.getTime()).substring(0, 5))) {
                if (!event.getMember().getRoles().contains(birthdayRole)) {
                    event.getGuild().getController().addRolesToMember(event.getMember(), birthdayRole).queue(s -> channel.sendMessage(String.format(EmoteReference.POPPER + "**%s is a year older now! Wish them a happy birthday.** :tada:", event.getMember().getEffectiveName())).queue());
                }
            } else {
                if (event.getGuild().getRoles().contains(birthdayRole)) {
                    event.getGuild().getController().removeRolesFromMember(event.getMember(), birthdayRole).queue();
                }
            }
        }
    } catch (Exception e) {
        if (e instanceof PermissionException) {
            resetBirthdays(event.getGuild());
            event.getChannel().sendMessage(EmoteReference.ERROR + "Error while applying birthday role, so the role assigner will be resetted. **Remember that the bot MUST have permissions to apply roles to that person, always**").queue();
        }
    //else ignore
    }
}
Also used : PermissionException(net.dv8tion.jda.core.exceptions.PermissionException) GuildData(net.kodehawa.mantarobot.data.entities.helpers.GuildData) DBGuild(net.kodehawa.mantarobot.data.entities.DBGuild) UserData(net.kodehawa.mantarobot.data.entities.helpers.UserData) SimpleDateFormat(java.text.SimpleDateFormat) PermissionException(net.dv8tion.jda.core.exceptions.PermissionException)

Example 12 with GuildData

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

the class InfoCmds method help.

@Command
public static void help(CommandRegistry cr) {
    Random r = new Random();
    List<String> jokes = Collections.unmodifiableList(Arrays.asList("Yo damn I heard you like help, because you just issued the help command to get the help about the help command.", "Congratulations, you managed to use the help command.", "Helps you to help yourself.", "Help Inception.", "A help helping helping helping help.", "I wonder if this is what you are looking for..."));
    cr.register("help", new SimpleCommand(Category.INFO) {

        @Override
        protected void call(GuildMessageReceivedEvent event, String content, String[] args) {
            if (content.isEmpty()) {
                String defaultPrefix = MantaroData.config().get().prefix, guildPrefix = MantaroData.db().getGuild(event.getGuild()).getData().getGuildCustomPrefix();
                String prefix = guildPrefix == null ? defaultPrefix : guildPrefix;
                DBGuild dbGuild = MantaroData.db().getGuild(event.getGuild());
                GuildData guildData = dbGuild.getData();
                EmbedBuilder embed = baseEmbed(event, "MantaroBot Help").setColor(Color.PINK).setDescription("Command help. For extended usage please use " + String.format("%shelp <command>.", prefix) + (guildData.getDisabledCommands().isEmpty() ? "" : "\n" + "Only showing non-disabled commands. Total disabled commands: " + guildData.getDisabledCommands().size())).setFooter(String.format("To check command usage, type %shelp <command> // -> Commands: " + CommandProcessor.REGISTRY.commands().values().stream().filter(c -> c.category() != null).count(), prefix), null);
                Arrays.stream(Category.values()).filter(c -> c != Category.CURRENCY || !MantaroData.config().get().isPremiumBot()).filter(c -> c != Category.MODERATION || CommandPermission.ADMIN.test(event.getMember())).filter(c -> c != Category.OWNER || CommandPermission.OWNER.test(event.getMember())).forEach(c -> embed.addField(c + " Commands:", forType(guildData, c), false));
                event.getChannel().sendMessage(embed.build()).queue();
            } else {
                net.kodehawa.mantarobot.modules.commands.base.Command command = CommandProcessor.REGISTRY.commands().get(content);
                if (command != null) {
                    final MessageEmbed help = command.help(event);
                    Optional.ofNullable(help).ifPresent((help1) -> event.getChannel().sendMessage(help1).queue());
                    if (help == null)
                        event.getChannel().sendMessage(EmoteReference.ERROR + "There's no extended help set for this command.").queue();
                } else {
                    event.getChannel().sendMessage(EmoteReference.ERROR + "A command with this name doesn't exist").queue();
                }
            }
        }

        @Override
        public MessageEmbed help(GuildMessageReceivedEvent event) {
            return helpEmbed(event, "Help Command").setColor(Color.PINK).setDescription("**" + jokes.get(r.nextInt(jokes.size())) + "**").addField("Usage", "`~>help` - **Return information about who issued the command**.\n" + "`~>help <command>` - **Return information about the command specified**.", false).build();
        }
    });
}
Also used : SimpleCommand(net.kodehawa.mantarobot.modules.commands.SimpleCommand) Color(java.awt.Color) MantaroInfo(net.kodehawa.mantarobot.MantaroInfo) CommandProcessor(net.kodehawa.mantarobot.core.CommandProcessor) java.util(java.util) CommandStatsManager(net.kodehawa.mantarobot.commands.info.CommandStatsManager) Utils(net.kodehawa.mantarobot.utils.Utils) Module(net.kodehawa.mantarobot.modules.Module) MantaroShard(net.kodehawa.mantarobot.MantaroShard) StatsHelper.calculateDouble(net.kodehawa.mantarobot.commands.info.StatsHelper.calculateDouble) MantaroBot(net.kodehawa.mantarobot.MantaroBot) MessageBuilder(net.dv8tion.jda.core.MessageBuilder) GuildMessageReceivedEvent(net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent) Cursor(com.rethinkdb.net.Cursor) DBGuild(net.kodehawa.mantarobot.data.entities.DBGuild) Duration(java.time.Duration) JDA(net.dv8tion.jda.core.JDA) CommandRegistry(net.kodehawa.mantarobot.modules.CommandRegistry) ManagementFactory(java.lang.management.ManagementFactory) Command(net.kodehawa.mantarobot.modules.Command) TextChannelGround(net.kodehawa.mantarobot.commands.currency.TextChannelGround) CommandListener(net.kodehawa.mantarobot.core.listeners.command.CommandListener) GuildStatsManager(net.kodehawa.mantarobot.commands.info.GuildStatsManager) PostLoadEvent(net.kodehawa.mantarobot.modules.events.PostLoadEvent) JDAInfo(net.dv8tion.jda.core.JDAInfo) net.dv8tion.jda.core.entities(net.dv8tion.jda.core.entities) RateLimiter(net.kodehawa.mantarobot.commands.currency.RateLimiter) MantaroListener(net.kodehawa.mantarobot.core.listeners.MantaroListener) Category(net.kodehawa.mantarobot.modules.commands.base.Category) Instant(java.time.Instant) AsyncInfoMonitor(net.kodehawa.mantarobot.commands.info.AsyncInfoMonitor) Collectors(java.util.stream.Collectors) EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) OnlineStatus(net.dv8tion.jda.core.OnlineStatus) TimeUnit(java.util.concurrent.TimeUnit) PlayerLibrary(com.sedmelluq.discord.lavaplayer.tools.PlayerLibrary) DurationFormatUtils(org.apache.commons.lang3.time.DurationFormatUtils) GuildData(net.kodehawa.mantarobot.data.entities.helpers.GuildData) EmoteReference(net.kodehawa.mantarobot.utils.commands.EmoteReference) DateTimeFormatter(java.time.format.DateTimeFormatter) CommandPermission(net.kodehawa.mantarobot.modules.commands.CommandPermission) MantaroData(net.kodehawa.mantarobot.data.MantaroData) HelpUtils.forType(net.kodehawa.mantarobot.commands.info.HelpUtils.forType) RethinkDB(com.rethinkdb.RethinkDB) StatsHelper.calculateInt(net.kodehawa.mantarobot.commands.info.StatsHelper.calculateInt) GuildData(net.kodehawa.mantarobot.data.entities.helpers.GuildData) EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) DBGuild(net.kodehawa.mantarobot.data.entities.DBGuild) SimpleCommand(net.kodehawa.mantarobot.modules.commands.SimpleCommand) SimpleCommand(net.kodehawa.mantarobot.modules.commands.SimpleCommand) Command(net.kodehawa.mantarobot.modules.Command) GuildMessageReceivedEvent(net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent) SimpleCommand(net.kodehawa.mantarobot.modules.commands.SimpleCommand) Command(net.kodehawa.mantarobot.modules.Command)

Example 13 with GuildData

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

the class MiscCmds method onPostLoad.

@Command
public static void onPostLoad(PostLoadEvent e) {
    OptsCmd.registerOption("timedisplay:set", (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 mode (12h or 24h)").queue();
            return;
        }
        String mode = args[0];
        switch(mode) {
            case "12h":
                event.getChannel().sendMessage(EmoteReference.CORRECT + "Set time display mode to 12h").queue();
                guildData.setTimeDisplay(1);
                dbGuild.save();
                break;
            case "24h":
                event.getChannel().sendMessage(EmoteReference.CORRECT + "Set time display mode to 24h").queue();
                guildData.setTimeDisplay(0);
                dbGuild.save();
                break;
            default:
                event.getChannel().sendMessage(EmoteReference.ERROR + "Not a valid choice. Valid choices: **24h**, **12h**").queue();
                break;
        }
    });
}
Also used : 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 14 with GuildData

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

the class ActionCmds method onPostLoad.

@Command
public static void onPostLoad(PostLoadEvent e) {
    OptsCmd.registerOption("actionmention:toggle", event -> {
        DBGuild dbGuild = MantaroData.db().getGuild(event.getGuild());
        GuildData guildData = dbGuild.getData();
        boolean toggler = guildData.isNoMentionsAction();
        guildData.setNoMentionsAction(!toggler);
        event.getChannel().sendMessage(EmoteReference.CORRECT + "Set no action mentions in chat to " + "**" + !toggler + "**").queue();
        dbGuild.save();
    });
}
Also used : 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 15 with GuildData

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

the class CustomCmds method onPostLoad.

@Command
public static void onPostLoad(PostLoadEvent e) {
    db().getCustomCommands().forEach(custom -> {
        if (!NAME_PATTERN.matcher(custom.getName()).matches()) {
            String newName = INVALID_CHARACTERS_PATTERN.matcher(custom.getName()).replaceAll("_");
            log.warn("Custom Command with Invalid Characters '%s' found. Replacing with '%'", custom.getName());
            custom.deleteAsync();
            custom = CustomCommand.of(custom.getGuildId(), newName, custom.getValues());
            custom.saveAsync();
        }
        if (CommandProcessor.REGISTRY.commands().containsKey(custom.getName()) && !CommandProcessor.REGISTRY.commands().get(custom.getName()).equals(customCommand)) {
            custom.deleteAsync();
            custom = CustomCommand.of(custom.getGuildId(), "_" + custom.getName(), custom.getValues());
            custom.saveAsync();
        }
        CommandProcessor.REGISTRY.commands().put(custom.getName(), customCommand);
        customCommands.put(custom.getId(), custom.getValues());
    });
    OptsCmd.registerOption("admincustom", (event, args) -> {
        if (args.length == 0) {
            OptsCmd.onHelp(event);
            return;
        }
        String action = args[0];
        DBGuild dbGuild = MantaroData.db().getGuild(event.getGuild());
        GuildData guildData = dbGuild.getData();
        try {
            guildData.setCustomAdminLock(Boolean.parseBoolean(action));
            dbGuild.save();
            String toSend = EmoteReference.CORRECT + (Boolean.parseBoolean(action) ? "``Permission -> User command creation " + "is now admin only.``" : "``Permission -> User command creation can be done by anyone.``");
            event.getChannel().sendMessage(toSend).queue();
        } catch (Exception ex) {
            event.getChannel().sendMessage(EmoteReference.ERROR + "Silly, that's not a boolean value!").queue();
        }
    });
}
Also used : GuildData(net.kodehawa.mantarobot.data.entities.helpers.GuildData) DBGuild(net.kodehawa.mantarobot.data.entities.DBGuild) SimpleCommand(net.kodehawa.mantarobot.modules.commands.SimpleCommand) CustomCommand(net.kodehawa.mantarobot.data.entities.CustomCommand) Command(net.kodehawa.mantarobot.modules.Command) AbstractCommand(net.kodehawa.mantarobot.modules.commands.base.AbstractCommand)

Aggregations

DBGuild (net.kodehawa.mantarobot.data.entities.DBGuild)17 GuildData (net.kodehawa.mantarobot.data.entities.helpers.GuildData)17 Command (net.kodehawa.mantarobot.modules.Command)8 SimpleCommand (net.kodehawa.mantarobot.modules.commands.SimpleCommand)8 GuildMessageReceivedEvent (net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent)6 MantaroData (net.kodehawa.mantarobot.data.MantaroData)6 EmoteReference (net.kodehawa.mantarobot.utils.commands.EmoteReference)6 TimeUnit (java.util.concurrent.TimeUnit)5 Collectors (java.util.stream.Collectors)5 Permission (net.dv8tion.jda.core.Permission)5 Slf4j (lombok.extern.slf4j.Slf4j)4 EmbedBuilder (net.dv8tion.jda.core.EmbedBuilder)4 net.dv8tion.jda.core.entities (net.dv8tion.jda.core.entities)4 PermissionException (net.dv8tion.jda.core.exceptions.PermissionException)4 MantaroBot (net.kodehawa.mantarobot.MantaroBot)4 Utils (net.kodehawa.mantarobot.utils.Utils)4 SimpleDateFormat (java.text.SimpleDateFormat)3 DiscordUtils (net.kodehawa.mantarobot.utils.DiscordUtils)3 FriendlyException (com.sedmelluq.discord.lavaplayer.tools.FriendlyException)2 AudioTrack (com.sedmelluq.discord.lavaplayer.track.AudioTrack)2