Search in sources :

Example 31 with AuditLogEntry

use of net.dv8tion.jda.api.audit.AuditLogEntry in project Sx4 by sx4-discord-bot.

the class ServerLogHandler method onGuildJoin.

public void onGuildJoin(GuildJoinEvent event) {
    ShardManager manager = event.getJDA().getShardManager();
    Member owner = event.getGuild().getOwner();
    WebhookEmbedBuilder embed = new WebhookEmbedBuilder().setColor(this.bot.getConfig().getGreen()).setThumbnailUrl(event.getGuild().getIconUrl()).setTimestamp(Instant.now()).setDescription(String.format("I am now in %,d servers and connected to %,d users", manager.getGuildCache().size(), manager.getUserCache().size())).setAuthor(new WebhookEmbed.EmbedAuthor("Joined Server!", event.getJDA().getSelfUser().getEffectiveAvatarUrl(), null)).addField(new WebhookEmbed.EmbedField(true, "Server Name", event.getGuild().getName())).addField(new WebhookEmbed.EmbedField(true, "Server ID", event.getGuild().getId())).addField(new WebhookEmbed.EmbedField(true, "Server Owner", (owner == null ? "Anonymous#0000" : owner.getUser().getAsTag()) + "\n" + event.getGuild().getOwnerId())).addField(new WebhookEmbed.EmbedField(true, "Server Members", String.format("%,d member%s", event.getGuild().getMemberCount(), event.getGuild().getMemberCount() == 1 ? "" : "s")));
    this.webhook.send(embed.build());
    if (event.getGuild().getSelfMember().hasPermission(Permission.VIEW_AUDIT_LOGS)) {
        event.getGuild().retrieveAuditLogs().type(ActionType.BOT_ADD).queueAfter(500, TimeUnit.MILLISECONDS, audits -> {
            AuditLogEntry audit = audits.stream().filter(auditLog -> auditLog.getTargetIdLong() == event.getJDA().getSelfUser().getIdLong()).filter(auditLog -> Duration.between(auditLog.getTimeCreated(), ZonedDateTime.now(ZoneOffset.UTC)).toSeconds() <= 5).findFirst().orElse(null);
            if (audit != null) {
                audit.getUser().openPrivateChannel().flatMap(channel -> channel.sendMessage("Thanks for adding me to your server!\nThe default prefix is `s?` however if that doesn't work use `@Sx4#1617 prefix` to check your current prefixes\nAll my info and commands can be found in `s?help`\nIf you need any help feel free to join the support server: https://discord.gg/PqJNcfB")).queue();
            }
        });
    }
}
Also used : ActionType(net.dv8tion.jda.api.audit.ActionType) Document(org.bson.Document) WebhookClient(club.minnced.discord.webhook.WebhookClient) GuildJoinEvent(net.dv8tion.jda.api.events.guild.GuildJoinEvent) Permission(net.dv8tion.jda.api.Permission) ZonedDateTime(java.time.ZonedDateTime) Member(net.dv8tion.jda.api.entities.Member) AuditLogEntry(net.dv8tion.jda.api.audit.AuditLogEntry) RequestBody(okhttp3.RequestBody) WebhookEmbedBuilder(club.minnced.discord.webhook.send.WebhookEmbedBuilder) Sx4(com.sx4.bot.core.Sx4) WebhookEmbed(club.minnced.discord.webhook.send.WebhookEmbed) TimeUtility(com.sx4.bot.utility.TimeUtility) Duration(java.time.Duration) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) GenericEvent(net.dv8tion.jda.api.events.GenericEvent) ZoneOffset(java.time.ZoneOffset) MediaType(okhttp3.MediaType) Request(okhttp3.Request) ShardManager(net.dv8tion.jda.api.sharding.ShardManager) HttpCallback(com.sx4.bot.http.HttpCallback) Instant(java.time.Instant) GuildLeaveEvent(net.dv8tion.jda.api.events.guild.GuildLeaveEvent) Executors(java.util.concurrent.Executors) TimeUnit(java.util.concurrent.TimeUnit) EventListener(net.dv8tion.jda.api.hooks.EventListener) NotNull(org.jetbrains.annotations.NotNull) WebhookClientBuilder(club.minnced.discord.webhook.WebhookClientBuilder) WebhookEmbed(club.minnced.discord.webhook.send.WebhookEmbed) AuditLogEntry(net.dv8tion.jda.api.audit.AuditLogEntry) WebhookEmbedBuilder(club.minnced.discord.webhook.send.WebhookEmbedBuilder) ShardManager(net.dv8tion.jda.api.sharding.ShardManager) Member(net.dv8tion.jda.api.entities.Member)

Example 32 with AuditLogEntry

use of net.dv8tion.jda.api.audit.AuditLogEntry in project Sx4 by sx4-discord-bot.

the class LoggerHandler method onGuildVoiceGuildMute.

public void onGuildVoiceGuildMute(GuildVoiceGuildMuteEvent event) {
    Guild guild = event.getGuild();
    Member member = event.getMember();
    User user = member.getUser();
    GuildVoiceState voiceState = event.getVoiceState();
    VoiceChannel channel = voiceState.getChannel();
    boolean muted = voiceState.isGuildMuted();
    LoggerEvent loggerEvent = muted ? LoggerEvent.MEMBER_SERVER_VOICE_MUTE : LoggerEvent.MEMBER_SERVER_VOICE_UNMUTE;
    LoggerContext loggerContext = new LoggerContext().setUser(user).setChannel(channel == null ? 0L : channel.getIdLong());
    WebhookEmbedBuilder embed = new WebhookEmbedBuilder();
    embed.setDescription(String.format("`%s` has been %s", member.getEffectiveName(), muted ? "muted" : "unmuted"));
    embed.setTimestamp(Instant.now());
    embed.setAuthor(new EmbedAuthor(member.getUser().getAsTag(), member.getUser().getEffectiveAvatarUrl(), null));
    embed.setFooter(new EmbedFooter(String.format("User ID: %s", user.getId()), null));
    embed.setColor(muted ? this.bot.getConfig().getRed() : this.bot.getConfig().getGreen());
    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.MEMBER_UPDATE).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() == member.getUser().getIdLong()).filter(e -> e.getChangeByKey(AuditLogKey.MEMBER_MUTE) != null).map(AuditLogEntry::getUser).findFirst().orElse(null);
                if (moderator != null) {
                    loggerContext.setModerator(moderator);
                    embed.setDescription(String.format("`%s` has been %s by **%s**", member.getEffectiveName(), muted ? "muted" : "unmuted", 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) LoggerEvent(com.sx4.bot.entities.management.LoggerEvent) Document(org.bson.Document) LoggerContext(com.sx4.bot.entities.management.LoggerContext) EmbedFooter(club.minnced.discord.webhook.send.WebhookEmbed.EmbedFooter) EmbedAuthor(club.minnced.discord.webhook.send.WebhookEmbed.EmbedAuthor) WebhookEmbedBuilder(club.minnced.discord.webhook.send.WebhookEmbedBuilder)

Aggregations

AuditLogEntry (net.dv8tion.jda.api.audit.AuditLogEntry)32 ActionType (net.dv8tion.jda.api.audit.ActionType)30 Instant (java.time.Instant)29 NotNull (org.jetbrains.annotations.NotNull)29 WebhookEmbed (club.minnced.discord.webhook.send.WebhookEmbed)28 WebhookEmbedBuilder (club.minnced.discord.webhook.send.WebhookEmbedBuilder)28 Sx4 (com.sx4.bot.core.Sx4)28 TLongObjectHashMap (gnu.trove.map.hash.TLongObjectHashMap)28 Duration (java.time.Duration)28 ZoneOffset (java.time.ZoneOffset)28 ZonedDateTime (java.time.ZonedDateTime)28 Permission (net.dv8tion.jda.api.Permission)28 EmbedAuthor (club.minnced.discord.webhook.send.WebhookEmbed.EmbedAuthor)27 EmbedField (club.minnced.discord.webhook.send.WebhookEmbed.EmbedField)27 EmbedFooter (club.minnced.discord.webhook.send.WebhookEmbed.EmbedFooter)27 Accumulators (com.mongodb.client.model.Accumulators)27 Aggregates (com.mongodb.client.model.Aggregates)27 Filters (com.mongodb.client.model.Filters)27 Projections (com.mongodb.client.model.Projections)27 MongoDatabase (com.sx4.bot.database.mongo.MongoDatabase)27