|  | 
|  | 1 | +package org.togetherjava.tjbot.commands.moderation.attachment; | 
|  | 2 | + | 
|  | 3 | +import net.dv8tion.jda.api.EmbedBuilder; | 
|  | 4 | +import net.dv8tion.jda.api.MessageBuilder; | 
|  | 5 | +import net.dv8tion.jda.api.entities.Message; | 
|  | 6 | +import net.dv8tion.jda.api.entities.MessageEmbed; | 
|  | 7 | +import net.dv8tion.jda.api.events.message.MessageReceivedEvent; | 
|  | 8 | +import net.dv8tion.jda.api.requests.RestAction; | 
|  | 9 | +import org.togetherjava.tjbot.commands.MessageReceiverAdapter; | 
|  | 10 | +import org.togetherjava.tjbot.config.Config; | 
|  | 11 | +import org.togetherjava.tjbot.moderation.ModAuditLogWriter; | 
|  | 12 | + | 
|  | 13 | +import javax.annotation.Nonnull; | 
|  | 14 | +import java.awt.*; | 
|  | 15 | +import java.util.List; | 
|  | 16 | +import java.util.Locale; | 
|  | 17 | +import java.util.regex.Pattern; | 
|  | 18 | + | 
|  | 19 | +/** | 
|  | 20 | + * Reacts to blacklisted attachments being posted, upon which they are deleted. | 
|  | 21 | + */ | 
|  | 22 | +public final class BlacklistedAttachmentListener extends MessageReceiverAdapter { | 
|  | 23 | +    private final ModAuditLogWriter modAuditLogWriter; | 
|  | 24 | +    private final List<String> blacklistedFileExtensions; | 
|  | 25 | + | 
|  | 26 | +    /** | 
|  | 27 | +     * Creates a AttachmentListener to receive all message sent in any channel. | 
|  | 28 | +     * | 
|  | 29 | +     * @param config to find the blacklisted media attachments | 
|  | 30 | +     * @param modAuditLogWriter to inform the mods about the suspicious attachment | 
|  | 31 | +     */ | 
|  | 32 | +    public BlacklistedAttachmentListener(Config config, ModAuditLogWriter modAuditLogWriter) { | 
|  | 33 | +        super(Pattern.compile(".*")); | 
|  | 34 | +        this.modAuditLogWriter = modAuditLogWriter; | 
|  | 35 | +        this.blacklistedFileExtensions = config.getBlacklistedFileExtensions(); | 
|  | 36 | +    } | 
|  | 37 | + | 
|  | 38 | +    @Override | 
|  | 39 | +    public void onMessageReceived(MessageReceivedEvent event) { | 
|  | 40 | +        if (event.getAuthor().isBot() || event.isWebhookMessage()) { | 
|  | 41 | +            return; | 
|  | 42 | +        } | 
|  | 43 | +        if (doesMessageContainBlacklistedContent(event.getMessage())) { | 
|  | 44 | +            handleBadMessage(event.getMessage()); | 
|  | 45 | +        } | 
|  | 46 | +    } | 
|  | 47 | + | 
|  | 48 | +    private void handleBadMessage(Message message) { | 
|  | 49 | +        message.delete().flatMap(any -> dmUser(message)).queue(any -> warnMods(message)); | 
|  | 50 | +    } | 
|  | 51 | + | 
|  | 52 | +    @Nonnull | 
|  | 53 | +    private RestAction<Message> dmUser(Message message) { | 
|  | 54 | +        Message dmMessage = createDmMessage(message); | 
|  | 55 | +        return message.getAuthor() | 
|  | 56 | +            .openPrivateChannel() | 
|  | 57 | +            .flatMap(privateChannel -> privateChannel.sendMessage(dmMessage)); | 
|  | 58 | +    } | 
|  | 59 | + | 
|  | 60 | +    @Nonnull | 
|  | 61 | +    private Message createDmMessage(Message originalMessage) { | 
|  | 62 | +        String contentRaw = originalMessage.getContentRaw(); | 
|  | 63 | +        String blacklistedAttachments = | 
|  | 64 | +                String.join(", ", getBlacklistedAttachmentsFromMessage(originalMessage)); | 
|  | 65 | + | 
|  | 66 | +        String dmMessageContent = | 
|  | 67 | +                """ | 
|  | 68 | +                        Hey there, you posted a message containing a blacklisted file attachment: %s. | 
|  | 69 | +                        We had to delete your message for security reasons. | 
|  | 70 | +
 | 
|  | 71 | +                        Feel free to repost your message without, or with a different file instead. Sorry for any inconvenience caused by this 🙇️ | 
|  | 72 | +                        """ | 
|  | 73 | +                    .formatted(blacklistedAttachments); | 
|  | 74 | + | 
|  | 75 | +        // No embed needed if there was no message from the user | 
|  | 76 | +        if (contentRaw.isEmpty()) { | 
|  | 77 | +            return new MessageBuilder(dmMessageContent).build(); | 
|  | 78 | +        } | 
|  | 79 | +        return createBaseResponse(contentRaw, dmMessageContent); | 
|  | 80 | +    } | 
|  | 81 | + | 
|  | 82 | +    @Nonnull | 
|  | 83 | +    private Message createBaseResponse(String originalMessageContent, String dmMessageContent) { | 
|  | 84 | +        MessageEmbed originalMessageEmbed = | 
|  | 85 | +                new EmbedBuilder().setDescription(originalMessageContent) | 
|  | 86 | +                    .setColor(Color.ORANGE) | 
|  | 87 | +                    .build(); | 
|  | 88 | +        return new MessageBuilder(dmMessageContent).setEmbeds(originalMessageEmbed).build(); | 
|  | 89 | +    } | 
|  | 90 | + | 
|  | 91 | +    @Nonnull | 
|  | 92 | +    private List<String> getBlacklistedAttachmentsFromMessage(Message originalMessage) { | 
|  | 93 | +        return originalMessage.getAttachments() | 
|  | 94 | +            .stream() | 
|  | 95 | +            .filter(attachment -> blacklistedFileExtensions | 
|  | 96 | +                .contains(attachment.getFileExtension().toLowerCase(Locale.US))) | 
|  | 97 | +            .map(Message.Attachment::getFileName) | 
|  | 98 | +            .toList(); | 
|  | 99 | +    } | 
|  | 100 | + | 
|  | 101 | +    private boolean doesMessageContainBlacklistedContent(Message message) { | 
|  | 102 | +        return message.getAttachments() | 
|  | 103 | +            .stream() | 
|  | 104 | +            .anyMatch(attachment -> blacklistedFileExtensions | 
|  | 105 | +                .contains(attachment.getFileExtension().toLowerCase(Locale.US))); | 
|  | 106 | +    } | 
|  | 107 | + | 
|  | 108 | +    private void warnMods(Message sentUserMessage) { | 
|  | 109 | +        String blacklistedAttachmentsFromMessage = | 
|  | 110 | +                String.join(", ", getBlacklistedAttachmentsFromMessage(sentUserMessage)); | 
|  | 111 | + | 
|  | 112 | +        modAuditLogWriter.write( | 
|  | 113 | +                "Message with blacklisted content detected: %s" | 
|  | 114 | +                    .formatted(blacklistedAttachmentsFromMessage), | 
|  | 115 | +                "Sent Message: %s".formatted(sentUserMessage), sentUserMessage.getAuthor(), | 
|  | 116 | +                sentUserMessage.getTimeCreated(), sentUserMessage.getGuild()); | 
|  | 117 | +    } | 
|  | 118 | +} | 
0 commit comments