Added safe mode

This commit is contained in:
Norbi Peti 2016-12-31 19:43:35 +01:00
parent 85d26d7b9f
commit 6c654c28dd
7 changed files with 67 additions and 35 deletions

View file

@ -8,6 +8,7 @@ import java.util.List;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.plugin.RegisteredServiceProvider; import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitTask;
import com.google.common.io.Files; import com.google.common.io.Files;
import com.google.gson.*; import com.google.gson.*;
@ -30,6 +31,7 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
private static boolean stop = false; private static boolean stop = false;
public static IDiscordClient dc; public static IDiscordClient dc;
public static DiscordPlugin plugin; public static DiscordPlugin plugin;
public static boolean SafeMode = true;
@Override @Override
public void onEnable() { public void onEnable() {
@ -62,50 +64,53 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
public static IChannel annchannel; public static IChannel annchannel;
public static IChannel genchannel; public static IChannel genchannel;
public static IChannel chatchannel; public static IChannel chatchannel;
public static IChannel issuechannel;
public static IChannel botroomchannel; public static IChannel botroomchannel;
/** /**
* Don't send messages, just receive, the same channel is used when testing * Don't send messages, just receive, the same channel is used when testing
*/ */
public static IChannel officechannel; public static IChannel officechannel;
public static IChannel coffeechannel;
public static IChannel updatechannel; public static IChannel updatechannel;
public static IGuild mainServer; public static IGuild mainServer;
public static IGuild devServer; public static IGuild devServer;
private static volatile BukkitTask task;
@Override @Override
public void handle(ReadyEvent event) { public void handle(ReadyEvent event) {
try { try {
int retryc = 0; task = Bukkit.getScheduler().runTaskTimerAsynchronously(this, () -> {
do { if (mainServer == null || devServer == null) {
mainServer = event.getClient().getGuildByID("125813020357165056"); mainServer = event.getClient().getGuildByID("125813020357165056");
devServer = event.getClient().getGuildByID("219529124321034241"); devServer = event.getClient().getGuildByID("219529124321034241");
Thread.sleep(100); }
} while ((mainServer == null || devServer == null) && retryc++ < 10); if (mainServer == null || devServer == null)
if (!TBMCCoreAPI.IsTestServer()) { return; // Retry
botchannel = mainServer.getChannelByID("209720707188260864"); // bot if (!TBMCCoreAPI.IsTestServer()) {
annchannel = mainServer.getChannelByID("126795071927353344"); // announcements botchannel = mainServer.getChannelByID("209720707188260864"); // bot
genchannel = mainServer.getChannelByID("125813020357165056"); // general annchannel = mainServer.getChannelByID("126795071927353344"); // announcements
chatchannel = mainServer.getChannelByID("249663564057411596"); // minecraft_chat genchannel = mainServer.getChannelByID("125813020357165056"); // general
issuechannel = devServer.getChannelByID("219643416496046081"); // server-issues chatchannel = mainServer.getChannelByID("249663564057411596"); // minecraft_chat
botroomchannel = devServer.getChannelByID("239519012529111040"); // bot-room botroomchannel = devServer.getChannelByID("239519012529111040"); // bot-room
officechannel = devServer.getChannelByID("219626707458457603"); // developers-office officechannel = devServer.getChannelByID("219626707458457603"); // developers-office
coffeechannel = devServer.getChannelByID("219530035365675010"); // coffee-table updatechannel = devServer.getChannelByID("233724163519414272"); // server-updates
updatechannel = devServer.getChannelByID("233724163519414272"); // server-updates dc.changeStatus(Status.game("on TBMC"));
dc.changeStatus(Status.game("on TBMC")); } else {
} else { botchannel = devServer.getChannelByID("239519012529111040"); // bot-room
botchannel = devServer.getChannelByID("239519012529111040"); // bot-room annchannel = botchannel; // bot-room
annchannel = botchannel; // bot-room genchannel = botchannel; // bot-room
genchannel = botchannel; // bot-room botroomchannel = botchannel;// bot-room
botroomchannel = botchannel;// bot-room chatchannel = devServer.getChannelByID("248185455508455424"); // minecraft_chat_test
issuechannel = botchannel; // bot-room officechannel = devServer.getChannelByID("219626707458457603"); // developers-office
chatchannel = devServer.getChannelByID("248185455508455424"); // minecraft_chat_test updatechannel = botchannel;
officechannel = devServer.getChannelByID("219626707458457603"); // developers-office dc.changeStatus(Status.game("testing"));
coffeechannel = botchannel; // bot-room }
updatechannel = botchannel; if (botchannel == null || annchannel == null || genchannel == null || botroomchannel == null
dc.changeStatus(Status.game("testing")); || chatchannel == null || officechannel == null || updatechannel == null)
} return; // Retry
SafeMode = false;
if (task != null)
task.cancel();
}, 0, 10);
for (IListener<?> listener : CommandListener.getListeners()) for (IListener<?> listener : CommandListener.getListeners())
dc.getDispatcher().registerListener(listener); dc.getDispatcher().registerListener(listener);
MCChatListener mcchat = new MCChatListener(); MCChatListener mcchat = new MCChatListener();
@ -165,6 +170,10 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
private void AnnouncementGetterThreadMethod() { private void AnnouncementGetterThreadMethod() {
while (!stop) { while (!stop) {
try { try {
if (SafeMode) {
Thread.sleep(10000);
continue;
}
String body = TBMCCoreAPI.DownloadString(SubredditURL + "/new/.json?limit=10"); String body = TBMCCoreAPI.DownloadString(SubredditURL + "/new/.json?limit=10");
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();
@ -230,6 +239,8 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
e2.printStackTrace(); e2.printStackTrace();
} }
try { try {
if (SafeMode)
return null;
if (channel == chatchannel) if (channel == chatchannel)
MCChatListener.resetLastMessage(); // If this is a chat message, it'll be set again MCChatListener.resetLastMessage(); // If this is a chat message, it'll be set again
final String content = TBMCCoreAPI.IsTestServer() && channel != chatchannel final String content = TBMCCoreAPI.IsTestServer() && channel != chatchannel

View file

@ -1,7 +1,6 @@
package buttondevteam.discordplugin.listeners; package buttondevteam.discordplugin.listeners;
import java.awt.Color; import java.awt.Color;
import java.util.function.Predicate;
import java.util.function.Supplier; import java.util.function.Supplier;
import buttondevteam.discordplugin.DiscordPlugin; import buttondevteam.discordplugin.DiscordPlugin;
@ -16,6 +15,8 @@ import sx.blah.discord.util.RateLimitException;
public class AutoUpdaterListener implements IListener<MessageReceivedEvent> { public class AutoUpdaterListener implements IListener<MessageReceivedEvent> {
@Override @Override
public void handle(MessageReceivedEvent event) { public void handle(MessageReceivedEvent event) {
if (DiscordPlugin.SafeMode)
return;
if (!event.getMessage().getChannel().getID().equals(DiscordPlugin.officechannel.getID())) if (!event.getMessage().getChannel().getID().equals(DiscordPlugin.officechannel.getID()))
return; return;
if (!"239123781401051138".equals(event.getMessage().getWebhookID())) if (!"239123781401051138".equals(event.getMessage().getWebhookID()))

View file

@ -14,6 +14,8 @@ public class CommandListener {
return new IListener[] { new IListener<MentionEvent>() { return new IListener[] { new IListener<MentionEvent>() {
@Override @Override
public void handle(MentionEvent event) { public void handle(MentionEvent event) {
if (DiscordPlugin.SafeMode)
return;
if (event.getMessage().getAuthor().isBot()) if (event.getMessage().getAuthor().isBot())
return; return;
final IChannel channel = event.getMessage().getChannel(); final IChannel channel = event.getMessage().getChannel();
@ -24,6 +26,8 @@ public class CommandListener {
}, new IListener<MessageReceivedEvent>() { }, new IListener<MessageReceivedEvent>() {
@Override @Override
public void handle(MessageReceivedEvent event) { public void handle(MessageReceivedEvent event) {
if (DiscordPlugin.SafeMode)
return;
if (!event.getMessage().getChannel().isPrivate()) if (!event.getMessage().getChannel().isPrivate())
return; return;
if (event.getMessage().getAuthor().isBot()) if (event.getMessage().getAuthor().isBot())
@ -43,6 +47,8 @@ public class CommandListener {
* @return Whether it ran the command (always true if mentionedonly is false) * @return Whether it ran the command (always true if mentionedonly is false)
*/ */
public static boolean runCommand(IMessage message, boolean mentionedonly) { public static boolean runCommand(IMessage message, boolean mentionedonly) {
if (DiscordPlugin.SafeMode)
return true;
message.getChannel().setTypingStatus(true); message.getChannel().setTypingStatus(true);
final StringBuilder cmdwithargs = new StringBuilder(message.getContent()); final StringBuilder cmdwithargs = new StringBuilder(message.getContent());
final String mention = DiscordPlugin.dc.getOurUser().mention(false); final String mention = DiscordPlugin.dc.getOurUser().mention(false);

View file

@ -13,6 +13,8 @@ public class DebugMessageListener implements Listener{
} }
private static void SendMessage(String message) { private static void SendMessage(String message) {
if (DiscordPlugin.SafeMode)
return;
try { try {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.append("```").append("\n"); sb.append("```").append("\n");

View file

@ -18,6 +18,8 @@ public class ExceptionListener implements Listener {
@EventHandler @EventHandler
public void onException(TBMCExceptionEvent e) { public void onException(TBMCExceptionEvent e) {
if (DiscordPlugin.SafeMode)
return;
if (lastthrown.stream() if (lastthrown.stream()
.anyMatch(ex -> Arrays.equals(e.getException().getStackTrace(), ex.getStackTrace()) .anyMatch(ex -> Arrays.equals(e.getException().getStackTrace(), ex.getStackTrace())
&& e.getException().getMessage().equals(ex.getMessage())) && e.getException().getMessage().equals(ex.getMessage()))

View file

@ -57,6 +57,8 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
private static IMessage lastmessage = null; private static IMessage lastmessage = null;
private static long lastmsgtime = 0; private static long lastmsgtime = 0;
private static short lastlist = 0;
private static short lastlistp = 0;
public static final HashMap<String, DiscordSender> UnconnectedSenders = new HashMap<>(); public static final HashMap<String, DiscordSender> UnconnectedSenders = new HashMap<>();
public static final HashMap<String, DiscordPlayerSender> ConnectedSenders = new HashMap<>(); public static final HashMap<String, DiscordPlayerSender> ConnectedSenders = new HashMap<>();
@ -73,6 +75,7 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
/* && !(event.getMessage().getChannel().isPrivate() && privatechat) */) /* && !(event.getMessage().getChannel().isPrivate() && privatechat) */)
return; return;
lastmessage = null; lastmessage = null;
lastlist++;
if (author.isBot()) if (author.isBot())
return; return;
if (CommandListener.runCommand(event.getMessage(), true)) if (CommandListener.runCommand(event.getMessage(), true))
@ -115,12 +118,17 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
+ DiscordPlugin.botchannel.mention()); + DiscordPlugin.botchannel.mention());
return; return;
} }
if (cmd.equals("list") && Bukkit.getOnlinePlayers().size() == 0 && ListC++ > 2) // Lowered already if (lastlist > 5) {
ListC = 0;
lastlist = 0;
}
if (cmd.equals("list") && Bukkit.getOnlinePlayers().size() == lastlistp && ListC++ > 2) // Lowered already
{ {
dsender.sendMessage("Stop it. You know the answer."); dsender.sendMessage("Stop it. You know the answer.");
ListC = 0; lastlist = 0;
} else } else
Bukkit.dispatchCommand(dsender, cmd); Bukkit.dispatchCommand(dsender, cmd);
lastlistp = (short) Bukkit.getOnlinePlayers().size();
} else } else
TBMCChatAPI.SendChatMessage(Channel.GlobalChat, dsender, TBMCChatAPI.SendChatMessage(Channel.GlobalChat, dsender,
dmessage + (event.getMessage().getAttachments().size() > 0 ? "\n" + event.getMessage() dmessage + (event.getMessage().getAttachments().size() > 0 ? "\n" + event.getMessage()

View file

@ -39,6 +39,8 @@ public class MCListener implements Listener {
@EventHandler @EventHandler
public void onGetInfo(TBMCPlayerGetInfoEvent e) { public void onGetInfo(TBMCPlayerGetInfoEvent e) {
if (DiscordPlugin.SafeMode)
return;
DiscordPlayer dp = e.getPlayer().asPluginPlayer(DiscordPlayer.class); DiscordPlayer dp = e.getPlayer().asPluginPlayer(DiscordPlayer.class);
if (dp.getDiscordID() == null || dp.getDiscordID() == "") if (dp.getDiscordID() == null || dp.getDiscordID() == "")
return; return;