本文整理汇总了C#中Robot.LoadScriptFile方法的典型用法代码示例。如果您正苦于以下问题:C# Robot.LoadScriptFile方法的具体用法?C# Robot.LoadScriptFile怎么用?C# Robot.LoadScriptFile使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Robot
的用法示例。
在下文中一共展示了Robot.LoadScriptFile方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Register
public void Register(Robot robot)
{
robot.Respond(@"scriptthis (.*):(.*)", async msg =>
{
string name = msg.Match[1].Trim();
string script = msg.Match[2].Trim();
//Save script to file
var filePath = Path.Combine(Environment.CurrentDirectory, Path.Combine("scripts", string.Format("{0}.csx", name)));
File.WriteAllText(filePath, script);
//try to load file
try
{
robot.LoadScriptFile("test1", filePath);
msg.Send(string.Format("Successfully added script: {0}", name));
}
catch (Exception scriptEx)
{
if (File.Exists(filePath))
{
//clean up
try
{
File.Delete(filePath);
}
catch { }
}
msg.Send(string.Format("Failed to load script: ({0}) - {1}", name, scriptEx.Message));
}
//???
//profit
});
robot.Respond(@"scriptthat (.*)", async msg =>
{
string url = msg.Match[1].Trim();
Uri uri;
try
{
uri = new Uri(url);
}
catch (Exception ex)
{
msg.Send("Invalid Uri: " + ex.Message);
return;
}
if (!uri.Host.Equals("gist.github.com", StringComparison.OrdinalIgnoreCase))
{
await msg.Send("Only accepting Github Gists, try again later...");
return;
}
var gistId = url.Substring(url.LastIndexOf("/") + 1);
await msg.Http(string.Format("https://api.github.com/gists/{0}", gistId))
.GetJson((ex, response, body) =>
{
if (ex != null)
{
msg.Send("That's a bad one...");
return;
}
foreach (var gistFile in body["files"])
{
var script = (string)gistFile.Children().First()["content"];
var name = (string)gistFile.Children().First()["filename"];
if (!name.EndsWith(".csx"))
{
name += ".csx";
}
var filePath = Path.Combine(Environment.CurrentDirectory, Path.Combine("scripts", name));
File.WriteAllText(filePath, script);
try
{
robot.LoadScriptFile(name, filePath);
msg.Send(string.Format("Successfully added script: {0}", name));
}
catch (Exception scriptEx)
{
if (File.Exists(filePath))
{
//clean up
try
{
File.Delete(filePath);
}
catch { }
}
msg.Send(string.Format("Failed to load script: ({0}) - {1}", name, scriptEx.Message));
}
}
});
});
}