Search in sources :

Example 1 with GuildBanEvent

use of net.dv8tion.jda.api.events.guild.GuildBanEvent in project Sx4 by sx4-discord-bot.

the class LoggerHandler method onGuildBan.

public void onGuildBan(GuildBanEvent event) {
    Guild guild = event.getGuild();
    User user = event.getUser();
    LoggerEvent loggerEvent = LoggerEvent.MEMBER_BANNED;
    LoggerContext loggerContext = new LoggerContext().setUser(user);
    WebhookEmbedBuilder embed = new WebhookEmbedBuilder();
    embed.setDescription(String.format("`%s` has been banned", user.getName()));
    embed.setColor(this.bot.getConfig().getRed());
    embed.setTimestamp(Instant.now());
    embed.setAuthor(new EmbedAuthor(user.getAsTag(), user.getEffectiveAvatarUrl(), null));
    embed.setFooter(new EmbedFooter(String.format("User ID: %s", user.getId()), null));
    this.bot.getMongo().aggregateLoggers(this.getPipeline(guild.getIdLong())).whenComplete((documents, exception) -> {
        if (ExceptionUtility.sendErrorMessage(exception)) {
            return;
        }
        if (documents.isEmpty()) {
            return;
        }
        Document data = documents.get(0);
        List<Document> loggers = LoggerUtility.getValidLoggers(data.getList("loggers", Document.class), loggerEvent, loggerContext);
        if (loggers.isEmpty()) {
            return;
        }
        if (guild.getSelfMember().hasPermission(Permission.VIEW_AUDIT_LOGS)) {
            this.retrieveAuditLogsDelayed(guild, ActionType.BAN).whenComplete((logs, auditException) -> {
                User moderator = logs == null ? null : logs.stream().filter(e -> Duration.between(e.getTimeCreated(), ZonedDateTime.now(ZoneOffset.UTC)).toSeconds() <= 5).filter(e -> e.getTargetIdLong() == user.getIdLong()).map(AuditLogEntry::getUser).findFirst().orElse(null);
                if (moderator != null) {
                    loggerContext.setModerator(moderator);
                    embed.setDescription(String.format("`%s` has been banned by **%s**", user.getName(), moderator.getAsTag()));
                }
                this.queue(guild, loggers, loggerEvent, loggerContext, embed.build());
            });
        } else {
            this.queue(guild, loggers, loggerEvent, loggerContext, embed.build());
        }
    });
}
Also used : MessageBulkDeleteEvent(net.dv8tion.jda.api.events.message.MessageBulkDeleteEvent) Document(org.bson.Document) PermissionOverrideCreateEvent(net.dv8tion.jda.api.events.guild.override.PermissionOverrideCreateEvent) RoleCreateEvent(net.dv8tion.jda.api.events.role.RoleCreateEvent) StoreChannelDeleteEvent(net.dv8tion.jda.api.events.channel.store.StoreChannelDeleteEvent) Permission(net.dv8tion.jda.api.Permission) ZonedDateTime(java.time.ZonedDateTime) Aggregates(com.mongodb.client.model.Aggregates) EmoteUpdateRolesEvent(net.dv8tion.jda.api.events.emote.update.EmoteUpdateRolesEvent) AuditLogChange(net.dv8tion.jda.api.audit.AuditLogChange) CategoryCreateEvent(net.dv8tion.jda.api.events.channel.category.CategoryCreateEvent) LoggerManager(com.sx4.bot.managers.LoggerManager) ColourUtility(com.sx4.bot.utility.ColourUtility) TextChannelDeleteEvent(net.dv8tion.jda.api.events.channel.text.TextChannelDeleteEvent) EmoteAddedEvent(net.dv8tion.jda.api.events.emote.EmoteAddedEvent) TextChannelUpdateNameEvent(net.dv8tion.jda.api.events.channel.text.update.TextChannelUpdateNameEvent) TextChannelCreateEvent(net.dv8tion.jda.api.events.channel.text.TextChannelCreateEvent) Duration(java.time.Duration) GenericEvent(net.dv8tion.jda.api.events.GenericEvent) ZoneOffset(java.time.ZoneOffset) LoggerContext(com.sx4.bot.entities.management.LoggerContext) LoggerEvent(com.sx4.bot.entities.management.LoggerEvent) AuditLogKey(net.dv8tion.jda.api.audit.AuditLogKey) PermissionOverrideDeleteEvent(net.dv8tion.jda.api.events.guild.override.PermissionOverrideDeleteEvent) EmbedFooter(club.minnced.discord.webhook.send.WebhookEmbed.EmbedFooter) GuildMessageUpdateEvent(net.dv8tion.jda.api.events.message.guild.GuildMessageUpdateEvent) java.util.concurrent(java.util.concurrent) EmoteUpdateNameEvent(net.dv8tion.jda.api.events.emote.update.EmoteUpdateNameEvent) CategoryDeleteEvent(net.dv8tion.jda.api.events.channel.category.CategoryDeleteEvent) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) GuildBanEvent(net.dv8tion.jda.api.events.guild.GuildBanEvent) GuildMemberRemoveEvent(net.dv8tion.jda.api.events.guild.member.GuildMemberRemoveEvent) EventListener(net.dv8tion.jda.api.hooks.EventListener) CategoryUpdateNameEvent(net.dv8tion.jda.api.events.channel.category.update.CategoryUpdateNameEvent) GuildMemberUpdateNicknameEvent(net.dv8tion.jda.api.events.guild.member.update.GuildMemberUpdateNicknameEvent) NotNull(org.jetbrains.annotations.NotNull) RoleUpdateColorEvent(net.dv8tion.jda.api.events.role.update.RoleUpdateColorEvent) ActionType(net.dv8tion.jda.api.audit.ActionType) net.dv8tion.jda.api.entities(net.dv8tion.jda.api.entities) VoiceChannelUpdateNameEvent(net.dv8tion.jda.api.events.channel.voice.update.VoiceChannelUpdateNameEvent) java.util(java.util) JDA(net.dv8tion.jda.api.JDA) EmbedAuthor(club.minnced.discord.webhook.send.WebhookEmbed.EmbedAuthor) GuildMemberRoleRemoveEvent(net.dv8tion.jda.api.events.guild.member.GuildMemberRoleRemoveEvent) RoleUpdatePermissionsEvent(net.dv8tion.jda.api.events.role.update.RoleUpdatePermissionsEvent) VoiceChannelDeleteEvent(net.dv8tion.jda.api.events.channel.voice.VoiceChannelDeleteEvent) Projections(com.mongodb.client.model.Projections) MongoDatabase(com.sx4.bot.database.mongo.MongoDatabase) TLongIntHashMap(gnu.trove.map.hash.TLongIntHashMap) EmbedField(club.minnced.discord.webhook.send.WebhookEmbed.EmbedField) AuditLogEntry(net.dv8tion.jda.api.audit.AuditLogEntry) Filters(com.mongodb.client.model.Filters) GuildMessageDeleteEvent(net.dv8tion.jda.api.events.message.guild.GuildMessageDeleteEvent) Bson(org.bson.conversions.Bson) Pair(net.dv8tion.jda.internal.utils.tuple.Pair) WebhookEmbedBuilder(club.minnced.discord.webhook.send.WebhookEmbedBuilder) Sx4(com.sx4.bot.core.Sx4) GuildMemberRoleAddEvent(net.dv8tion.jda.api.events.guild.member.GuildMemberRoleAddEvent) WebhookEmbed(club.minnced.discord.webhook.send.WebhookEmbed) EmoteRemovedEvent(net.dv8tion.jda.api.events.emote.EmoteRemovedEvent) Operators(com.sx4.bot.database.mongo.model.Operators) Accumulators(com.mongodb.client.model.Accumulators) TLongIntMap(gnu.trove.map.TLongIntMap) VoiceChannelCreateEvent(net.dv8tion.jda.api.events.channel.voice.VoiceChannelCreateEvent) LoggerUtility(com.sx4.bot.utility.LoggerUtility) RoleUpdateNameEvent(net.dv8tion.jda.api.events.role.update.RoleUpdateNameEvent) StoreChannelUpdateNameEvent(net.dv8tion.jda.api.events.channel.store.update.StoreChannelUpdateNameEvent) TLongObjectHashMap(gnu.trove.map.hash.TLongObjectHashMap) OkHttpClient(okhttp3.OkHttpClient) PermissionOverrideUpdateEvent(net.dv8tion.jda.api.events.guild.override.PermissionOverrideUpdateEvent) net.dv8tion.jda.api.events.guild.voice(net.dv8tion.jda.api.events.guild.voice) StoreChannelCreateEvent(net.dv8tion.jda.api.events.channel.store.StoreChannelCreateEvent) GuildMessage(com.sx4.bot.entities.cache.GuildMessage) GuildUnbanEvent(net.dv8tion.jda.api.events.guild.GuildUnbanEvent) TLongObjectMap(gnu.trove.map.TLongObjectMap) RoleDeleteEvent(net.dv8tion.jda.api.events.role.RoleDeleteEvent) ExceptionUtility(com.sx4.bot.utility.ExceptionUtility) StringUtility(com.sx4.bot.utility.StringUtility) GuildMemberJoinEvent(net.dv8tion.jda.api.events.guild.member.GuildMemberJoinEvent) EmbedFooter(club.minnced.discord.webhook.send.WebhookEmbed.EmbedFooter) EmbedAuthor(club.minnced.discord.webhook.send.WebhookEmbed.EmbedAuthor) WebhookEmbedBuilder(club.minnced.discord.webhook.send.WebhookEmbedBuilder) LoggerEvent(com.sx4.bot.entities.management.LoggerEvent) Document(org.bson.Document) LoggerContext(com.sx4.bot.entities.management.LoggerContext)

Example 2 with GuildBanEvent

use of net.dv8tion.jda.api.events.guild.GuildBanEvent in project GeyserDiscordBot by GeyserMC.

the class LogHandler method onGuildBan.

@Override
public void onGuildBan(@NotNull GuildBanEvent event) {
    Guild.Ban ban = event.getGuild().retrieveBan(event.getUser()).complete();
    // Get the ban from the audit log to get the user that created it
    AuditLogEntry banLog = event.getGuild().retrieveAuditLogs().type(ActionType.BAN).stream().filter(auditLogEntry -> auditLogEntry.getTargetIdLong() == ban.getUser().getIdLong()).findFirst().orElse(null);
    GeyserBot.getGeneralThreadPool().schedule(() -> {
        AuditLogEntry newBanLog;
        if (banLog == null) {
            newBanLog = event.getGuild().retrieveAuditLogs().type(ActionType.BAN).stream().filter(auditLogEntry -> auditLogEntry.getTargetIdLong() == ban.getUser().getIdLong()).findFirst().orElse(null);
            // Still null, shouldn't happen but just quietly fail
            if (newBanLog == null) {
                return;
            }
        } else {
            newBanLog = banLog;
        }
        // Don't log bans by the bot (they are handled separately)
        if (newBanLog.getUser().getIdLong() != event.getJDA().getSelfUser().getIdLong()) {
            // Log the change
            int id = GeyserBot.storageManager.addLog(event.getGuild().getMember(newBanLog.getUser()), "ban", event.getUser(), ban.getReason());
            // Send the embed as a reply and to the log
            try {
                ServerSettings.getLogChannel(event.getGuild()).sendMessageEmbeds(new EmbedBuilder().setTitle("Banned user").addField("User", event.getUser().getAsMention(), false).addField("Staff member", newBanLog.getUser().getAsMention(), false).addField("Reason", ban.getReason(), false).setFooter("ID: " + id).setTimestamp(Instant.now()).setColor(BotColors.FAILURE.getColor()).build()).queue();
            } catch (IllegalArgumentException ignored) {
            }
        }
    }, banLog == null ? 5 : 0, TimeUnit.SECONDS);
}
Also used : ActionType(net.dv8tion.jda.api.audit.ActionType) MessageUpdateEvent(net.dv8tion.jda.api.events.message.MessageUpdateEvent) GuildVoiceLeaveEvent(net.dv8tion.jda.api.events.guild.voice.GuildVoiceLeaveEvent) HashMap(java.util.HashMap) Invite(net.dv8tion.jda.api.entities.Invite) AuditLogEntry(net.dv8tion.jda.api.audit.AuditLogEntry) ArrayList(java.util.ArrayList) Guild(net.dv8tion.jda.api.entities.Guild) ServerSettings(org.geysermc.discordbot.storage.ServerSettings) Map(java.util.Map) BotColors(org.geysermc.discordbot.util.BotColors) GeyserBot(org.geysermc.discordbot.GeyserBot) Message(net.dv8tion.jda.api.entities.Message) BotHelpers(org.geysermc.discordbot.util.BotHelpers) ErrorResponseException(net.dv8tion.jda.api.exceptions.ErrorResponseException) GuildVoiceMoveEvent(net.dv8tion.jda.api.events.guild.voice.GuildVoiceMoveEvent) ListenerAdapter(net.dv8tion.jda.api.hooks.ListenerAdapter) TimeFormat(net.dv8tion.jda.api.utils.TimeFormat) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) Instant(java.time.Instant) GuildBanEvent(net.dv8tion.jda.api.events.guild.GuildBanEvent) TimeUnit(java.util.concurrent.TimeUnit) GuildMemberRemoveEvent(net.dv8tion.jda.api.events.guild.member.GuildMemberRemoveEvent) GuildVoiceJoinEvent(net.dv8tion.jda.api.events.guild.voice.GuildVoiceJoinEvent) List(java.util.List) MessageDeleteEvent(net.dv8tion.jda.api.events.message.MessageDeleteEvent) GuildUnbanEvent(net.dv8tion.jda.api.events.guild.GuildUnbanEvent) CacheBuilder(com.google.common.cache.CacheBuilder) Cache(com.google.common.cache.Cache) MessageReceivedEvent(net.dv8tion.jda.api.events.message.MessageReceivedEvent) NotNull(org.jetbrains.annotations.NotNull) GuildMemberJoinEvent(net.dv8tion.jda.api.events.guild.member.GuildMemberJoinEvent) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) AuditLogEntry(net.dv8tion.jda.api.audit.AuditLogEntry) Guild(net.dv8tion.jda.api.entities.Guild)

Example 3 with GuildBanEvent

use of net.dv8tion.jda.api.events.guild.GuildBanEvent in project Gary by help-chat.

the class LoggingHandler method execute.

@Override
protected void execute(GenericEvent event) {
    switch(EventsEnum.fromEvent(event)) {
        case MEMBER_JOIN:
            GuildMemberJoinEvent e = (GuildMemberJoinEvent) event;
            log(EventsEnum.MEMBER_JOIN, e.getJDA(), e.getGuild(), e.getUser());
            break;
        case MEMBER_LEAVE:
            if (!wasBan) {
                GuildMemberLeaveEvent e2 = (GuildMemberLeaveEvent) event;
                log(EventsEnum.MEMBER_LEAVE, e2.getJDA(), e2.getGuild(), e2.getUser());
            } else {
                wasBan = false;
            }
            break;
        case MEMBER_BANNED:
            wasBan = true;
            GuildBanEvent e3 = (GuildBanEvent) event;
            log(EventsEnum.MEMBER_BANNED, e3.getJDA(), e3.getGuild(), e3.getUser());
            break;
        case MESSAGE_EDIT:
            GuildMessageUpdateEvent e5 = (GuildMessageUpdateEvent) event;
            if (!e5.getAuthor().isBot() && StringUtils.equalsIgnoreCase(e5.getChannel().getName(), Constants.CHANNELS)) {
                log(EventsEnum.MESSAGE_EDIT, e5.getJDA(), e5.getGuild(), e5.getAuthor(), e5.getChannel(), e5.getMessage());
            }
            break;
        case MESSAGE_DELETE:
            GuildMessageDeleteEvent e6 = (GuildMessageDeleteEvent) event;
            if ((StringUtils.equalsIgnoreCase(e6.getChannel().getName(), Constants.CHANNELS))) {
                log(EventsEnum.MESSAGE_DELETE, e6.getJDA(), e6.getGuild(), MessageUtils.getAuthor(e6.getMessageIdLong()), e6.getChannel(), e6.getMessageIdLong(), MessageUtils.getMessage(e6.getMessageIdLong()));
            }
            break;
        case MESSAGE_BULK_DELETE:
            MessageBulkDeleteEvent e7 = (MessageBulkDeleteEvent) event;
            if ((StringUtils.equalsIgnoreCase(e7.getChannel().getName(), Constants.CHANNELS))) {
                log(EventsEnum.MESSAGE_BULK_DELETE, e7.getJDA(), e7.getGuild(), e7.getChannel(), e7.getMessageIds());
            }
            break;
        case VOICE_JOIN:
            GuildVoiceJoinEvent e8 = (GuildVoiceJoinEvent) event;
            log(EventsEnum.VOICE_JOIN, e8.getJDA(), e8.getGuild(), e8.getMember().getUser(), e8.getChannelJoined());
            break;
    }
}
Also used : GuildBanEvent(net.dv8tion.jda.api.events.guild.GuildBanEvent) GuildVoiceJoinEvent(net.dv8tion.jda.api.events.guild.voice.GuildVoiceJoinEvent) GuildMemberJoinEvent(net.dv8tion.jda.api.events.guild.member.GuildMemberJoinEvent) GuildMessageDeleteEvent(net.dv8tion.jda.api.events.message.guild.GuildMessageDeleteEvent) MessageBulkDeleteEvent(net.dv8tion.jda.api.events.message.MessageBulkDeleteEvent) GuildMemberLeaveEvent(net.dv8tion.jda.api.events.guild.member.GuildMemberLeaveEvent) GuildMessageUpdateEvent(net.dv8tion.jda.api.events.message.guild.GuildMessageUpdateEvent)

Example 4 with GuildBanEvent

use of net.dv8tion.jda.api.events.guild.GuildBanEvent in project JDA by DV8FromTheWorld.

the class GuildBanHandler method handleInternally.

@Override
protected Long handleInternally(DataObject content) {
    final long id = content.getLong("guild_id");
    if (getJDA().getGuildSetupController().isLocked(id))
        return id;
    DataObject userJson = content.getObject("user");
    GuildImpl guild = (GuildImpl) getJDA().getGuildById(id);
    if (guild == null) {
        getJDA().getEventCache().cache(EventCache.Type.GUILD, id, responseNumber, allContent, this::handle);
        EventCache.LOG.debug("Received Guild Member {} event for a Guild not yet cached.", JDALogger.getLazyString(() -> banned ? "Ban" : "Unban"));
        return null;
    }
    User user = getJDA().getEntityBuilder().createUser(userJson);
    if (banned) {
        getJDA().handleEvent(new GuildBanEvent(getJDA(), responseNumber, guild, user));
    } else {
        getJDA().handleEvent(new GuildUnbanEvent(getJDA(), responseNumber, guild, user));
    }
    return null;
}
Also used : GuildBanEvent(net.dv8tion.jda.api.events.guild.GuildBanEvent) GuildImpl(net.dv8tion.jda.internal.entities.GuildImpl) DataObject(net.dv8tion.jda.api.utils.data.DataObject) User(net.dv8tion.jda.api.entities.User) GuildUnbanEvent(net.dv8tion.jda.api.events.guild.GuildUnbanEvent)

Aggregations

GuildBanEvent (net.dv8tion.jda.api.events.guild.GuildBanEvent)4 GuildUnbanEvent (net.dv8tion.jda.api.events.guild.GuildUnbanEvent)3 GuildMemberJoinEvent (net.dv8tion.jda.api.events.guild.member.GuildMemberJoinEvent)3 Instant (java.time.Instant)2 ActionType (net.dv8tion.jda.api.audit.ActionType)2 AuditLogEntry (net.dv8tion.jda.api.audit.AuditLogEntry)2 GuildMemberRemoveEvent (net.dv8tion.jda.api.events.guild.member.GuildMemberRemoveEvent)2 NotNull (org.jetbrains.annotations.NotNull)2 WebhookEmbed (club.minnced.discord.webhook.send.WebhookEmbed)1 EmbedAuthor (club.minnced.discord.webhook.send.WebhookEmbed.EmbedAuthor)1 EmbedField (club.minnced.discord.webhook.send.WebhookEmbed.EmbedField)1 EmbedFooter (club.minnced.discord.webhook.send.WebhookEmbed.EmbedFooter)1 WebhookEmbedBuilder (club.minnced.discord.webhook.send.WebhookEmbedBuilder)1 Cache (com.google.common.cache.Cache)1 CacheBuilder (com.google.common.cache.CacheBuilder)1 Accumulators (com.mongodb.client.model.Accumulators)1 Aggregates (com.mongodb.client.model.Aggregates)1 Filters (com.mongodb.client.model.Filters)1 Projections (com.mongodb.client.model.Projections)1 Sx4 (com.sx4.bot.core.Sx4)1