Search in sources :

Example 1 with GuildMemberJoinEvent

use of net.dv8tion.jda.core.events.guild.member.GuildMemberJoinEvent in project MantaroBot by Mantaro.

the class MantaroListener method onUserJoin.

private void onUserJoin(GuildMemberJoinEvent event) {
    try {
        String role = MantaroData.db().getGuild(event.getGuild()).getData().getGuildAutoRole();
        DBGuild dbg = MantaroData.db().getGuild(event.getGuild());
        GuildData data = dbg.getData();
        String hour = df.format(new Date(System.currentTimeMillis()));
        if (role != null) {
            try {
                event.getGuild().getController().addRolesToMember(event.getMember(), event.getGuild().getRoleById(role)).queue(s -> log.debug("Successfully added a new role to " + event.getMember()));
            } catch (PermissionException e) {
                MantaroData.db().getGuild(event.getGuild()).getData().setGuildAutoRole(null);
                MantaroData.db().getGuild(event.getGuild()).save();
                event.getGuild().getOwner().getUser().openPrivateChannel().queue(messageChannel -> messageChannel.sendMessage("Removed autorole since I don't have the permissions to assign that role").queue());
            }
        }
        String logChannel = MantaroData.db().getGuild(event.getGuild()).getData().getGuildLogChannel();
        if (logChannel != null) {
            TextChannel tc = event.getGuild().getTextChannelById(logChannel);
            tc.sendMessage(String.format("`[%s]` 📣 `%s#%s` just joined `%s` `(User #%d | ID:%s)`", hour, event.getMember().getEffectiveName(), event.getMember().getUser().getDiscriminator(), event.getGuild().getName(), event.getGuild().getMembers().size(), event.getGuild().getId())).queue();
            logTotal++;
        }
        String joinChannel = MantaroData.db().getGuild(event.getGuild()).getData().getLogJoinLeaveChannel();
        String joinMessage = MantaroData.db().getGuild(event.getGuild()).getData().getJoinMessage();
        if (joinChannel != null && joinMessage != null) {
            TextChannel tc = event.getGuild().getTextChannelById(joinChannel);
            if (joinMessage.contains("$(")) {
                Map<String, String> dynamicMap = new HashMap<>();
                map("event", dynamicMap, event);
                joinMessage = dynamicResolve(joinMessage, dynamicMap);
            }
            int c = joinMessage.indexOf(':');
            if (c != -1) {
                String m = joinMessage.substring(0, c);
                String v = joinMessage.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(joinMessage).queue();
        }
    } catch (Exception e) {
    }
}
Also used : PermissionException(net.dv8tion.jda.core.exceptions.PermissionException) UserData(net.kodehawa.mantarobot.data.entities.helpers.UserData) StatusChangeEvent(net.dv8tion.jda.core.events.StatusChangeEvent) java.util(java.util) GuildMessageDeleteEvent(net.dv8tion.jda.core.events.message.guild.GuildMessageDeleteEvent) Async(br.com.brjdevs.java.utils.async.Async) GuildMemberJoinEvent(net.dv8tion.jda.core.events.guild.member.GuildMemberJoinEvent) SimpleDateFormat(java.text.SimpleDateFormat) MantaroBot(net.kodehawa.mantarobot.MantaroBot) PermissionException(net.dv8tion.jda.core.exceptions.PermissionException) GuildMessageReceivedEvent(net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent) GuildLeaveEvent(net.dv8tion.jda.core.events.guild.GuildLeaveEvent) Permission(net.dv8tion.jda.core.Permission) DBGuild(net.kodehawa.mantarobot.data.entities.DBGuild) DisconnectEvent(net.dv8tion.jda.core.events.DisconnectEvent) JDA(net.dv8tion.jda.core.JDA) GuildMessageUpdateEvent(net.dv8tion.jda.core.events.message.guild.GuildMessageUpdateEvent) EventListener(net.dv8tion.jda.core.hooks.EventListener) DateFormat(java.text.DateFormat) CommandListener(net.kodehawa.mantarobot.core.listeners.command.CommandListener) GuildStatsManager(net.kodehawa.mantarobot.commands.info.GuildStatsManager) net.dv8tion.jda.core.entities(net.dv8tion.jda.core.entities) RateLimiter(net.kodehawa.mantarobot.commands.currency.RateLimiter) Mapifier.map(net.kodehawa.mantarobot.commands.custom.Mapifier.map) GsonDataManager(net.kodehawa.mantarobot.utils.data.GsonDataManager) GuildJoinEvent(net.dv8tion.jda.core.events.guild.GuildJoinEvent) Collectors(java.util.stream.Collectors) CacheLoader(com.google.common.cache.CacheLoader) TimeUnit(java.util.concurrent.TimeUnit) Slf4j(lombok.extern.slf4j.Slf4j) ModLog(net.kodehawa.mantarobot.commands.moderation.ModLog) Mapifier.dynamicResolve(net.kodehawa.mantarobot.commands.custom.Mapifier.dynamicResolve) ExceptionEvent(net.dv8tion.jda.core.events.ExceptionEvent) GuildData(net.kodehawa.mantarobot.data.entities.helpers.GuildData) EmoteReference(net.kodehawa.mantarobot.utils.commands.EmoteReference) EmbedJSON(net.kodehawa.mantarobot.commands.custom.EmbedJSON) MantaroData(net.kodehawa.mantarobot.data.MantaroData) Event(net.dv8tion.jda.core.events.Event) ShardMonitorEvent(net.kodehawa.mantarobot.core.ShardMonitorEvent) GuildUnbanEvent(net.dv8tion.jda.core.events.guild.GuildUnbanEvent) LoggedEvent(net.kodehawa.mantarobot.commands.info.GuildStatsManager.LoggedEvent) Pattern(java.util.regex.Pattern) GuildBanEvent(net.dv8tion.jda.core.events.guild.GuildBanEvent) GuildMemberLeaveEvent(net.dv8tion.jda.core.events.guild.member.GuildMemberLeaveEvent) 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 2 with GuildMemberJoinEvent

use of net.dv8tion.jda.core.events.guild.member.GuildMemberJoinEvent in project MantaroBot by Mantaro.

the class MantaroListener method onEvent.

@Override
public void onEvent(Event event) {
    if (event instanceof ShardMonitorEvent) {
        if (MantaroBot.getInstance().getShards()[shardId].getEventManager().getLastJDAEventTimeDiff() > 120000)
            return;
        ((ShardMonitorEvent) event).alive(shardId, ShardMonitorEvent.MANTARO_LISTENER);
        return;
    }
    if (event instanceof GuildMessageReceivedEvent) {
        GuildMessageReceivedEvent e = (GuildMessageReceivedEvent) event;
        Async.thread("BirthdayThread", () -> onMessage(e));
        return;
    }
    //Log intensifies
    if (event instanceof GuildMessageUpdateEvent) {
        Async.thread("LogThread(Edit)", () -> logEdit((GuildMessageUpdateEvent) event));
        return;
    }
    if (event instanceof GuildMessageDeleteEvent) {
        Async.thread("LogThread(Delete)", () -> logDelete((GuildMessageDeleteEvent) event));
        return;
    }
    if (event instanceof GuildMemberJoinEvent) {
        Async.thread("LogThread(Join)", () -> onUserJoin((GuildMemberJoinEvent) event));
        return;
    }
    if (event instanceof GuildMemberLeaveEvent) {
        Async.thread("LogThread(Leave)", () -> onUserLeave((GuildMemberLeaveEvent) event));
        return;
    }
    if (event instanceof GuildUnbanEvent) {
        Async.thread("LogThread(Unban)", () -> logUnban((GuildUnbanEvent) event));
        return;
    }
    if (event instanceof GuildBanEvent) {
        Async.thread("LogThread(Ban)", () -> logBan((GuildBanEvent) event));
        return;
    }
    if (event instanceof GuildJoinEvent) {
        Async.thread("LogThread(GuildJoin)", () -> onJoin((GuildJoinEvent) event));
        return;
    }
    if (event instanceof GuildLeaveEvent) {
        Async.thread("LogThread(GuildLeave)", () -> onLeave((GuildLeaveEvent) event));
    }
    //debug
    if (event instanceof StatusChangeEvent) {
        logStatusChange((StatusChangeEvent) event);
    }
    if (event instanceof DisconnectEvent) {
        onDisconnect((DisconnectEvent) event);
    }
    if (event instanceof ExceptionEvent) {
        onException((ExceptionEvent) event);
    }
}
Also used : ExceptionEvent(net.dv8tion.jda.core.events.ExceptionEvent) GuildJoinEvent(net.dv8tion.jda.core.events.guild.GuildJoinEvent) StatusChangeEvent(net.dv8tion.jda.core.events.StatusChangeEvent) GuildMemberJoinEvent(net.dv8tion.jda.core.events.guild.member.GuildMemberJoinEvent) GuildMemberLeaveEvent(net.dv8tion.jda.core.events.guild.member.GuildMemberLeaveEvent) GuildMessageUpdateEvent(net.dv8tion.jda.core.events.message.guild.GuildMessageUpdateEvent) GuildLeaveEvent(net.dv8tion.jda.core.events.guild.GuildLeaveEvent) GuildBanEvent(net.dv8tion.jda.core.events.guild.GuildBanEvent) ShardMonitorEvent(net.kodehawa.mantarobot.core.ShardMonitorEvent) GuildMessageDeleteEvent(net.dv8tion.jda.core.events.message.guild.GuildMessageDeleteEvent) GuildUnbanEvent(net.dv8tion.jda.core.events.guild.GuildUnbanEvent) DisconnectEvent(net.dv8tion.jda.core.events.DisconnectEvent) GuildMessageReceivedEvent(net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent)

Aggregations

DisconnectEvent (net.dv8tion.jda.core.events.DisconnectEvent)2 ExceptionEvent (net.dv8tion.jda.core.events.ExceptionEvent)2 StatusChangeEvent (net.dv8tion.jda.core.events.StatusChangeEvent)2 GuildBanEvent (net.dv8tion.jda.core.events.guild.GuildBanEvent)2 GuildJoinEvent (net.dv8tion.jda.core.events.guild.GuildJoinEvent)2 GuildLeaveEvent (net.dv8tion.jda.core.events.guild.GuildLeaveEvent)2 GuildUnbanEvent (net.dv8tion.jda.core.events.guild.GuildUnbanEvent)2 GuildMemberJoinEvent (net.dv8tion.jda.core.events.guild.member.GuildMemberJoinEvent)2 GuildMemberLeaveEvent (net.dv8tion.jda.core.events.guild.member.GuildMemberLeaveEvent)2 GuildMessageDeleteEvent (net.dv8tion.jda.core.events.message.guild.GuildMessageDeleteEvent)2 GuildMessageReceivedEvent (net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent)2 GuildMessageUpdateEvent (net.dv8tion.jda.core.events.message.guild.GuildMessageUpdateEvent)2 ShardMonitorEvent (net.kodehawa.mantarobot.core.ShardMonitorEvent)2 Async (br.com.brjdevs.java.utils.async.Async)1 CacheLoader (com.google.common.cache.CacheLoader)1 DateFormat (java.text.DateFormat)1 SimpleDateFormat (java.text.SimpleDateFormat)1 java.util (java.util)1 TimeUnit (java.util.concurrent.TimeUnit)1 Pattern (java.util.regex.Pattern)1