Search in sources :

Example 1 with UnknownUser

use of ml.duncte123.skybot.objects.user.UnknownUser in project SkyBot by duncte123.

the class MessageListener method onMessageBulkDelete.

@SuppressWarnings("PMD.UseConcurrentHashMap")
protected void onMessageBulkDelete(final MessageBulkDeleteEvent event) {
    this.handlerThread.submit(() -> {
        try {
            final DunctebotGuild guild = new DunctebotGuild(event.getGuild(), variables);
            if (!guild.getSettings().isMessageLogging()) {
                // just delete the message here as we don't want to keep it around
                this.redis.deleteMessages(event.getMessageIds());
                return;
            }
            final List<MessageData> dataList = this.redis.getAndDeleteMessages(event.getMessageIds());
            final StringBuilder builder = new StringBuilder();
            // temporarily store the users to prevent spamming discord for the data
            final Map<Long, User> tmpUsers = new HashMap<>();
            final JDA jda = event.getJDA();
            // reverse the list to preserve the correct order
            Collections.reverse(dataList);
            for (final MessageData data : dataList) {
                final long authorId = data.getAuthorId();
                final Consumer<User> userConsumer = (user) -> {
                    builder.append('[').append(data.getCratedAt().format(DateTimeFormatter.RFC_1123_DATE_TIME)).append("] (").append(user.getAsTag()).append(" - ").append(user.getIdLong()).append(") [").append(data.getMessageId()).append("]: ").append(data.getContent()).append('\n');
                };
                if (tmpUsers.containsKey(authorId)) {
                    userConsumer.accept(tmpUsers.get(authorId));
                } else {
                    // try to fetch the user since we don't cache them
                    // calls are sequential making sure the messages are still in order
                    jda.retrieveUserById(authorId).queue((user) -> {
                        tmpUsers.put(authorId, user);
                        userConsumer.accept(user);
                    }, (error) -> userConsumer.accept(new UnknownUser(authorId)));
                }
            }
            final TextChannel channel = event.getChannel();
            final EmbedBuilder embed = EmbedUtils.embedField("Bulk Delete", "Bulk deleted messages from <#%s> are available in the attached file.".formatted(channel.getIdLong())).setColor(0xE67E22).setTimestamp(Instant.now());
            modLog(new MessageConfig.Builder().addEmbed(true, embed).setActionConfig((action) -> action.addFile(builder.toString().getBytes(), "bulk_delete_%s.txt".formatted(System.currentTimeMillis()))), guild);
        } catch (Exception e) {
            LOGGER.error("Exception on message bulk delete", e);
        }
    });
}
Also used : MessageData(ml.duncte123.skybot.objects.discord.MessageData) MessageBulkDeleteEvent(net.dv8tion.jda.api.events.message.MessageBulkDeleteEvent) EmbedUtils(me.duncte123.botcommons.messaging.EmbedUtils) StringUtils(me.duncte123.botcommons.StringUtils) Permission(net.dv8tion.jda.api.Permission) CommandCategory(ml.duncte123.skybot.objects.command.CommandCategory) PerspectiveApi(ml.duncte123.skybot.utils.PerspectiveApi) UNKNOWN_MESSAGE(net.dv8tion.jda.api.requests.ErrorResponse.UNKNOWN_MESSAGE) SpamFilter(ml.duncte123.skybot.utils.SpamFilter) Matcher(java.util.regex.Matcher) GuildSettingsUtils(ml.duncte123.skybot.utils.GuildSettingsUtils) GuildMessageReceivedEvent(net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent) GuildSetting(com.dunctebot.models.settings.GuildSetting) GuildMessageUpdateEvent(net.dv8tion.jda.api.events.message.guild.GuildMessageUpdateEvent) Variables(ml.duncte123.skybot.Variables) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) UnknownUser(ml.duncte123.skybot.objects.user.UnknownUser) Pattern(java.util.regex.Pattern) MISSING_PERMISSIONS(net.dv8tion.jda.api.requests.ErrorResponse.MISSING_PERMISSIONS) ICommand(ml.duncte123.skybot.objects.command.ICommand) net.dv8tion.jda.api.entities(net.dv8tion.jda.api.entities) BotCommons(me.duncte123.botcommons.BotCommons) java.util(java.util) JDA(net.dv8tion.jda.api.JDA) MarkdownSanitizer(net.dv8tion.jda.api.utils.MarkdownSanitizer) CustomCommand(ml.duncte123.skybot.objects.command.custom.CustomCommand) GuildMessageDeleteEvent(net.dv8tion.jda.api.events.message.guild.GuildMessageDeleteEvent) MessageUtils(me.duncte123.botcommons.messaging.MessageUtils) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Sentry(io.sentry.Sentry) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) ErrorHandler(net.dv8tion.jda.api.exceptions.ErrorHandler) GenericGuildMessageEvent(net.dv8tion.jda.api.events.message.guild.GenericGuildMessageEvent) CommandUtils.isGuildPatron(ml.duncte123.skybot.utils.CommandUtils.isGuildPatron) ShardManager(net.dv8tion.jda.api.sharding.ShardManager) MessageUtils.sendMsg(me.duncte123.botcommons.messaging.MessageUtils.sendMsg) CommandContext(ml.duncte123.skybot.objects.command.CommandContext) CommandUtils.isDev(ml.duncte123.skybot.utils.CommandUtils.isDev) ModerationUtils.modLog(ml.duncte123.skybot.utils.ModerationUtils.modLog) CommandManager(ml.duncte123.skybot.CommandManager) DunctebotGuild(ml.duncte123.skybot.entities.jda.DunctebotGuild) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) AirUtils.setJDAContext(ml.duncte123.skybot.utils.AirUtils.setJDAContext) Triple(kotlin.Triple) Settings(ml.duncte123.skybot.Settings) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) MessageConfig(me.duncte123.botcommons.messaging.MessageConfig) RedisConnection(ml.duncte123.skybot.database.RedisConnection) DateTimeFormatter(java.time.format.DateTimeFormatter) UnknownUser(ml.duncte123.skybot.objects.user.UnknownUser) UnknownUser(ml.duncte123.skybot.objects.user.UnknownUser) MessageData(ml.duncte123.skybot.objects.discord.MessageData) JDA(net.dv8tion.jda.api.JDA) DunctebotGuild(ml.duncte123.skybot.entities.jda.DunctebotGuild) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) MessageConfig(me.duncte123.botcommons.messaging.MessageConfig)

Example 2 with UnknownUser

use of ml.duncte123.skybot.objects.user.UnknownUser in project SkyBot by DuncteBot.

the class MessageListener method onMessageBulkDelete.

@SuppressWarnings("PMD.UseConcurrentHashMap")
protected void onMessageBulkDelete(final MessageBulkDeleteEvent event) {
    this.handlerThread.submit(() -> {
        try {
            final DunctebotGuild guild = new DunctebotGuild(event.getGuild(), variables);
            if (!guild.getSettings().isMessageLogging()) {
                // just delete the message here as we don't want to keep it around
                this.redis.deleteMessages(event.getMessageIds());
                return;
            }
            final List<MessageData> dataList = this.redis.getAndDeleteMessages(event.getMessageIds());
            final StringBuilder builder = new StringBuilder();
            // temporarily store the users to prevent spamming discord for the data
            final Map<Long, User> tmpUsers = new HashMap<>();
            final JDA jda = event.getJDA();
            // reverse the list to preserve the correct order
            Collections.reverse(dataList);
            for (final MessageData data : dataList) {
                final long authorId = data.getAuthorId();
                final Consumer<User> userConsumer = (user) -> {
                    builder.append('[').append(data.getCratedAt().format(DateTimeFormatter.RFC_1123_DATE_TIME)).append("] (").append(user.getAsTag()).append(" - ").append(user.getIdLong()).append(") [").append(data.getMessageId()).append("]: ").append(data.getContent()).append('\n');
                };
                if (tmpUsers.containsKey(authorId)) {
                    userConsumer.accept(tmpUsers.get(authorId));
                } else {
                    // try to fetch the user since we don't cache them
                    // calls are sequential making sure the messages are still in order
                    jda.retrieveUserById(authorId).queue((user) -> {
                        tmpUsers.put(authorId, user);
                        userConsumer.accept(user);
                    }, (error) -> userConsumer.accept(new UnknownUser(authorId)));
                }
            }
            final TextChannel channel = event.getChannel();
            final EmbedBuilder embed = EmbedUtils.embedField("Bulk Delete", "Bulk deleted messages from <#%s> are available in the attached file.".formatted(channel.getIdLong())).setColor(0xE67E22).setTimestamp(Instant.now());
            modLog(new MessageConfig.Builder().addEmbed(true, embed).setActionConfig((action) -> action.addFile(builder.toString().getBytes(), "bulk_delete_%s.txt".formatted(System.currentTimeMillis()))), guild);
        } catch (Exception e) {
            LOGGER.error("Exception on message bulk delete", e);
        }
    });
}
Also used : MessageData(ml.duncte123.skybot.objects.discord.MessageData) MessageBulkDeleteEvent(net.dv8tion.jda.api.events.message.MessageBulkDeleteEvent) EmbedUtils(me.duncte123.botcommons.messaging.EmbedUtils) StringUtils(me.duncte123.botcommons.StringUtils) Permission(net.dv8tion.jda.api.Permission) CommandCategory(ml.duncte123.skybot.objects.command.CommandCategory) PerspectiveApi(ml.duncte123.skybot.utils.PerspectiveApi) UNKNOWN_MESSAGE(net.dv8tion.jda.api.requests.ErrorResponse.UNKNOWN_MESSAGE) SpamFilter(ml.duncte123.skybot.utils.SpamFilter) Matcher(java.util.regex.Matcher) GuildSettingsUtils(ml.duncte123.skybot.utils.GuildSettingsUtils) GuildMessageReceivedEvent(net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent) GuildSetting(com.dunctebot.models.settings.GuildSetting) GuildMessageUpdateEvent(net.dv8tion.jda.api.events.message.guild.GuildMessageUpdateEvent) Variables(ml.duncte123.skybot.Variables) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) UnknownUser(ml.duncte123.skybot.objects.user.UnknownUser) Pattern(java.util.regex.Pattern) MISSING_PERMISSIONS(net.dv8tion.jda.api.requests.ErrorResponse.MISSING_PERMISSIONS) ICommand(ml.duncte123.skybot.objects.command.ICommand) net.dv8tion.jda.api.entities(net.dv8tion.jda.api.entities) BotCommons(me.duncte123.botcommons.BotCommons) java.util(java.util) JDA(net.dv8tion.jda.api.JDA) MarkdownSanitizer(net.dv8tion.jda.api.utils.MarkdownSanitizer) CustomCommand(ml.duncte123.skybot.objects.command.custom.CustomCommand) GuildMessageDeleteEvent(net.dv8tion.jda.api.events.message.guild.GuildMessageDeleteEvent) MessageUtils(me.duncte123.botcommons.messaging.MessageUtils) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Sentry(io.sentry.Sentry) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) ErrorHandler(net.dv8tion.jda.api.exceptions.ErrorHandler) GenericGuildMessageEvent(net.dv8tion.jda.api.events.message.guild.GenericGuildMessageEvent) CommandUtils.isGuildPatron(ml.duncte123.skybot.utils.CommandUtils.isGuildPatron) ShardManager(net.dv8tion.jda.api.sharding.ShardManager) MessageUtils.sendMsg(me.duncte123.botcommons.messaging.MessageUtils.sendMsg) CommandContext(ml.duncte123.skybot.objects.command.CommandContext) CommandUtils.isDev(ml.duncte123.skybot.utils.CommandUtils.isDev) ModerationUtils.modLog(ml.duncte123.skybot.utils.ModerationUtils.modLog) CommandManager(ml.duncte123.skybot.CommandManager) DunctebotGuild(ml.duncte123.skybot.entities.jda.DunctebotGuild) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) AirUtils.setJDAContext(ml.duncte123.skybot.utils.AirUtils.setJDAContext) Triple(kotlin.Triple) Settings(ml.duncte123.skybot.Settings) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) MessageConfig(me.duncte123.botcommons.messaging.MessageConfig) RedisConnection(ml.duncte123.skybot.database.RedisConnection) DateTimeFormatter(java.time.format.DateTimeFormatter) UnknownUser(ml.duncte123.skybot.objects.user.UnknownUser) UnknownUser(ml.duncte123.skybot.objects.user.UnknownUser) MessageData(ml.duncte123.skybot.objects.discord.MessageData) JDA(net.dv8tion.jda.api.JDA) DunctebotGuild(ml.duncte123.skybot.entities.jda.DunctebotGuild) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) MessageConfig(me.duncte123.botcommons.messaging.MessageConfig)

Aggregations

GuildSetting (com.dunctebot.models.settings.GuildSetting)2 Sentry (io.sentry.Sentry)2 Instant (java.time.Instant)2 DateTimeFormatter (java.time.format.DateTimeFormatter)2 java.util (java.util)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 Collectors (java.util.stream.Collectors)2 Nonnull (javax.annotation.Nonnull)2 Nullable (javax.annotation.Nullable)2 Triple (kotlin.Triple)2 BotCommons (me.duncte123.botcommons.BotCommons)2 StringUtils (me.duncte123.botcommons.StringUtils)2 EmbedUtils (me.duncte123.botcommons.messaging.EmbedUtils)2 MessageConfig (me.duncte123.botcommons.messaging.MessageConfig)2 MessageUtils (me.duncte123.botcommons.messaging.MessageUtils)2