Search in sources :

Example 11 with DBGuild

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

the class MantaroListener method onUserLeave.

private void onUserLeave(GuildMemberLeaveEvent event) {
    try {
        String hour = df.format(new Date(System.currentTimeMillis()));
        DBGuild dbg = MantaroData.db().getGuild(event.getGuild());
        GuildData data = dbg.getData();
        String logChannel = MantaroData.db().getGuild(event.getGuild()).getData().getGuildLogChannel();
        if (logChannel != null) {
            TextChannel tc = event.getGuild().getTextChannelById(logChannel);
            tc.sendMessage("`[" + hour + "]` " + "📣 `" + event.getMember().getEffectiveName() + "#" + event.getMember().getUser().getDiscriminator() + "` just left `" + event.getGuild().getName() + "` `(User #" + event.getGuild().getMembers().size() + ")`").queue();
            logTotal++;
        }
        String leaveChannel = MantaroData.db().getGuild(event.getGuild()).getData().getLogJoinLeaveChannel();
        String leaveMessage = MantaroData.db().getGuild(event.getGuild()).getData().getLeaveMessage();
        if (leaveChannel != null && leaveMessage != null) {
            TextChannel tc = event.getGuild().getTextChannelById(leaveChannel);
            if (leaveMessage.contains("$(")) {
                Map<String, String> dynamicMap = new HashMap<>();
                map("event", dynamicMap, event);
                leaveMessage = dynamicResolve(leaveMessage, dynamicMap);
            }
            int c = leaveMessage.indexOf(':');
            if (c != -1) {
                String m = leaveMessage.substring(0, c);
                String v = leaveMessage.substring(c + 1);
                if (m.equals("embed")) {
                    EmbedJSON embed;
                    try {
                        embed = GsonDataManager.gson(false).fromJson('{' + v + '}', EmbedJSON.class);
                    } catch (Exception ignored) {
                        tc.sendMessage(EmoteReference.ERROR2 + "The string ``{" + v + "}`` isn't a valid JSON.").queue();
                        return;
                    }
                    tc.sendMessage(embed.gen(event)).queue();
                    return;
                }
            }
            tc.sendMessage(leaveMessage).queue();
        }
    } catch (Exception e) {
    }
}
Also used : GuildData(net.kodehawa.mantarobot.data.entities.helpers.GuildData) DBGuild(net.kodehawa.mantarobot.data.entities.DBGuild) EmbedJSON(net.kodehawa.mantarobot.commands.custom.EmbedJSON) PermissionException(net.dv8tion.jda.core.exceptions.PermissionException)

Example 12 with DBGuild

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

the class MantaroListener method resetBirthdays.

private void resetBirthdays(Guild guild) {
    DBGuild data = MantaroData.db().getGuild(guild);
    data.getData().setBirthdayChannel(null);
    data.getData().setBirthdayRole(null);
    data.save();
}
Also used : DBGuild(net.kodehawa.mantarobot.data.entities.DBGuild)

Example 13 with DBGuild

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

the class AudioRequester method loadSingle.

private void loadSingle(AudioTrack audioTrack, boolean silent) {
    AudioTrackInfo trackInfo = audioTrack.getInfo();
    DBGuild dbGuild = MantaroData.db().getGuild(event.getGuild());
    GuildData guildData = dbGuild.getData();
    String title = trackInfo.title;
    long length = trackInfo.length;
    long queueLimit = !Optional.ofNullable(dbGuild.getData().getMusicQueueSizeLimit()).isPresent() ? MAX_QUEUE_LENGTH : dbGuild.getData().getMusicQueueSizeLimit();
    int fqSize = guildData.getMaxFairQueue();
    if (getMusicManager().getTrackScheduler().getQueue().size() > queueLimit && !MantaroData.db().getUser(event.getMember()).isPremium() && !dbGuild.isPremium()) {
        if (!silent)
            event.getChannel().sendMessage(String.format(":warning: Could not queue %s: Surpassed queue song limit!", title)).queue(message -> message.delete().queueAfter(30, TimeUnit.SECONDS));
        if (musicManager.getTrackScheduler().isStopped())
            event.getGuild().getAudioManager().closeAudioConnection();
        return;
    }
    if (audioTrack.getInfo().length > MAX_SONG_LENGTH && !MantaroData.db().getUser(event.getMember()).isPremium() && !dbGuild.isPremium()) {
        event.getChannel().sendMessage(String.format(":warning: Could not queue %s: Track is longer than 21 minutes! (%s)", title, AudioUtils.getLength(length))).queue();
        if (musicManager.getTrackScheduler().isStopped())
            //do you?
            event.getGuild().getAudioManager().closeAudioConnection();
        return;
    }
    //Comparing if the URLs are the same to be 100% sure they're just not spamming the same url over and over again.
    if (musicManager.getTrackScheduler().getQueue().stream().filter(track -> track.getInfo().uri.equals(audioTrack.getInfo().uri)).count() > fqSize) {
        event.getChannel().sendMessage(EmoteReference.ERROR + String.format("**Surpassed fair queue level of %d (Too many songs which are exactly equal)**", fqSize + 1)).queue();
        return;
    }
    musicManager.getTrackScheduler().queue(new AudioTrackContext(event.getAuthor(), event.getChannel(), audioTrack.getSourceManager() instanceof YoutubeAudioSourceManager ? "https://www.youtube.com/watch?v=" + audioTrack.getIdentifier() : trackUrl, audioTrack));
    if (!silent) {
        event.getChannel().sendMessage(String.format("📣 Added to queue -> **%s** **!(%s)**", title, AudioUtils.getLength(length))).queue();
    }
}
Also used : FriendlyException(com.sedmelluq.discord.lavaplayer.tools.FriendlyException) Color(java.awt.Color) Utils(net.kodehawa.mantarobot.utils.Utils) IntConsumer(java.util.function.IntConsumer) DiscordUtils(net.kodehawa.mantarobot.utils.DiscordUtils) AudioPlaylist(com.sedmelluq.discord.lavaplayer.track.AudioPlaylist) ReactionOperations(net.kodehawa.mantarobot.core.listeners.operations.ReactionOperations) AudioLoadResultHandler(com.sedmelluq.discord.lavaplayer.player.AudioLoadResultHandler) EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) TimeUnit(java.util.concurrent.TimeUnit) Slf4j(lombok.extern.slf4j.Slf4j) GuildMessageReceivedEvent(net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent) Permission(net.dv8tion.jda.core.Permission) DBGuild(net.kodehawa.mantarobot.data.entities.DBGuild) YoutubeAudioSourceManager(com.sedmelluq.discord.lavaplayer.source.youtube.YoutubeAudioSourceManager) GuildData(net.kodehawa.mantarobot.data.entities.helpers.GuildData) EmoteReference(net.kodehawa.mantarobot.utils.commands.EmoteReference) MantaroData(net.kodehawa.mantarobot.data.MantaroData) Optional(java.util.Optional) AudioTrack(com.sedmelluq.discord.lavaplayer.track.AudioTrack) AudioTrackInfo(com.sedmelluq.discord.lavaplayer.track.AudioTrackInfo) GuildData(net.kodehawa.mantarobot.data.entities.helpers.GuildData) DBGuild(net.kodehawa.mantarobot.data.entities.DBGuild) YoutubeAudioSourceManager(com.sedmelluq.discord.lavaplayer.source.youtube.YoutubeAudioSourceManager) AudioTrackInfo(com.sedmelluq.discord.lavaplayer.track.AudioTrackInfo)

Example 14 with DBGuild

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

the class CommandRegistry method process.

public boolean process(GuildMessageReceivedEvent event, String cmdname, String content) {
    Command cmd = commands.get(cmdname);
    Config conf = MantaroData.config().get();
    DBGuild dbg = MantaroData.db().getGuild(event.getGuild());
    GuildData data = dbg.getData();
    if (cmd == null)
        return false;
    if (data.getDisabledCommands().contains(cmdname)) {
        return false;
    }
    if (data.getChannelSpecificDisabledCommands().get(event.getChannel().getId()) != null && data.getChannelSpecificDisabledCommands().get(event.getChannel().getId()).contains(cmdname)) {
        return false;
    }
    if (data.getDisabledUsers().contains(event.getAuthor().getId())) {
        return false;
    }
    if (MantaroData.db().getGuild(event.getGuild()).getData().getDisabledChannels().contains(event.getChannel().getId()) && cmd.category() != Category.MODERATION) {
        return false;
    }
    if (MantaroData.config().get().isPremiumBot() && cmd.category() == Category.CURRENCY) {
        return false;
    }
    if (data.getDisabledCategories().contains(cmd.category())) {
        return false;
    }
    //If we are in the patreon bot, deny all requests from unknown guilds.
    if (conf.isPremiumBot() && !conf.isOwner(event.getAuthor()) && !dbg.isPremium()) {
        event.getChannel().sendMessage(EmoteReference.ERROR + "Seems like you're trying to use the Patreon bot when this guild is **not** marked as premium. " + "**If you think this is an error please contact Kodehawa#3457 or poke me on #donators in the support guild**").queue();
        return false;
    }
    if (!cmd.permission().test(event.getMember())) {
        event.getChannel().sendMessage(EmoteReference.STOP + "You have no permissions to trigger this command").queue();
        return false;
    }
    cmd.run(event, cmdname, content);
    return true;
}
Also used : GuildData(net.kodehawa.mantarobot.data.entities.helpers.GuildData) DBGuild(net.kodehawa.mantarobot.data.entities.DBGuild) AliasCommand(net.kodehawa.mantarobot.modules.commands.AliasCommand) Command(net.kodehawa.mantarobot.modules.commands.base.Command) Config(net.kodehawa.mantarobot.data.Config)

Example 15 with DBGuild

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

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