Skip to content

Commit

Permalink
Make file previews support forwarded messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Matyrobbrt committed Oct 22, 2024
1 parent 0cc75a8 commit 0b34488
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ groovy-contracts = { module = "org.apache.groovy:groovy-contracts", version.ref

guava = { module = "com.google.guava:guava", version = "30.1.1-jre" }

jda = { module = "net.dv8tion:JDA", version = "5.0.1" }
jda = { module = "net.dv8tion:JDA", version = "5.1.2" }

jdbi-core = { module = "org.jdbi:jdbi3-core", version.ref = "jdbi" }
jdbi-sqlobject = { module = "org.jdbi:jdbi3-sqlobject", version.ref = "jdbi" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import com.google.auto.service.AutoService;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.emoji.Emoji;
import net.dv8tion.jda.api.entities.messages.MessageSnapshot;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.events.message.react.MessageReactionAddEvent;
import net.neoforged.camelot.config.module.FilePreview;
Expand All @@ -18,6 +20,7 @@
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;

@AutoService(CamelotModule.class)
public class FilePreviewModule extends CamelotModule.Base<FilePreview> {
Expand All @@ -42,10 +45,7 @@ public boolean shouldLoad() {
@Override
public void registerListeners(JDABuilder builder) {
builder.addEventListeners(Utils.listenerFor(MessageReceivedEvent.class, event -> {
if (
event.getMessage().getAttachments().stream().anyMatch(it -> config().getExtensions().contains(it.getFileExtension()))
|| hasCodeBlock(event.getMessage().getContentRaw())
) {
if (messageMatches(event.getMessage())) {
event.getMessage().addReaction(EMOJI).queue();
}
}));
Expand All @@ -59,7 +59,10 @@ public void registerListeners(JDABuilder builder) {
try {
// We could check if the message is valid for gisting, but it's not really needed since the only way we'd have reacted is if the message is gistable
final var gist = new GistBuilder(config().getAuth());
for (final var attach : it.getAttachments()) {

final var itr = Stream.concat(it.getAttachments().stream(), it.getMessageSnapshots().stream().flatMap(s -> s.getAttachments().stream())).iterator();
while (itr.hasNext()) {
final var attach = itr.next();
if (config().getExtensions().contains(attach.getFileExtension())) {
try (final var is = URI.create(attach.getProxy().getUrl()).toURL().openStream()) {
gist.file(attach.getFileName(), new String(is.readAllBytes()));
Expand Down Expand Up @@ -109,6 +112,21 @@ public void registerListeners(JDABuilder builder) {
}));
}

private boolean messageMatches(Message message) {
if (message.getAttachments().stream().anyMatch(it -> config().getExtensions().contains(it.getFileExtension())) || hasCodeBlock(message.getContentRaw())) {
return true;
}

if (!message.getMessageSnapshots().isEmpty()) {
for (MessageSnapshot messageSnapshot : message.getMessageSnapshots()) {
if (messageSnapshot.getAttachments().stream().anyMatch(it -> config().getExtensions().contains(it.getFileExtension()))) {
return true;
}
}
}
return false;
}

// A message has a codeblock if it has at least two "counts" of ```, and if the codeblock is at least 10 lines long or 300 characters
private static boolean hasCodeBlock(String msg) {
final int idx = msg.indexOf("```");
Expand Down

0 comments on commit 0b34488

Please sign in to comment.