Search in sources :

Example 1 with UserData

use of net.kodehawa.mantarobot.data.entities.helpers.UserData 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)

Aggregations

SimpleDateFormat (java.text.SimpleDateFormat)1 PermissionException (net.dv8tion.jda.core.exceptions.PermissionException)1 DBGuild (net.kodehawa.mantarobot.data.entities.DBGuild)1 GuildData (net.kodehawa.mantarobot.data.entities.helpers.GuildData)1 UserData (net.kodehawa.mantarobot.data.entities.helpers.UserData)1