From 207732cd866780e2e748a9760fb71cff883f88eb Mon Sep 17 00:00:00 2001 From: Pokechu22 Date: Fri, 29 Jan 2016 16:11:26 -0800 Subject: [PATCH] Add an option to add a delay between bot message Because bots can send several messages quickly, this adds an option to slow down the rate at which messages are produced (to avoid issues with antispam plugins). This should help solve part of the troubles @mobdon was having in #105. Right now the default time is 2 seconds per message. However, messages are sent imediately if the bot doesn't need to delay (so if it's a bot that only outputs one or two messages, those will still happen imediately). Also, note that it's limited per-bot right now. I also added an optional parameter to the SendText method so that bots can avoid this behavior if they need to. In some cases, they'll want to send multiple messages. --- MinecraftClient/ChatBot.cs | 43 +++++++++++++++++++++++++++++++++- MinecraftClient/McTcpClient.cs | 1 + MinecraftClient/Settings.cs | 3 +++ 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/MinecraftClient/ChatBot.cs b/MinecraftClient/ChatBot.cs index 816e9978..0d3fa75e 100644 --- a/MinecraftClient/ChatBot.cs +++ b/MinecraftClient/ChatBot.cs @@ -41,6 +41,34 @@ namespace MinecraftClient private McTcpClient Handler { get { return master != null ? master.Handler : _handler; } } private McTcpClient _handler = null; private ChatBot master = null; + private Queue chatQueue = new Queue(); + private DateTime? lastMessageSentTime = null; + private bool CanSendTextNow + { + get + { + return lastMessageSentTime != null + ? DateTime.Now > lastMessageSentTime.Value + Settings.botMessageDelay + : true; + } + } + + /// + /// Processes the current chat message queue, displaying a message after enough time passes. + /// + internal void ProcessQueuedText() + { + if (chatQueue.Count > 0) + { + if (CanSendTextNow) + { + string text = chatQueue.Dequeue(); + LogToConsole("Sending '" + text + "'"); + lastMessageSentTime = DateTime.Now; + Handler.SendText(text); + } + } + } /* ================================================== */ /* Main methods to override for creating your bot */ @@ -84,11 +112,24 @@ namespace MinecraftClient /// Send text to the server. Can be anything such as chat messages or commands /// /// Text to send to the server + /// Whether the message should be sent immediately rather than being queued to avoid chat spam /// True if the text was sent with no error - protected bool SendText(string text) + protected bool SendText(string text, bool sendImmediately = false) { + if (Settings.botMessageDelay.TotalSeconds > 0 && !sendImmediately) + { + if (!CanSendTextNow) + { + chatQueue.Enqueue(text); + // TODO: We don't know whether there was an error at this point, so we assume there isn't. + // Might not be the best idea. + return true; + } + } + LogToConsole("Sending '" + text + "'"); + lastMessageSentTime = DateTime.Now; return Handler.SendText(text); } diff --git a/MinecraftClient/McTcpClient.cs b/MinecraftClient/McTcpClient.cs index 6423460a..b45d0a85 100644 --- a/MinecraftClient/McTcpClient.cs +++ b/MinecraftClient/McTcpClient.cs @@ -459,6 +459,7 @@ namespace MinecraftClient try { bots[i].Update(); + bots[i].ProcessQueuedText(); } catch (Exception e) { diff --git a/MinecraftClient/Settings.cs b/MinecraftClient/Settings.cs index 36ce2f87..3b0a65ca 100644 --- a/MinecraftClient/Settings.cs +++ b/MinecraftClient/Settings.cs @@ -44,6 +44,7 @@ namespace MinecraftClient public static string TranslationsFile_Website_Download = "http://resources.download.minecraft.net"; public static TimeSpan splitMessageDelay = TimeSpan.FromSeconds(2); public static List Bots_Owners = new List(); + public static TimeSpan botMessageDelay = TimeSpan.FromSeconds(2); public static string Language = "en_GB"; public static bool chatTimeStamps = false; public static bool interactiveMode = true; @@ -182,6 +183,7 @@ namespace MinecraftClient case "showxpbarmessages": DisplayXPBarMessages = str2bool(argValue); break; case "terrainandmovements": TerrainAndMovements = str2bool(argValue); break; case "privatemsgscmdname": PrivateMsgsCmdName = argValue.ToLower().Trim(); break; + case "botmessagedelay": botMessageDelay = TimeSpan.FromSeconds(str2int(argValue)); break; case "botowners": Bots_Owners.Clear(); @@ -406,6 +408,7 @@ namespace MinecraftClient + "consoletitle=%username%@%serverip% - Minecraft Console Client\r\n" + "internalcmdchar=slash #use 'none', 'slash' or 'backslash'\r\n" + "splitmessagedelay=2 #seconds between each part of a long message\r\n" + + "botmessagedelay=2 #seconds to delay between message a bot makes to avoid accidental spam\n\n" + "mcversion=auto #use 'auto' or '1.X.X' values\r\n" + "brandinfo=mcc #use 'mcc','vanilla', or 'none'\r\n" + "chatbotlogfile= #leave empty for no logfile\r\n"