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"