diff --git a/MinecraftClient/ChatBots/AutoRespond.cs b/MinecraftClient/ChatBots/AutoRespond.cs index 19146854..974ac8e6 100644 --- a/MinecraftClient/ChatBots/AutoRespond.cs +++ b/MinecraftClient/ChatBots/AutoRespond.cs @@ -12,6 +12,7 @@ namespace MinecraftClient.ChatBots { private string matchesFile; private List respondRules; + private enum MessageType { Public, Private, Other }; /// /// Create a new AutoRespond bot @@ -31,6 +32,7 @@ namespace MinecraftClient.ChatBots private string match; private string actionPublic; private string actionPrivate; + private string actionOther; /// /// Create a respond rule from a regex and a reponse message or command @@ -38,12 +40,14 @@ namespace MinecraftClient.ChatBots /// Regex /// Internal command to run for public messages /// Internal command to run for private messages - public RespondRule(Regex regex, string actionPublic, string actionPrivate) + /// Internal command to run for any other messages + public RespondRule(Regex regex, string actionPublic, string actionPrivate, string actionOther) { this.regex = regex; this.match = null; this.actionPublic = actionPublic; this.actionPrivate = actionPrivate; + this.actionOther = actionOther; } /// @@ -52,12 +56,13 @@ namespace MinecraftClient.ChatBots /// Match string /// Internal command to run for public messages /// Internal command to run for private messages - public RespondRule(string match, string actionPublic, string actionPrivate) + public RespondRule(string match, string actionPublic, string actionPrivate, string actionOther) { this.regex = null; this.match = match; this.actionPublic = actionPublic; this.actionPrivate = actionPrivate; + this.actionOther = actionOther; } /// @@ -65,16 +70,27 @@ namespace MinecraftClient.ChatBots /// /// Player who have sent the message /// Message to match against the regex or match string - /// True if the provided message was sent privately eg with /tell + /// Type of the message public/private message, or other message /// Internal command to run as a response to this user, or null if no match has been detected - public string Match(string username, string message, bool privateMsg) + public string Match(string username, string message, MessageType msgType) { + string toSend = null; + + switch (msgType) + { + case MessageType.Public: toSend = actionPublic; break; + case MessageType.Private: toSend = actionPrivate; break; + case MessageType.Other: toSend = actionOther; break; + } + + if (String.IsNullOrEmpty(toSend)) + return null; + if (regex != null) { if (regex.IsMatch(message)) { Match regexMatch = regex.Match(message); - string toSend = privateMsg ? actionPrivate : actionPublic; for (int i = regexMatch.Groups.Count - 1; i >= 1; i--) toSend = toSend.Replace("$" + i, regexMatch.Groups[i].Value); toSend = toSend.Replace("$u", username); @@ -85,11 +101,10 @@ namespace MinecraftClient.ChatBots { if (message.ToLower().Contains(match.ToLower())) { - return (privateMsg - ? actionPrivate - : actionPublic).Replace("$u", username); + return toSend.Replace("$u", username); } } + return null; } } @@ -105,6 +120,7 @@ namespace MinecraftClient.ChatBots string matchString = null; string matchAction = null; string matchActionPrivate = null; + string matchActionOther = null; respondRules = new List(); foreach (string lineRAW in File.ReadAllLines(matchesFile)) @@ -117,11 +133,12 @@ namespace MinecraftClient.ChatBots switch (line.Substring(1, line.Length - 2).ToLower()) { case "match": - CheckAddMatch(matchRegex, matchString, matchAction, matchActionPrivate); + CheckAddMatch(matchRegex, matchString, matchAction, matchActionPrivate, matchActionOther); matchRegex = null; matchString = null; matchAction = null; matchActionPrivate = null; + matchActionOther = null; break; } } @@ -137,12 +154,13 @@ namespace MinecraftClient.ChatBots case "match": matchString = argValue; break; case "action": matchAction = argValue; break; case "actionprivate": matchActionPrivate = argValue; break; + case "actionother": matchActionOther = argValue; break; } } } } } - CheckAddMatch(matchRegex, matchString, matchAction, matchActionPrivate); + CheckAddMatch(matchRegex, matchString, matchAction, matchActionPrivate, matchActionOther); } else { @@ -158,22 +176,17 @@ namespace MinecraftClient.ChatBots /// Matching string /// Action if the matching message is public /// Action if the matching message is private - private void CheckAddMatch(Regex matchRegex, string matchString, string matchAction, string matchActionPrivate) + private void CheckAddMatch(Regex matchRegex, string matchString, string matchAction, string matchActionPrivate, string matchActionOther) { - if (matchAction != null || matchActionPrivate != null) + if (matchAction != null || matchActionPrivate != null || matchActionOther != null) { - if (matchActionPrivate == null) - { - matchActionPrivate = matchAction; - } - if (matchRegex != null) { - respondRules.Add(new RespondRule(matchRegex, matchAction, matchActionPrivate)); + respondRules.Add(new RespondRule(matchRegex, matchAction, matchActionPrivate, matchActionOther)); } else if (matchString != null) { - respondRules.Add(new RespondRule(matchString, matchAction, matchActionPrivate)); + respondRules.Add(new RespondRule(matchString, matchAction, matchActionPrivate, matchActionOther)); } } } @@ -183,20 +196,21 @@ namespace MinecraftClient.ChatBots //Remove colour codes text = GetVerbatim(text); - //Check if this is a valid message + //Get Message type string sender = "", message = ""; - bool chatMessage = IsChatMessage(text, ref message, ref sender); - bool privateMessage = false; - if (!chatMessage) - privateMessage = IsPrivateMessage(text, ref message, ref sender); + MessageType msgType = MessageType.Other; + if (IsChatMessage(text, ref message, ref sender)) + msgType = MessageType.Public; + else if (IsPrivateMessage(text, ref message, ref sender)) + msgType = MessageType.Private; - //Process only chat messages sent by another user - if ((chatMessage || privateMessage) && sender != Settings.Username) + //Do not process messages sent by the bot itself + if (msgType == MessageType.Other || sender != Settings.Username) { foreach (RespondRule rule in respondRules) { - string toPerform = rule.Match(sender, message, privateMessage); - if (toPerform != null) + string toPerform = rule.Match(sender, message, msgType); + if (!String.IsNullOrEmpty(toPerform)) { string response = null; LogToConsole(toPerform); diff --git a/MinecraftClient/config/sample-matches.ini b/MinecraftClient/config/sample-matches.ini index fcf228a0..ec85b87c 100644 --- a/MinecraftClient/config/sample-matches.ini +++ b/MinecraftClient/config/sample-matches.ini @@ -4,8 +4,9 @@ # Structure of a match: [Match] Followed by the match and action # The match can be a simple match or an advanced regular expression -# You can define a different action if the match was in a private message # You can use $u for username of the player triggering the match +# You can define an action if the match was in a private message +# You can define an action if the match was not sent by a player # Regex matches are also supported eg $1, $2, $3.. in actions # Simple example: Respond to a message containing a keyword @@ -14,19 +15,30 @@ match=hi action=send hi, $u! actionprivate=send /tell $u Hello! +actionother=log detected "hi" message + +# You do not need to specify all the "action" fields +# Only one of them is required for each match # Advanced example: Use a regular expression +# Here a "regex" field is used instead of "match" field +# Do not use both "regex" and "match" fields... [Match] regex=^.*hello ([a-zA-Z0-9_]+).*$ action=send hello too, $1! -# You can also use any other internal command -# Private action is optional +# Example of using a script [Match] match=dotest action=script test +# Example of matching a server announcement + +[Match] +match=server is restarting +actionother=script restart + # Enjoy! # - ORelio \ No newline at end of file