Search in sources :

Example 1 with Event

use of net.dv8tion.jda.core.events.Event 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)

Aggregations

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 Collectors (java.util.stream.Collectors)1 Slf4j (lombok.extern.slf4j.Slf4j)1 JDA (net.dv8tion.jda.core.JDA)1 Permission (net.dv8tion.jda.core.Permission)1 net.dv8tion.jda.core.entities (net.dv8tion.jda.core.entities)1 DisconnectEvent (net.dv8tion.jda.core.events.DisconnectEvent)1 Event (net.dv8tion.jda.core.events.Event)1 ExceptionEvent (net.dv8tion.jda.core.events.ExceptionEvent)1 StatusChangeEvent (net.dv8tion.jda.core.events.StatusChangeEvent)1 GuildBanEvent (net.dv8tion.jda.core.events.guild.GuildBanEvent)1 GuildJoinEvent (net.dv8tion.jda.core.events.guild.GuildJoinEvent)1 GuildLeaveEvent (net.dv8tion.jda.core.events.guild.GuildLeaveEvent)1 GuildUnbanEvent (net.dv8tion.jda.core.events.guild.GuildUnbanEvent)1