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

View file

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

View file

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

View file

@ -18,6 +18,8 @@ public class ExceptionListener implements Listener {
@EventHandler
public void onException(TBMCExceptionEvent e) {
if (DiscordPlugin.SafeMode)
return;
if (lastthrown.stream()
.anyMatch(ex -> Arrays.equals(e.getException().getStackTrace(), ex.getStackTrace())
&& 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 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, DiscordPlayerSender> ConnectedSenders = new HashMap<>();
@ -73,6 +75,7 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
/* && !(event.getMessage().getChannel().isPrivate() && privatechat) */)
return;
lastmessage = null;
lastlist++;
if (author.isBot())
return;
if (CommandListener.runCommand(event.getMessage(), true))
@ -115,12 +118,17 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
+ DiscordPlugin.botchannel.mention());
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.");
ListC = 0;
lastlist = 0;
} else
Bukkit.dispatchCommand(dsender, cmd);
lastlistp = (short) Bukkit.getOnlinePlayers().size();
} else
TBMCChatAPI.SendChatMessage(Channel.GlobalChat, dsender,
dmessage + (event.getMessage().getAttachments().size() > 0 ? "\n" + event.getMessage()

View file

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