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);
|
||||
String line = reader.readLine();
|
||||
lastannouncementtime = Long.parseLong(line);
|
||||
file.delete();
|
||||
} else {
|
||||
lastannouncementtime = getConfig().getLong("lastannouncementtime");
|
||||
lastseentime = getConfig().getLong("lastseentime");
|
||||
}
|
||||
ClientBuilder cb = new ClientBuilder();
|
||||
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
|
||||
}
|
||||
dc.changeStatus(Status.game("on TBMC"));
|
||||
botchannel.sendMessage("Minecraft server started up");
|
||||
// TBMCDiscordAPI.SendException(new Exception("This is a test exception"), "This is a test error message");
|
||||
sendMessageToChannel(botchannel, "Minecraft server started up");
|
||||
Runnable r = new Runnable() {
|
||||
public void run() {
|
||||
AnnouncementGetterThreadMethod();
|
||||
|
@ -108,6 +111,7 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
|
|||
}
|
||||
|
||||
private long lastannouncementtime = 0;
|
||||
private long lastseentime = 0;
|
||||
|
||||
private void AnnouncementGetterThreadMethod() {
|
||||
while (!stop) {
|
||||
|
@ -122,14 +126,6 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
|
|||
JsonObject item = json.get(i).getAsJsonObject();
|
||||
final JsonObject data = item.get("data").getAsJsonObject();
|
||||
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");
|
||||
String distinguished;
|
||||
if (distinguishedjson.isJsonNull())
|
||||
|
@ -138,20 +134,22 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
|
|||
distinguished = distinguishedjson.getAsString();
|
||||
String permalink = "https://www.reddit.com" + data.get("permalink").getAsString();
|
||||
long date = data.get("created_utc").getAsLong();
|
||||
if (date <= lastannouncementtime)
|
||||
continue;
|
||||
(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");
|
||||
lastanntime = date;
|
||||
if (date > lastseentime)
|
||||
lastseentime = date;
|
||||
else if (date > lastannouncementtime) {
|
||||
(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");
|
||||
lastanntime = date;
|
||||
}
|
||||
}
|
||||
if (msgsb.length() > 0)
|
||||
genchannel.pin(genchannel.sendMessage(msgsb.toString()));
|
||||
if (modmsgsb.length() > 0) // TODO: Wait for distinguish
|
||||
annchannel.sendMessage(modmsgsb.toString());
|
||||
genchannel.pin(sendMessageToChannel(genchannel, msgsb.toString()));
|
||||
if (modmsgsb.length() > 0)
|
||||
sendMessageToChannel(annchannel, modmsgsb.toString());
|
||||
lastannouncementtime = lastanntime; // If sending succeeded
|
||||
File file = new File("TBMC", "DiscordRedditLastAnnouncement.txt");
|
||||
Files.write(lastannouncementtime + "", file, StandardCharsets.UTF_8);
|
||||
getConfig().set("lastannouncementtime", lastannouncementtime);
|
||||
getConfig().set("lastseentime", lastseentime);
|
||||
} catch (Exception e) {
|
||||
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;
|
||||
try {
|
||||
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"))
|
||||
DiscordPlugin.botchannel.sendMessage("Minecraft server restarting");
|
||||
DiscordPlugin.sendMessageToChannel(DiscordPlugin.botchannel, "Minecraft server restarting");
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
|
|
|
@ -1,38 +1,18 @@
|
|||
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;
|
||||
|
||||
public final class TBMCDiscordAPI {
|
||||
public static void SendException(Throwable e, String sourcemessage) {
|
||||
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();
|
||||
//System.out.println("B");
|
||||
sb.append(sourcemessage).append("\n");
|
||||
sb.append("```").append("\n");
|
||||
// e.printStackTrace(str);
|
||||
sb.append(ExceptionUtils.getStackTrace(e)).append("\n");
|
||||
sb.append("```");
|
||||
// str.flush();
|
||||
// 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");
|
||||
DiscordPlugin.sendMessageToChannel(DiscordPlugin.issuechannel, sb.toString());
|
||||
} catch (Exception ex) {
|
||||
//System.out.println("EX");
|
||||
//System.out.println(ex);
|
||||
ex.printStackTrace();
|
||||
}
|
||||
//System.out.println("G");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,8 @@
|
|||
package buttondevteam.discordplugin.commands;
|
||||
|
||||
import com.google.common.collect.HashBiMap;
|
||||
|
||||
import buttondevteam.discordplugin.DiscordPlugin;
|
||||
import sx.blah.discord.handle.obj.IMessage;
|
||||
|
||||
public class ConnectCommand extends DiscordCommandBase {
|
||||
|
@ -9,13 +12,32 @@ public class ConnectCommand extends DiscordCommandBase {
|
|||
return "connect";
|
||||
}
|
||||
|
||||
/**
|
||||
* Key: Minecraft name<br>
|
||||
* Value: Discord ID
|
||||
*/
|
||||
public static HashBiMap<String, String> WaitingToConnect = HashBiMap.create();
|
||||
|
||||
@Override
|
||||
public void run(IMessage message, String args) { // TODO: Throws?
|
||||
try {
|
||||
message.getChannel().sendMessage("Connect command WIP.");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
public void run(IMessage message, String args) {
|
||||
if (args.length() == 0) {
|
||||
DiscordPlugin.sendMessageToChannel(message.getChannel(), "Usage: connect <Minecraftname>");
|
||||
return;
|
||||
}
|
||||
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);
|
||||
|
||||
private static final HashMap<String, DiscordCommandBase> commands = new HashMap<String, DiscordCommandBase>();
|
||||
|
||||
protected void respond(IMessage message, String messagetosend) {
|
||||
}
|
||||
|
||||
static {
|
||||
commands.put("connect", new ConnectCommand()); // TODO: API for adding commands?
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue