Search in sources :

Example 6 with GuildSettings

use of ml.duncte123.skybot.objects.guild.GuildSettings in project SkyBot by duncte123.

the class BotListener method onGuildMemberJoin.

/**
 * This will fire when a new member joins
 *
 * @param event The corresponding {@link GuildMemberJoinEvent}
 */
@Override
public void onGuildMemberJoin(GuildMemberJoinEvent event) {
    if (event.getMember().equals(event.getGuild().getSelfMember()))
        return;
    /*
        {{USER_MENTION}} = mention user
        {{USER_NAME}} = return username
        {{GUILD_NAME}} = the name of the guild
        {{GUILD_USER_COUNT}} = member count
        {{GUILD_OWNER_MENTION}} = mention the guild owner
        {{GUILD_OWNER_NAME}} = return the name form the owner
         */
    GuildSettings settings = GuildSettingsUtils.getGuild(event.getGuild());
    if (settings.isEnableJoinMessage()) {
        String welcomeLeaveChannelId = (settings.getWelcomeLeaveChannel() == null || "".equals(settings.getWelcomeLeaveChannel()) ? GuildUtils.getPublicChannel(event.getGuild()).getId() : settings.getWelcomeLeaveChannel());
        TextChannel welcomeLeaveChannel = event.getGuild().getTextChannelById(welcomeLeaveChannelId);
        String msg = parseGuildVars(settings.getCustomJoinMessage(), event);
        if (!msg.isEmpty() || "".equals(msg) || welcomeLeaveChannel != null)
            MessageUtils.sendMsg(welcomeLeaveChannel, msg);
    }
    if (settings.getAutoroleRole() != null && !"".equals(settings.getAutoroleRole()) && event.getGuild().getSelfMember().hasPermission(Permission.MANAGE_ROLES)) {
        Role r = event.getGuild().getRoleById(settings.getAutoroleRole());
        if (r != null && !event.getGuild().getPublicRole().equals(r))
            event.getGuild().getController().addSingleRoleToMember(event.getMember(), r).queue(null, it -> {
            });
    }
}
Also used : EvalCommand(ml.duncte123.skybot.commands.essentials.eval.EvalCommand) Arrays(java.util.Arrays) ShutdownEvent(net.dv8tion.jda.core.events.ShutdownEvent) CommandCategory(ml.duncte123.skybot.objects.command.CommandCategory) GuildMemberJoinEvent(net.dv8tion.jda.core.events.guild.member.GuildMemberJoinEvent) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) ListenerAdapter(net.dv8tion.jda.core.hooks.ListenerAdapter) ShardManager(net.dv8tion.jda.bot.sharding.ShardManager) GuildMessageReceivedEvent(net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent) GuildLeaveEvent(net.dv8tion.jda.core.events.guild.GuildLeaveEvent) Matcher(java.util.regex.Matcher) Permission(net.dv8tion.jda.core.Permission) Command(ml.duncte123.skybot.objects.command.Command) ml.duncte123.skybot.utils(ml.duncte123.skybot.utils) MusicCommand(ml.duncte123.skybot.objects.command.MusicCommand) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) LavalinkManager(fredboat.audio.player.LavalinkManager) ReadyEvent(net.dv8tion.jda.core.events.ReadyEvent) Logger(org.slf4j.Logger) GuildVoiceLeaveEvent(net.dv8tion.jda.core.events.guild.voice.GuildVoiceLeaveEvent) net.dv8tion.jda.core.entities(net.dv8tion.jda.core.entities) ErrorResponseException(net.dv8tion.jda.core.exceptions.ErrorResponseException) GuildJoinEvent(net.dv8tion.jda.core.events.guild.GuildJoinEvent) Executors(java.util.concurrent.Executors) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) GenericGuildMemberEvent(net.dv8tion.jda.core.events.guild.member.GenericGuildMemberEvent) GuildSettings(ml.duncte123.skybot.objects.guild.GuildSettings) Pattern(java.util.regex.Pattern) GuildMusicManager(ml.duncte123.skybot.audio.GuildMusicManager) GuildMemberLeaveEvent(net.dv8tion.jda.core.events.guild.member.GuildMemberLeaveEvent) GuildVoiceMoveEvent(net.dv8tion.jda.core.events.guild.voice.GuildVoiceMoveEvent) GuildSettings(ml.duncte123.skybot.objects.guild.GuildSettings)

Example 7 with GuildSettings

use of ml.duncte123.skybot.objects.guild.GuildSettings in project SkyBot by duncte123.

the class GuildSettingsUtils method registerNewGuild.

/**
 * This will register a new guild with their settings on bot join
 *
 * @param g The guild that we are joining
 * @return The new guild
 */
public static GuildSettings registerNewGuild(Guild g) {
    if (AirUtils.guildSettings.containsKey(g.getId())) {
        return AirUtils.guildSettings.get(g.getId());
    }
    GuildSettings newGuildSettings = new GuildSettings(g.getId());
    String dbName = AirUtils.DB.getName();
    Connection database = AirUtils.DB.getConnManager().getConnection();
    try {
        ResultSet resultSet = database.createStatement().executeQuery("SELECT id FROM " + dbName + ".guildSettings WHERE guildId='" + g.getId() + "'");
        int rows = 0;
        while (resultSet.next()) rows++;
        if (rows == 0) {
            PreparedStatement smt = database.prepareStatement("INSERT INTO " + dbName + ".guildSettings(guildId, guildName," + "customWelcomeMessage, prefix, customLeaveMessage) " + "VALUES('" + g.getId() + "',  ? , ? , ? , ?)");
            smt.setString(1, g.getName().replaceAll("\\P{Print}", ""));
            smt.setString(2, newGuildSettings.getCustomJoinMessage());
            smt.setString(3, Settings.PREFIX);
            smt.setString(4, newGuildSettings.getCustomLeaveMessage().replaceAll("\\P{Print}", ""));
            smt.execute();
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (database != null) {
            try {
                database.close();
            } catch (SQLException e2) {
                e2.printStackTrace();
            }
        }
    }
    AirUtils.guildSettings.put(g.getId(), newGuildSettings);
    return newGuildSettings;
}
Also used : GuildSettings(ml.duncte123.skybot.objects.guild.GuildSettings)

Aggregations

GuildSettings (ml.duncte123.skybot.objects.guild.GuildSettings)7 LavalinkManager (fredboat.audio.player.LavalinkManager)2 Arrays (java.util.Arrays)2 HashMap (java.util.HashMap)2 Executors (java.util.concurrent.Executors)2 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)2 TimeUnit (java.util.concurrent.TimeUnit)2 Consumer (java.util.function.Consumer)2 Matcher (java.util.regex.Matcher)2 Pattern (java.util.regex.Pattern)2 GuildMusicManager (ml.duncte123.skybot.audio.GuildMusicManager)2 EvalCommand (ml.duncte123.skybot.commands.essentials.eval.EvalCommand)2 Command (ml.duncte123.skybot.objects.command.Command)2 CommandCategory (ml.duncte123.skybot.objects.command.CommandCategory)2 MusicCommand (ml.duncte123.skybot.objects.command.MusicCommand)2 ml.duncte123.skybot.utils (ml.duncte123.skybot.utils)2 ShardManager (net.dv8tion.jda.bot.sharding.ShardManager)2 Permission (net.dv8tion.jda.core.Permission)2 net.dv8tion.jda.core.entities (net.dv8tion.jda.core.entities)2 ReadyEvent (net.dv8tion.jda.core.events.ReadyEvent)2