Skip to content
This repository has been archived by the owner on Jun 19, 2021. It is now read-only.

Commit

Permalink
Pastebin
Browse files Browse the repository at this point in the history
  • Loading branch information
duplexsystem committed Jan 31, 2021
1 parent 8fb2689 commit 546a02f
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ dependencies {
implementation("com.google.guava:guava:30.1-jre")
testImplementation("org.junit.jupiter:junit-jupiter-engine:5.5.2")
testImplementation("org.junit.platform:junit-platform-runner:1.5.2")
implementation("com.mrivanplays:BinClient:1.0.1-SNAPSHOT")
}

group = "org.yatopiamc"
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/org/yatopiamc/bot/YatopiaBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.yatopiamc.bot.mappings.MappingParser;
import org.yatopiamc.bot.mappings.spigot.SpigotMappingHandler;
import org.yatopiamc.bot.mappings.yarn.YarnMappingHandler;
import org.yatopiamc.bot.paste.PasteMessageListener;
import org.yatopiamc.bot.timings.TimingsMessageListener;
import org.yatopiamc.bot.util.NetworkUtils;

Expand All @@ -43,6 +44,7 @@ public class YatopiaBot {

public static final Logger LOGGER = LoggerFactory.getLogger(YatopiaBot.class);
private final TimingsMessageListener timingsMessageListener = new TimingsMessageListener();
private final PasteMessageListener pasteMessageListener = new PasteMessageListener();

public static void main(String[] args) throws LoginException, InterruptedException, IOException {
ConfigInitializer config = new ConfigInitializer();
Expand Down Expand Up @@ -88,6 +90,7 @@ public void start() throws LoginException, InterruptedException, IOException {
.setActivity(Activity.playing("Yatopia.jar"))
.disableCache(CacheFlag.VOICE_STATE, CacheFlag.ACTIVITY)
.addEventListeners(timingsMessageListener)
.addEventListeners(pasteMessageListener)
.build()
.awaitReady();

Expand Down
90 changes: 90 additions & 0 deletions src/main/java/org/yatopiamc/bot/paste/PasteMessageListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package org.yatopiamc.bot.paste;

import com.mrivanplays.binclient.servers.HasteServer;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.yatopiamc.bot.timings.TimingsMessageListener;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.time.Instant;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class PasteMessageListener extends ListenerAdapter {
private static final Logger LOGGER = LoggerFactory.getLogger(TimingsMessageListener.class);
private static final Pattern VERSION = Pattern.compile("\\d+\\.\\d+\\.\\d+");

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

@Override
public void onMessageReceived(@NotNull MessageReceivedEvent event) {
List<Message.Attachment> attachments = event.getMessage().getAttachments();
if (attachments.isEmpty()) return;
CopyOnWriteArraySet<String> pasteIds = new CopyOnWriteArraySet<>();
AtomicInteger waitingPastes = new AtomicInteger(0);
for (Message.Attachment item : attachments) {
if (item.getSize() > 5000000) continue;
CompletableFuture<File> file = item.downloadToFile();
try {
if (isBinaryFile(file.join())) continue;
Stream<String> lines = Files.lines(file.join().toPath());
waitingPastes.getAndIncrement();
pasteServer.createPaste(lines.collect(Collectors.joining(System.lineSeparator()))).async(paste -> {
pasteIds.add(paste);
waitingPastes.getAndDecrement();
}, e -> {LOGGER.warn("Failed to upload to paste service");
waitingPastes.getAndDecrement(); });
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
file.join().delete();
} catch (Exception e) {
LOGGER.warn("Error Deleting File.");
}
}
}
while (waitingPastes.get() > 0) { }
if (pasteIds.isEmpty()) return;
JDA jda = event.getJDA();
final EmbedBuilder embedBuilder = new EmbedBuilder();
final User messageAuthor = event.getAuthor();
embedBuilder.setTitle("Pastebin"); //remove url because people delete timings reports
embedBuilder.setColor(0xffff00);
embedBuilder.setTimestamp(Instant.now());
embedBuilder.setAuthor(messageAuthor.getAsTag(), messageAuthor.getEffectiveAvatarUrl(), messageAuthor.getEffectiveAvatarUrl());
embedBuilder.setFooter("https://yatopiamc.org/", jda.getSelfUser().getEffectiveAvatarUrl());
int i = 0;
for (String paste : pasteIds) {
embedBuilder.addField(String.format("Paste %d", i), pasteServer.retrievePaste(paste).sync().getUrl(), true);
i++;
}

event.getChannel().sendMessage(embedBuilder.build()).queue();
}

// stack copy
Boolean isBinaryFile(File f) throws IOException {
String type = Files.probeContentType(f.toPath());
//type isn't text
if (type == null) {
//type couldn't be determined, assume binary
return true;
} else return !type.startsWith("text");
}

}

0 comments on commit 546a02f

Please sign in to comment.