Added a few things
- Implemented the connect command on the Discord side - It'll retry if the message sending failed - Wait for distinguish
This commit is contained in:
parent
0d4dc309a5
commit
267555848c
5 changed files with 74 additions and 49 deletions
|
@ -41,6 +41,10 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
|
||||||
BufferedReader reader = Files.newReader(file, StandardCharsets.UTF_8);
|
BufferedReader reader = Files.newReader(file, StandardCharsets.UTF_8);
|
||||||
String line = reader.readLine();
|
String line = reader.readLine();
|
||||||
lastannouncementtime = Long.parseLong(line);
|
lastannouncementtime = Long.parseLong(line);
|
||||||
|
file.delete();
|
||||||
|
} else {
|
||||||
|
lastannouncementtime = getConfig().getLong("lastannouncementtime");
|
||||||
|
lastseentime = getConfig().getLong("lastseentime");
|
||||||
}
|
}
|
||||||
ClientBuilder cb = new ClientBuilder();
|
ClientBuilder cb = new ClientBuilder();
|
||||||
cb.withToken(Files.readFirstLine(new File("TBMC", "Token.txt"), StandardCharsets.UTF_8));
|
cb.withToken(Files.readFirstLine(new File("TBMC", "Token.txt"), StandardCharsets.UTF_8));
|
||||||
|
@ -79,8 +83,7 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
|
||||||
issuechannel = devServer.getChannelByID("239519012529111040"); // bottest
|
issuechannel = devServer.getChannelByID("239519012529111040"); // bottest
|
||||||
}
|
}
|
||||||
dc.changeStatus(Status.game("on TBMC"));
|
dc.changeStatus(Status.game("on TBMC"));
|
||||||
botchannel.sendMessage("Minecraft server started up");
|
sendMessageToChannel(botchannel, "Minecraft server started up");
|
||||||
// TBMCDiscordAPI.SendException(new Exception("This is a test exception"), "This is a test error message");
|
|
||||||
Runnable r = new Runnable() {
|
Runnable r = new Runnable() {
|
||||||
public void run() {
|
public void run() {
|
||||||
AnnouncementGetterThreadMethod();
|
AnnouncementGetterThreadMethod();
|
||||||
|
@ -108,6 +111,7 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
|
||||||
}
|
}
|
||||||
|
|
||||||
private long lastannouncementtime = 0;
|
private long lastannouncementtime = 0;
|
||||||
|
private long lastseentime = 0;
|
||||||
|
|
||||||
private void AnnouncementGetterThreadMethod() {
|
private void AnnouncementGetterThreadMethod() {
|
||||||
while (!stop) {
|
while (!stop) {
|
||||||
|
@ -122,14 +126,6 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
|
||||||
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 stickied = data.get("stickied").getAsString();
|
|
||||||
JsonElement flairjson = data.get("link_flair_text");
|
|
||||||
String flair;
|
|
||||||
if (flairjson.isJsonNull())
|
|
||||||
flair = null;
|
|
||||||
else
|
|
||||||
flair = flairjson.getAsString();
|
|
||||||
JsonElement distinguishedjson = data.get("distinguished");
|
JsonElement distinguishedjson = data.get("distinguished");
|
||||||
String distinguished;
|
String distinguished;
|
||||||
if (distinguishedjson.isJsonNull())
|
if (distinguishedjson.isJsonNull())
|
||||||
|
@ -138,20 +134,22 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
|
||||||
distinguished = distinguishedjson.getAsString();
|
distinguished = distinguishedjson.getAsString();
|
||||||
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 > lastseentime)
|
||||||
continue;
|
lastseentime = date;
|
||||||
(distinguished != null && distinguished.equals("moderator") ? modmsgsb : msgsb)
|
else if (date > lastannouncementtime) {
|
||||||
.append("A new post was submitted to the subreddit by ").append(author).append("\n")
|
(distinguished != null && distinguished.equals("moderator") ? modmsgsb : msgsb)
|
||||||
.append(permalink).append("\n");
|
.append("A new post was submitted to the subreddit by ").append(author).append("\n")
|
||||||
lastanntime = date;
|
.append(permalink).append("\n");
|
||||||
|
lastanntime = date;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (msgsb.length() > 0)
|
if (msgsb.length() > 0)
|
||||||
genchannel.pin(genchannel.sendMessage(msgsb.toString()));
|
genchannel.pin(sendMessageToChannel(genchannel, msgsb.toString()));
|
||||||
if (modmsgsb.length() > 0) // TODO: Wait for distinguish
|
if (modmsgsb.length() > 0)
|
||||||
annchannel.sendMessage(modmsgsb.toString());
|
sendMessageToChannel(annchannel, modmsgsb.toString());
|
||||||
lastannouncementtime = lastanntime; // If sending succeeded
|
lastannouncementtime = lastanntime; // If sending succeeded
|
||||||
File file = new File("TBMC", "DiscordRedditLastAnnouncement.txt");
|
getConfig().set("lastannouncementtime", lastannouncementtime);
|
||||||
Files.write(lastannouncementtime + "", file, StandardCharsets.UTF_8);
|
getConfig().set("lastseentime", lastseentime);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
@ -162,4 +160,25 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static IMessage sendMessageToChannel(IChannel channel, String message) {
|
||||||
|
for (int i = 0; i < 10; i++) {
|
||||||
|
try {
|
||||||
|
Thread.sleep(i * 100);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
return channel.sendMessage(message);
|
||||||
|
} catch (Exception e) {
|
||||||
|
if (i == 9) {
|
||||||
|
Bukkit.getLogger().warning("Failed to deliver message to Discord! Channel: " + channel.getName()
|
||||||
|
+ " Message: " + message);
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
} else
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,9 +11,9 @@ public class MCListener implements Listener {
|
||||||
return;
|
return;
|
||||||
try {
|
try {
|
||||||
if (e.getCommand().equalsIgnoreCase("stop"))
|
if (e.getCommand().equalsIgnoreCase("stop"))
|
||||||
DiscordPlugin.botchannel.sendMessage("Minecraft server shutting down!");
|
DiscordPlugin.sendMessageToChannel(DiscordPlugin.botchannel, "Minecraft server shutting down!");
|
||||||
else if (e.getCommand().equalsIgnoreCase("restart"))
|
else if (e.getCommand().equalsIgnoreCase("restart"))
|
||||||
DiscordPlugin.botchannel.sendMessage("Minecraft server restarting");
|
DiscordPlugin.sendMessageToChannel(DiscordPlugin.botchannel, "Minecraft server restarting");
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,38 +1,18 @@
|
||||||
package buttondevteam.discordplugin;
|
package buttondevteam.discordplugin;
|
||||||
|
|
||||||
import java.io.PrintStream;
|
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
|
|
||||||
import org.apache.commons.io.output.ByteArrayOutputStream;
|
|
||||||
import org.apache.commons.lang.exception.ExceptionUtils;
|
import org.apache.commons.lang.exception.ExceptionUtils;
|
||||||
|
|
||||||
public final class TBMCDiscordAPI {
|
public final class TBMCDiscordAPI {
|
||||||
public static void SendException(Throwable e, String sourcemessage) {
|
public static void SendException(Throwable e, String sourcemessage) {
|
||||||
try {
|
try {
|
||||||
//System.out.println("A");
|
|
||||||
// ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
||||||
// PrintStream str = new PrintStream(baos, true, "UTF-8");
|
|
||||||
// PrintStream str = new PrintStream(baos);
|
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
//System.out.println("B");
|
|
||||||
sb.append(sourcemessage).append("\n");
|
sb.append(sourcemessage).append("\n");
|
||||||
sb.append("```").append("\n");
|
sb.append("```").append("\n");
|
||||||
// e.printStackTrace(str);
|
|
||||||
sb.append(ExceptionUtils.getStackTrace(e)).append("\n");
|
sb.append(ExceptionUtils.getStackTrace(e)).append("\n");
|
||||||
sb.append("```");
|
sb.append("```");
|
||||||
// str.flush();
|
DiscordPlugin.sendMessageToChannel(DiscordPlugin.issuechannel, sb.toString());
|
||||||
// str.close();
|
|
||||||
//System.out.println("C");
|
|
||||||
//System.out.println("D");
|
|
||||||
// final String string = baos.toString(StandardCharsets.UTF_8);
|
|
||||||
//System.out.println("E");
|
|
||||||
DiscordPlugin.issuechannel.sendMessage(sb.toString());
|
|
||||||
//System.out.println("F");
|
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
//System.out.println("EX");
|
|
||||||
//System.out.println(ex);
|
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
}
|
}
|
||||||
//System.out.println("G");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,8 @@
|
||||||
package buttondevteam.discordplugin.commands;
|
package buttondevteam.discordplugin.commands;
|
||||||
|
|
||||||
|
import com.google.common.collect.HashBiMap;
|
||||||
|
|
||||||
|
import buttondevteam.discordplugin.DiscordPlugin;
|
||||||
import sx.blah.discord.handle.obj.IMessage;
|
import sx.blah.discord.handle.obj.IMessage;
|
||||||
|
|
||||||
public class ConnectCommand extends DiscordCommandBase {
|
public class ConnectCommand extends DiscordCommandBase {
|
||||||
|
@ -9,13 +12,32 @@ public class ConnectCommand extends DiscordCommandBase {
|
||||||
return "connect";
|
return "connect";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Key: Minecraft name<br>
|
||||||
|
* Value: Discord ID
|
||||||
|
*/
|
||||||
|
public static HashBiMap<String, String> WaitingToConnect = HashBiMap.create();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run(IMessage message, String args) { // TODO: Throws?
|
public void run(IMessage message, String args) {
|
||||||
try {
|
if (args.length() == 0) {
|
||||||
message.getChannel().sendMessage("Connect command WIP.");
|
DiscordPlugin.sendMessageToChannel(message.getChannel(), "Usage: connect <Minecraftname>");
|
||||||
} catch (Exception e) {
|
return;
|
||||||
e.printStackTrace();
|
|
||||||
}
|
}
|
||||||
|
if (args.contains(" ")) {
|
||||||
|
DiscordPlugin.sendMessageToChannel(message.getChannel(),
|
||||||
|
"Too many arguments.\nUsage: connect <Minecraftname>");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (WaitingToConnect.inverse().containsKey(message.getAuthor().getID())) {
|
||||||
|
DiscordPlugin.sendMessageToChannel(message.getChannel(),
|
||||||
|
"Replacing " + WaitingToConnect.inverse().get(message.getAuthor().getID()) + " with " + args);
|
||||||
|
WaitingToConnect.inverse().remove(message.getAuthor().getID());
|
||||||
|
}
|
||||||
|
WaitingToConnect.put(args, message.getAuthor().getID());
|
||||||
|
DiscordPlugin.sendMessageToChannel(message.getChannel(),
|
||||||
|
"Pending connection - accept connection in Minecraft from the account " + args
|
||||||
|
+ " before the server gets restarted.");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,6 +10,10 @@ public abstract class DiscordCommandBase {
|
||||||
public abstract void run(IMessage message, String args);
|
public abstract void run(IMessage message, String args);
|
||||||
|
|
||||||
private static final HashMap<String, DiscordCommandBase> commands = new HashMap<String, DiscordCommandBase>();
|
private static final HashMap<String, DiscordCommandBase> commands = new HashMap<String, DiscordCommandBase>();
|
||||||
|
|
||||||
|
protected void respond(IMessage message, String messagetosend) {
|
||||||
|
}
|
||||||
|
|
||||||
static {
|
static {
|
||||||
commands.put("connect", new ConnectCommand()); // TODO: API for adding commands?
|
commands.put("connect", new ConnectCommand()); // TODO: API for adding commands?
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue