Improvements, fixes, additions

- Now it waits 10s even if sending the message failed
- And it'll retry sending it
- Fixed condition
- Added posting distinguished posts to announcements
This commit is contained in:
Norbi Peti 2016-10-10 21:40:45 +02:00
parent 45f6cdd39f
commit f7dfc297ee

View file

@ -51,11 +51,13 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
} }
private IChannel channel; private IChannel channel;
private IChannel modchannel;
@Override @Override
public void handle(ReadyEvent event) { public void handle(ReadyEvent event) {
try { try {
channel = event.getClient().getGuilds().get(0).getChannelByID("209720707188260864"); // bot channel = event.getClient().getGuilds().get(0).getChannelByID("209720707188260864"); // bot
modchannel = event.getClient().getGuilds().get(0).getChannelByID("126795071927353344"); // announcements
channel.sendMessage("Minecraft server started up"); channel.sendMessage("Minecraft server started up");
Runnable r = new Runnable() { Runnable r = new Runnable() {
public void run() { public void run() {
@ -88,11 +90,13 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
JsonArray json = new JsonParser().parse(body).getAsJsonObject().get("data").getAsJsonObject() JsonArray json = new JsonParser().parse(body).getAsJsonObject().get("data").getAsJsonObject()
.get("children").getAsJsonArray(); .get("children").getAsJsonArray();
StringBuilder msgsb = new StringBuilder(); StringBuilder msgsb = new StringBuilder();
StringBuilder modmsgsb = new StringBuilder();
long lastanntime = lastannouncementtime;
for (int i = json.size() - 1; i >= 0; i--) { for (int i = json.size() - 1; i >= 0; i--) {
JsonObject item = json.get(i).getAsJsonObject(); JsonObject item = json.get(i).getAsJsonObject();
final JsonObject data = item.get("data").getAsJsonObject(); final JsonObject data = item.get("data").getAsJsonObject();
String author = data.get("author").getAsString(); String author = data.get("author").getAsString();
String title = data.get("title").getAsString(); // String title = data.get("title").getAsString();
// String stickied = data.get("stickied").getAsString(); // String stickied = data.get("stickied").getAsString();
JsonElement flairjson = data.get("link_flair_text"); JsonElement flairjson = data.get("link_flair_text");
String flair; String flair;
@ -109,23 +113,27 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
String permalink = "https://www.reddit.com" + data.get("permalink").getAsString(); String permalink = "https://www.reddit.com" + data.get("permalink").getAsString();
long date = data.get("created_utc").getAsLong(); long date = data.get("created_utc").getAsLong();
if (date <= lastannouncementtime) if (date <= lastannouncementtime)
break; continue;
msgsb.append("A new post was submitted to the subreddit by ").append(author).append("\n") (distinguished != null && distinguished.equals("moderator") ? modmsgsb : msgsb)
.append("A new post was submitted to the subreddit by ").append(author).append("\n")
.append(permalink).append("\n"); .append(permalink).append("\n");
lastannouncementtime = date; lastanntime = date;
File file = new File("TBMC", "DiscordRedditLastAnnouncement.txt");
Files.write(lastannouncementtime + "", file, StandardCharsets.UTF_8);
} }
if (msgsb.length() > 0) if (msgsb.length() > 0)
channel.sendMessage(msgsb.toString()); // TODO: Mod msgsb for announcements channel.sendMessage(msgsb.toString());
if (modmsgsb.length() > 0)
modchannel.sendMessage(modmsgsb.toString());
lastannouncementtime = lastanntime; // If sending succeeded
File file = new File("TBMC", "DiscordRedditLastAnnouncement.txt");
Files.write(lastannouncementtime + "", file, StandardCharsets.UTF_8);
} catch (Exception e) {
e.printStackTrace();
}
try { try {
Thread.sleep(10000); Thread.sleep(10000);
} catch (InterruptedException ex) { } catch (InterruptedException ex) {
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
} }
} catch (Exception e) {
e.printStackTrace();
}
} }
} }
} }