示例#1
0
        public static async Task <(string result, bool isCode)> AddMethod(string arguments)
        {
            var code = CleanupCode(arguments);

            var match = AddCommandRegex.Match(code);

            if (!match.Success)
            {
                return("Usage: +fun <named function>", false);
            }

            var name     = match.Groups["name"].Value;
            var testCode = $"{code};\nreturn {name};";

            var result = (await RunModule.Run(testCode)).Output.Trim();

            if (result.StartsWith("ERROR:") || result.StartsWith("EXCEPTION:") || result.StartsWith("mondbox"))
            {
                return(result, true);
            }

            var methodName = name;

            if (name[0] == '(')
            {
                if (!Shared.Util.TryConvertOperatorName(name.Substring(1, name.Length - 2), out methodName))
                {
                    return("Failed to get method name of operator.", false);
                }
            }

            var cmd = new SqlCommand(@"INSERT INTO mondbot.variables (name, type, data, version) VALUES (:name, :type, :data, 2)
                                       ON CONFLICT (name) DO UPDATE SET type = :type, data = :data, version = 2;")
            {
                ["name"] = methodName,
                ["type"] = (int)VariableType.Method,
                ["data"] = code
            };

            using (cmd)
            {
                await cmd.ExecuteNonQuery();
            }

            return("Successfully updated method!", false);
        }
示例#2
0
        public static async Task <(byte[] image, string result)> RunScript(string code)
        {
            code = CleanupCode(code);

            if (string.IsNullOrWhiteSpace(code))
            {
                return(null, null);
            }

            var result = await RunModule.Run(code + ";");

            var image = result.Image;

            if (result.Image != null && result.Image.Length == 0)
            {
                image = null;
            }

            return(image, result.Output);
        }