This commit is contained in:
Norbi Peti 2016-10-07 23:48:39 +02:00
parent 824c72318c
commit b49da2f1df
2 changed files with 49 additions and 13 deletions

View file

@ -54,7 +54,8 @@
<artifactSet> <artifactSet>
<excludes> <excludes>
<exclude>org.spigotmc:spigot-api</exclude> <exclude>org.spigotmc:spigot-api</exclude>
</excludes> <exclude>com.github.TBMCPlugins.ButtonCore:ButtonCore</exclude>
</excludes> <!-- TODO: http://stackoverflow.com/questions/28458058/maven-shade-plugin-exclude-a-dependency-and-all-its-transitive-dependencies -->
</artifactSet> </artifactSet>
<pluginExecution> <pluginExecution>
<action> <action>

View file

@ -1,11 +1,17 @@
package buttondevteam.discordplugin; package buttondevteam.discordplugin;
import java.io.BufferedReader;
import java.io.File;
import java.nio.charset.StandardCharsets;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import com.google.common.base.Charsets; import com.google.common.base.Charsets;
import com.google.common.io.Files;
import com.google.gson.JsonArray; import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.JsonParser; import com.google.gson.JsonParser;
@ -22,14 +28,21 @@ import sx.blah.discord.handle.obj.IChannel;
public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> { public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
private static final String SubredditURL = "https://www.reddit.com/r/ChromaGamers"; private static final String SubredditURL = "https://www.reddit.com/r/ChromaGamers";
private static boolean stop = false; private static boolean stop = false;
private static IDiscordClient dc;
@Override @Override
public void onEnable() { public void onEnable() {
try { try {
Bukkit.getLogger().info("Initializing DiscordPlugin..."); Bukkit.getLogger().info("Initializing DiscordPlugin...");
final File file = new File("TBMC", "DiscordRedditLastAnnouncement.txt");
if (file.exists()) {
BufferedReader reader = Files.newReader(file, StandardCharsets.UTF_8);
String line = reader.readLine();
lastannouncementtime = Integer.parseInt(line);
}
ClientBuilder cb = new ClientBuilder(); ClientBuilder cb = new ClientBuilder();
cb.withToken(IOUtils.toString(getClass().getResourceAsStream("/Token.txt"), Charsets.UTF_8)); cb.withToken(IOUtils.toString(getClass().getResourceAsStream("/Token.txt"), Charsets.UTF_8));
IDiscordClient dc = cb.login(); dc = cb.login();
dc.getDispatcher().registerListener(this); dc.getDispatcher().registerListener(this);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
@ -61,23 +74,45 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
stop = true; stop = true;
} }
private long lastannouncementtime = 0;
private void AnnouncementGetterThreadMethod() { private void AnnouncementGetterThreadMethod() {
while (!stop) { while (!stop) {
try { try {
String body = TBMCCoreAPI.DownloadString(SubredditURL + ".json?limit=10"); // TODO: Save last announcement String body = TBMCCoreAPI.DownloadString(SubredditURL + "/new/.json?limit=10"); // TODO: Save last announcement
JsonArray json = new JsonParser().parse(body).getAsJsonArray().get(1).getAsJsonObject().get("data") JsonArray json = new JsonParser().parse(body).getAsJsonObject().get("data").getAsJsonObject()
.getAsJsonObject().get("children").getAsJsonArray(); .get("children").getAsJsonArray();
StringBuilder msgsb = new StringBuilder();
for (Object obj : json) { for (Object obj : json) {
JsonObject item = (JsonObject) obj; JsonObject item = (JsonObject) obj;
String author = item.get("data").getAsJsonObject().get("author").getAsString(); final JsonObject data = item.get("data").getAsJsonObject();
String post = item.get("data").getAsJsonObject().get("body").getAsString(); String author = data.get("author").getAsString();
String title = data.get("title").getAsString();
// String stickied = data.get("stickied").getAsString();
JsonElement flairjson = data.get("link_flair_text");
String flair;
if (flairjson.isJsonNull())
flair = null;
else
flair = flairjson.getAsString();
String distinguished = data.get("distinguished").getAsString();
String url = data.get("url").getAsString();
long date = data.get("created_utc").getAsLong();
if (date <= lastannouncementtime)
break;
System.out.println("author: " + author); System.out.println("author: " + author);
System.out.println("post: " + post); System.out.println("title: " + title);
try { System.out.println("distinguished: " + distinguished);
Thread.sleep(10); System.out.println("url: " + url);
} catch (InterruptedException ex) { if (distinguished.equals("moderator"))
Thread.currentThread().interrupt(); msgsb.append("A new mod post was submitted to the subreddit");
} else
msgsb.append("A new post was submitted to the subreddit");
msgsb.append("\nAuthor: ").append(author).append("\nFlair: ").append(flair).append("\nTitle: ")
.append(title).append("\n").append(url);
lastannouncementtime = date;
File file = new File("TBMC", "DiscordRedditLastAnnouncement.txt");
Files.write(lastannouncementtime + "", file, StandardCharsets.UTF_8);
} }
try { try {
Thread.sleep(10000); Thread.sleep(10000);