Skip to content

delete blacklisted attachments from message and replace with webhook #520

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ayyhimself
Copy link

new crazy amazing feature where if someone sends a message with a forbidden attachment then that attachment is removed and the whole message is replaced as a webhook with only the good attachments
works server wide
config in guild config

…e wonderful moderation of a given server marked as blacklisted
@ayyhimself ayyhimself requested a review from a team as a code owner July 11, 2025 18:31
Copy link
Member

@danthe1st danthe1st left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please address the changes I wrote in the review comments.

Also, please use a proper/professional commit message. You can change the commit message of your existing commit using git commit --amend (optionally with -m <message>) and then force-pushing to your branch with git push --force-with-lease.

@@ -54,6 +56,8 @@ public GuildConfig(Guild guild, Path file) {
this.starboardConfig = new StarboardConfig();
this.messageCacheConfig = new MessageCacheConfig();
this.serverLockConfig = new ServerLockConfig();
this.blacklistedMessageExtensions = List.of("application/x-msdos-program", "application/java-archive",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason for the use of MIME types instead of extensions?
If you are using MIME types, why is the variable called "extensions"?

Message message = event.getMessage();
List<Message.Attachment> attachments = message.getAttachments();
List<Message.Attachment> allowedAttachments = new ArrayList<>();
attachments.forEach(attachment -> {
Copy link
Member

@danthe1st danthe1st Jul 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be done nicely using the stream API, for example

List<Message.Attachment> allowedAttachments = attachments
    .stream()
    .filter(this::isAllowedAttachment)
    .toList();

if (tc == null) {
return;
}
long threadId = event.isFromThread() ? event.getChannel().getIdLong() : 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That can be merged with the previous event.isFromThread() check by declaring the variable before (and initializing it with 0).

}
});
if (message.getAttachments().size() != allowedAttachments.size()) {
IWebhookContainer tc = null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tc would imply TextChannel. webhookContainer (or something else if you have a better idea) would be a better name.

long threadId = event.isFromThread() ? event.getChannel().getIdLong() : 0;
WebhookUtil.ensureWebhookExists(
tc,
wh -> WebhookUtil.replaceMemberMessageWithAttachments(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a small embed to the message saying that unwanted attachments were removed.

* @return a {@link CompletableFuture} representing the action of sending
* the message
*/
public static CompletableFuture<ReadonlyMessage> mirrorMessageToWebhookWithAttachments(@NotNull Webhook webhook, @NotNull Message originalMessage, String newMessageContent, long threadId, @Nullable List<LayoutComponent> components, List<Message.Attachment> attachments) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't duplicate code. Instead, you can add an attachments parameter to the existing method and create a new overload with the old signature.

@danthe1st
Copy link
Member

Please also fix the checkstyle errors. We use tabs for indentation.

),
e -> ExceptionLogger.capture(
e,
"Error creating webhook for UnformattedCodeListener"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This message is incorrect here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants