Advertisement
SecRez

Auto Respond.

Aug 29th, 2016
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.67 KB | None | 0 0
  1.     created by ░L░u░a░  
  2.  
  3. using System;
  4. using System.IO;
  5. using System.Collections.Generic;
  6. using System.Text.RegularExpressions;
  7.  
  8. namespace MinecraftClient.ChatBots
  9. {
  10.     /// <summary>
  11.     /// This bot automatically runs actions when a user sends a message matching a specified rule
  12.     /// </summary>
  13.     class AutoRespond : ChatBot
  14.     {
  15.         private string matchesFile;
  16.         private List<RespondRule> respondRules;
  17.         private enum MessageType { Public, Private, Other };
  18.  
  19.         /// <summary>
  20.         /// Create a new AutoRespond bot
  21.         /// </summary>
  22.         /// <param name="matchesFile">INI File to load matches from</param>
  23.         public AutoRespond(string matchesFile)
  24.         {
  25.             this.matchesFile = matchesFile;
  26.         }
  27.  
  28.         /// <summary>
  29.         /// Describe a respond rule based on a simple match or a regex
  30.         /// </summary>
  31.         private class RespondRule
  32.         {
  33.             private Regex regex;
  34.             private string match;
  35.             private string actionPublic;
  36.             private string actionPrivate;
  37.             private string actionOther;
  38.             private bool ownersOnly;
  39.  
  40.             /// <summary>
  41.             /// Create a respond rule from a regex and a reponse message or command
  42.             /// </summary>
  43.             /// <param name="regex">Regex</param>
  44.             /// <param name="actionPublic">Internal command to run for public messages</param>
  45.             /// <param name="actionPrivate">Internal command to run for private messages</param>
  46.             /// <param name="actionOther">Internal command to run for any other messages</param>
  47.             /// <param name="ownersOnly">Only match messages from bot owners</param>
  48.             public RespondRule(Regex regex, string actionPublic, string actionPrivate, string actionOther, bool ownersOnly)
  49.             {
  50.                 this.regex = regex;
  51.                 this.match = null;
  52.                 this.actionPublic = actionPublic;
  53.                 this.actionPrivate = actionPrivate;
  54.                 this.actionOther = actionOther;
  55.                 this.ownersOnly = ownersOnly;
  56.             }
  57.  
  58.             /// <summary>
  59.             /// Create a respond rule from a match string and a reponse message or command
  60.             /// </summary>
  61.             /// <param name="match">Match string</param>
  62.             /// <param name="actionPublic">Internal command to run for public messages</param>
  63.             /// <param name="actionPrivate">Internal command to run for private messages</param>
  64.             /// <param name="ownersOnly">Only match messages from bot owners</param>
  65.             public RespondRule(string match, string actionPublic, string actionPrivate, string actionOther, bool ownersOnly)
  66.             {
  67.                 this.regex = null;
  68.                 this.match = match;
  69.                 this.actionPublic = actionPublic;
  70.                 this.actionPrivate = actionPrivate;
  71.                 this.actionOther = actionOther;
  72.                 this.ownersOnly = ownersOnly;
  73.             }
  74.  
  75.             /// <summary>
  76.             /// Match the respond rule to the specified string and return a message or command to send if a match is detected
  77.             /// </summary>
  78.             /// <param name="username">Player who have sent the message</param>
  79.             /// <param name="message">Message to match against the regex or match string</param>
  80.             /// <param name="msgType">Type of the message public/private message, or other message</param>
  81.             /// <returns>Internal command to run as a response to this user, or null if no match has been detected</returns>
  82.             public string Match(string username, string message, MessageType msgType)
  83.             {
  84.                 string toSend = null;
  85.  
  86.                 if (ownersOnly && (String.IsNullOrEmpty(username) || !Settings.Bots_Owners.Contains(username.ToLower())))
  87.                     return null;
  88.  
  89.                 switch (msgType)
  90.                 {
  91.                     case MessageType.Public: toSend = actionPublic; break;
  92.                     case MessageType.Private: toSend = actionPrivate; break;
  93.                     case MessageType.Other: toSend = actionOther; break;
  94.                 }
  95.  
  96.                 if (String.IsNullOrEmpty(toSend))
  97.                     return null;
  98.  
  99.                 if (regex != null)
  100.                 {
  101.                     if (regex.IsMatch(message))
  102.                     {
  103.                         Match regexMatch = regex.Match(message);
  104.                         for (int i = regexMatch.Groups.Count - 1; i >= 1; i--)
  105.                             toSend = toSend.Replace("$" + i, regexMatch.Groups[i].Value);
  106.                         toSend = toSend.Replace("$u", username);
  107.                         return toSend;
  108.                     }
  109.                 }
  110.                 else if (!String.IsNullOrEmpty(match))
  111.                 {
  112.                     if (message.ToLower().Contains(match.ToLower()))
  113.                     {
  114.                         return toSend.Replace("$u", username);
  115.                     }
  116.                 }
  117.  
  118.                 return null;
  119.             }
  120.         }
  121.  
  122.         /// <summary>
  123.         /// Initialize the AutoRespond bot from the matches file
  124.         /// </summary>
  125.         public override void Initialize()
  126.         {
  127.             if (File.Exists(matchesFile))
  128.             {
  129.                 Regex matchRegex = null;
  130.                 string matchString = null;
  131.                 string matchAction = null;
  132.                 string matchActionPrivate = null;
  133.                 string matchActionOther = null;
  134.                 bool ownersOnly = false;
  135.                 respondRules = new List<RespondRule>();
  136.  
  137.                 foreach (string lineRAW in File.ReadAllLines(matchesFile))
  138.                 {
  139.                     string line = lineRAW.Split('#')[0].Trim();
  140.                     if (line.Length > 0)
  141.                     {
  142.                         if (line[0] == '[' && line[line.Length - 1] == ']')
  143.                         {
  144.                             switch (line.Substring(1, line.Length - 2).ToLower())
  145.                             {
  146.                                 case "match":
  147.                                     CheckAddMatch(matchRegex, matchString, matchAction, matchActionPrivate, matchActionOther, ownersOnly);
  148.                                     matchRegex = null;
  149.                                     matchString = null;
  150.                                     matchAction = null;
  151.                                     matchActionPrivate = null;
  152.                                     matchActionOther = null;
  153.                                     ownersOnly = false;
  154.                                     break;
  155.                             }
  156.                         }
  157.                         else
  158.                         {
  159.                             string argName = line.Split('=')[0];
  160.                             if (line.Length > (argName.Length + 1))
  161.                             {
  162.                                 string argValue = line.Substring(argName.Length + 1);
  163.                                 switch (argName.ToLower())
  164.                                 {
  165.                                     case "regex": matchRegex = new Regex(argValue); break;
  166.                                     case "match": matchString = argValue; break;
  167.                                     case "action": matchAction = argValue; break;
  168.                                     case "actionprivate": matchActionPrivate = argValue; break;
  169.                                     case "actionother": matchActionOther = argValue; break;
  170.                                     case "ownersonly": ownersOnly = Settings.str2bool(argValue); break;
  171.                                 }
  172.                             }
  173.                         }
  174.                     }
  175.                 }
  176.                 CheckAddMatch(matchRegex, matchString, matchAction, matchActionPrivate, matchActionOther, ownersOnly);
  177.             }
  178.             else
  179.             {
  180.                 LogToConsole("File not found: '" + matchesFile + "'");
  181.                 UnloadBot(); //No need to keep the bot active
  182.             }
  183.         }
  184.  
  185.         /// <summary>
  186.         /// Create a new respond rule from the provided arguments, only if they are valid: at least one match and one action
  187.         /// </summary>
  188.         /// <param name="matchRegex">Matching regex</param>
  189.         /// <param name="matchString">Matching string</param>
  190.         /// <param name="matchAction">Action if the matching message is public</param>
  191.         /// <param name="matchActionPrivate">Action if the matching message is private</param>
  192.         /// <param name="ownersOnly">Only match messages from bot owners</param>
  193.         private void CheckAddMatch(Regex matchRegex, string matchString, string matchAction, string matchActionPrivate, string matchActionOther, bool ownersOnly)
  194.         {
  195.             if (matchAction != null || matchActionPrivate != null || matchActionOther != null)
  196.             {
  197.                 if (matchRegex != null)
  198.                 {
  199.                     respondRules.Add(new RespondRule(matchRegex, matchAction, matchActionPrivate, matchActionOther, ownersOnly));
  200.                 }
  201.                 else if (matchString != null)
  202.                 {
  203.                     respondRules.Add(new RespondRule(matchString, matchAction, matchActionPrivate, matchActionOther, ownersOnly));
  204.                 }
  205.             }
  206.         }
  207.  
  208.         public override void GetText(string text)
  209.         {
  210.             //Remove colour codes
  211.             text = GetVerbatim(text);
  212.  
  213.             //Get Message type
  214.             string sender = "", message = "";
  215.             MessageType msgType = MessageType.Other;
  216.             if (IsChatMessage(text, ref message, ref sender))
  217.                 msgType = MessageType.Public;
  218.             else if (IsPrivateMessage(text, ref message, ref sender))
  219.                 msgType = MessageType.Private;
  220.             else message = text;
  221.  
  222.             //Do not process messages sent by the bot itself
  223.             if (msgType == MessageType.Other || sender != Settings.Username)
  224.             {
  225.                 foreach (RespondRule rule in respondRules)
  226.                 {
  227.                     string toPerform = rule.Match(sender, message, msgType);
  228.                     if (!String.IsNullOrEmpty(toPerform))
  229.                     {
  230.                         string response = null;
  231.                         LogToConsole(toPerform);
  232.                         PerformInternalCommand(toPerform, ref response);
  233.                         if (!String.IsNullOrEmpty(response))
  234.                             LogToConsole(response);
  235.                     }
  236.                 }
  237.             }
  238.         }
  239.     }
  240. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement