|
| 1 | +package org.yatopiamc.bot.paste; |
| 2 | + |
| 3 | +import com.mrivanplays.binclient.servers.HasteServer; |
| 4 | +import net.dv8tion.jda.api.EmbedBuilder; |
| 5 | +import net.dv8tion.jda.api.JDA; |
| 6 | +import net.dv8tion.jda.api.entities.Message; |
| 7 | +import net.dv8tion.jda.api.entities.User; |
| 8 | +import net.dv8tion.jda.api.events.message.MessageReceivedEvent; |
| 9 | +import net.dv8tion.jda.api.hooks.ListenerAdapter; |
| 10 | +import org.jetbrains.annotations.NotNull; |
| 11 | +import org.slf4j.Logger; |
| 12 | +import org.slf4j.LoggerFactory; |
| 13 | +import org.yatopiamc.bot.timings.TimingsMessageListener; |
| 14 | + |
| 15 | +import java.io.File; |
| 16 | +import java.io.IOException; |
| 17 | +import java.nio.file.Files; |
| 18 | +import java.time.Instant; |
| 19 | +import java.util.List; |
| 20 | +import java.util.concurrent.CompletableFuture; |
| 21 | +import java.util.concurrent.CopyOnWriteArraySet; |
| 22 | +import java.util.concurrent.atomic.AtomicInteger; |
| 23 | +import java.util.regex.Pattern; |
| 24 | +import java.util.stream.Collectors; |
| 25 | +import java.util.stream.Stream; |
| 26 | + |
| 27 | +public class PasteMessageListener extends ListenerAdapter { |
| 28 | + private static final Logger LOGGER = LoggerFactory.getLogger(TimingsMessageListener.class); |
| 29 | + private static final Pattern VERSION = Pattern.compile("\\d+\\.\\d+\\.\\d+"); |
| 30 | + |
| 31 | + private static final HasteServer pasteServer = new HasteServer("https://bin.birdflop.com/"); //Might change this in the future but it works for now normal haste bin seems to not work |
| 32 | + |
| 33 | + @Override |
| 34 | + public void onMessageReceived(@NotNull MessageReceivedEvent event) { |
| 35 | + List<Message.Attachment> attachments = event.getMessage().getAttachments(); |
| 36 | + if (attachments.isEmpty()) return; |
| 37 | + CopyOnWriteArraySet<String> pasteIds = new CopyOnWriteArraySet<>(); |
| 38 | + AtomicInteger waitingPastes = new AtomicInteger(0); |
| 39 | + for (Message.Attachment item : attachments) { |
| 40 | + if (item.getSize() > 5000000) continue; |
| 41 | + CompletableFuture<File> file = item.downloadToFile(); |
| 42 | + try { |
| 43 | + if (isBinaryFile(file.join())) continue; |
| 44 | + Stream<String> lines = Files.lines(file.join().toPath()); |
| 45 | + waitingPastes.getAndIncrement(); |
| 46 | + pasteServer.createPaste(lines.collect(Collectors.joining(System.lineSeparator()))).async(paste -> { |
| 47 | + pasteIds.add(paste); |
| 48 | + waitingPastes.getAndDecrement(); |
| 49 | + }, e -> {LOGGER.warn("Failed to upload to paste service"); |
| 50 | + waitingPastes.getAndDecrement(); }); |
| 51 | + } catch (IOException e) { |
| 52 | + e.printStackTrace(); |
| 53 | + } finally { |
| 54 | + try { |
| 55 | + file.join().delete(); |
| 56 | + } catch (Exception e) { |
| 57 | + LOGGER.warn("Error Deleting File."); |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + while (waitingPastes.get() > 0) { } |
| 62 | + if (pasteIds.isEmpty()) return; |
| 63 | + JDA jda = event.getJDA(); |
| 64 | + final EmbedBuilder embedBuilder = new EmbedBuilder(); |
| 65 | + final User messageAuthor = event.getAuthor(); |
| 66 | + embedBuilder.setTitle("Pastebin"); //remove url because people delete timings reports |
| 67 | + embedBuilder.setColor(0xffff00); |
| 68 | + embedBuilder.setTimestamp(Instant.now()); |
| 69 | + embedBuilder.setAuthor(messageAuthor.getAsTag(), messageAuthor.getEffectiveAvatarUrl(), messageAuthor.getEffectiveAvatarUrl()); |
| 70 | + embedBuilder.setFooter("https://yatopiamc.org/", jda.getSelfUser().getEffectiveAvatarUrl()); |
| 71 | + int i = 0; |
| 72 | + for (String paste : pasteIds) { |
| 73 | + embedBuilder.addField(String.format("Paste %d", i), pasteServer.retrievePaste(paste).sync().getUrl(), true); |
| 74 | + i++; |
| 75 | + } |
| 76 | + |
| 77 | + event.getChannel().sendMessage(embedBuilder.build()).queue(); |
| 78 | + } |
| 79 | + |
| 80 | + // stack copy |
| 81 | + Boolean isBinaryFile(File f) throws IOException { |
| 82 | + String type = Files.probeContentType(f.toPath()); |
| 83 | + //type isn't text |
| 84 | + if (type == null) { |
| 85 | + //type couldn't be determined, assume binary |
| 86 | + return true; |
| 87 | + } else return !type.startsWith("text"); |
| 88 | + } |
| 89 | + |
| 90 | +} |
0 commit comments