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

Commit 546a02f

Browse files
committed
Pastebin
1 parent 8fb2689 commit 546a02f

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

build.gradle.kts

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ dependencies {
3232
implementation("com.google.guava:guava:30.1-jre")
3333
testImplementation("org.junit.jupiter:junit-jupiter-engine:5.5.2")
3434
testImplementation("org.junit.platform:junit-platform-runner:1.5.2")
35+
implementation("com.mrivanplays:BinClient:1.0.1-SNAPSHOT")
3536
}
3637

3738
group = "org.yatopiamc"

src/main/java/org/yatopiamc/bot/YatopiaBot.java

+3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.yatopiamc.bot.mappings.MappingParser;
2727
import org.yatopiamc.bot.mappings.spigot.SpigotMappingHandler;
2828
import org.yatopiamc.bot.mappings.yarn.YarnMappingHandler;
29+
import org.yatopiamc.bot.paste.PasteMessageListener;
2930
import org.yatopiamc.bot.timings.TimingsMessageListener;
3031
import org.yatopiamc.bot.util.NetworkUtils;
3132

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

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

4749
public static void main(String[] args) throws LoginException, InterruptedException, IOException {
4850
ConfigInitializer config = new ConfigInitializer();
@@ -88,6 +90,7 @@ public void start() throws LoginException, InterruptedException, IOException {
8890
.setActivity(Activity.playing("Yatopia.jar"))
8991
.disableCache(CacheFlag.VOICE_STATE, CacheFlag.ACTIVITY)
9092
.addEventListeners(timingsMessageListener)
93+
.addEventListeners(pasteMessageListener)
9194
.build()
9295
.awaitReady();
9396

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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

Comments
 (0)