Give access to AutoRespond matches inside scripts

Available as %match_u%, %match_1% and so on
See #770 for initial suggestion
See #772 for in-progress implementation
This commit is contained in:
ORelio 2020-03-27 21:14:05 +01:00
parent e4e1f0b9fa
commit 00112e4c6a
25 changed files with 129 additions and 69 deletions

View file

@ -76,8 +76,9 @@ namespace MinecraftClient.ChatBots
/// <param name="username">Player who have sent the message</param>
/// <param name="message">Message to match against the regex or match string</param>
/// <param name="msgType">Type of the message public/private message, or other message</param>
/// <param name="localVars">Dictionary to populate with match variables in case of Regex match</param>
/// <returns>Internal command to run as a response to this user, or null if no match has been detected</returns>
public string Match(string username, string message, MessageType msgType)
public string Match(string username, string message, MessageType msgType, Dictionary<string, object> localVars)
{
string toSend = null;
@ -99,9 +100,14 @@ namespace MinecraftClient.ChatBots
if (regex.IsMatch(message))
{
Match regexMatch = regex.Match(message);
localVars["match_0"] = regexMatch.Groups[0].Value;
for (int i = regexMatch.Groups.Count - 1; i >= 1; i--)
{
toSend = toSend.Replace("$" + i, regexMatch.Groups[i].Value);
localVars["match_" + i] = regexMatch.Groups[i].Value;
}
toSend = toSend.Replace("$u", username);
localVars["match_u"] = username;
return toSend;
}
}
@ -109,6 +115,8 @@ namespace MinecraftClient.ChatBots
{
if (message.ToLower().Contains(match.ToLower()))
{
localVars["match_0"] = message;
localVars["match_u"] = username;
return toSend.Replace("$u", username);
}
}
@ -225,12 +233,13 @@ namespace MinecraftClient.ChatBots
{
foreach (RespondRule rule in respondRules)
{
string toPerform = rule.Match(sender, message, msgType);
Dictionary<string, object> localVars = new Dictionary<string, object>();
string toPerform = rule.Match(sender, message, msgType, localVars);
if (!String.IsNullOrEmpty(toPerform))
{
string response = null;
LogToConsole(toPerform);
PerformInternalCommand(toPerform, ref response);
PerformInternalCommand(toPerform, ref response, localVars);
if (!String.IsNullOrEmpty(response))
LogToConsole(response);
}

View file

@ -25,17 +25,18 @@ namespace MinecraftClient.ChatBots
private bool csharp;
private Thread thread;
private ManualResetEvent tpause;
private Dictionary<string, object> localVars;
public Script(string filename)
{
ParseArguments(filename);
}
public Script(string filename, string ownername)
public Script(string filename, string ownername, Dictionary<string, object> localVars)
: this(filename)
{
if (ownername != "")
owner = ownername;
this.owner = ownername;
this.localVars = localVars;
}
private void ParseArguments(string argstr)
@ -135,14 +136,14 @@ namespace MinecraftClient.ChatBots
csharp = file.EndsWith(".cs");
thread = null;
if (owner != null)
if (!String.IsNullOrEmpty(owner))
SendPrivateMessage(owner, "Script '" + file + "' loaded.");
}
else
{
LogToConsole("File not found: '" + System.IO.Path.GetFullPath(file) + "'");
if (owner != null)
if (!String.IsNullOrEmpty(owner))
SendPrivateMessage(owner, "File not found: '" + file + "'");
UnloadBot(); //No need to keep the bot active
@ -161,7 +162,7 @@ namespace MinecraftClient.ChatBots
{
try
{
CSharpRunner.Run(this, tpause, lines, args);
CSharpRunner.Run(this, tpause, lines, args, localVars);
}
catch (CSharpException e)
{
@ -198,7 +199,7 @@ namespace MinecraftClient.ChatBots
{
if (instruction_line[0] != '#' && instruction_line[0] != '/' && instruction_line[1] != '/')
{
instruction_line = Settings.ExpandVars(instruction_line);
instruction_line = Settings.ExpandVars(instruction_line, localVars);
string instruction_name = instruction_line.Split(' ')[0];
switch (instruction_name.ToLower())
{