forked from mmbot/mmbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScriptsScripts.cs
135 lines (121 loc) · 4.71 KB
/
ScriptsScripts.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
using System.IO;
using MMBot.Scripts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MMBot.ScriptIt
{
public class ScriptsScripts : IMMBotScript
{
public void Register(Robot robot)
{
robot.Respond(@"scriptthis (.*):(.*)", async msg =>
{
if (!msg.Message.User.IsAdmin(robot))
{
msg.Send("You must be an admin to run this command");
return;
}
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 =>
{
if (!msg.Message.User.IsAdmin(robot))
{
msg.Send("You must be an admin to run this command");
return;
}
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));
}
}
});
});
}
public IEnumerable<string> GetHelp()
{
return new[]
{
"mmbot scriptthis <name>:<script> - creates a new mmbot script",
"mmbot scriptthis <gist url> - creates a new mmbot script from a github gist"
};
}
}
}