Merge pull request #590 from appelemac/math

Math Eval
This commit is contained in:
Master Kwoth 2016-08-22 13:36:46 +02:00 committed by GitHub
commit 9d4648104a
3 changed files with 354 additions and 4 deletions

View File

@ -0,0 +1,84 @@
using Discord;
using Discord.Commands;
using NadekoBot.Attributes;
using NadekoBot.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace NadekoBot.Modules.Searches
{
[Group]
public partial class Searches
{
[LocalizedCommand, LocalizedDescription, LocalizedSummary]
[RequireContext(ContextType.Guild)]
public static async Task Calculate(IMessage msg, [Remainder] string expression)
{
try
{
var expr = new NCalc.Expression(expression, NCalc.EvaluateOptions.IgnoreCase);
expr.EvaluateParameter += Expr_EvaluateParameter;
expr.EvaluateFunction += Expr_EvaluateFunction;
var result = expr.Evaluate();
await msg.Reply(string.Format("Your expression evaluated to: {0}", expr.Error ?? result));
}
catch (Exception e)
{
await msg.Reply($"Your expression failed to evaluate: {e.Message} ");
}
}
private static void Expr_EvaluateFunction(string name, NCalc.FunctionArgs args)
{
switch (name.ToLowerInvariant())
{
}
}
private static void Expr_EvaluateParameter(string name, NCalc.ParameterArgs args)
{
switch (name.ToLowerInvariant()) {
case "pi": args.Result= Math.PI;
break;
case "e": args.Result = Math.E;
break;
}
}
[Command("calcOperations")]
[RequireContext(ContextType.Guild)]
public async Task calcOperations(IMessage msg)
{
StringBuilder builder = new StringBuilder();
var selection = typeof(Math).GetTypeInfo().GetMethods().Except(typeof(object).GetTypeInfo().GetMethods()).Select(x =>
{
var name = x.Name;
if (x.GetParameters().Any())
{
name += " (" + string.Join(", ", x.GetParameters().Select(y => y.IsOptional ? $"[{y.ParameterType.Name + " " + y.Name }]" : y.ParameterType.Name + " " + y.Name)) + ")";
}
return name;
});
foreach (var method in selection) builder.AppendLine(method);
await msg.ReplyLong(builder.ToString());
}
}
class ExpressionContext
{
public double Pi { get; set; } = Math.PI;
}
}

View File

@ -20,13 +20,14 @@
"Microsoft.Extensions.PlatformAbstractions": "1.0.0", "Microsoft.Extensions.PlatformAbstractions": "1.0.0",
"Newtonsoft.Json": "9.0.1", "Newtonsoft.Json": "9.0.1",
"Microsoft.Extensions.DependencyInjection": "1.0.0", "Microsoft.Extensions.DependencyInjection": "1.0.0",
"Discord.Net": "1.0.0-dev", "Discord.Net.Commands": "1.0.0-dev-*",
"Discord.Net.Commands": "1.0.0-dev",
"System.Resources.ResourceWriter": "4.0.0-beta-22816", "System.Resources.ResourceWriter": "4.0.0-beta-22816",
"Google.Apis.YouTube.v3": "1.15.0.582", "Google.Apis.YouTube.v3": "1.15.0.582",
"Google.Apis.Urlshortener.v1": "1.15.0.138", "Google.Apis.Urlshortener.v1": "1.15.0.138",
"System.Diagnostics.Contracts": "4.0.1", "System.Diagnostics.Contracts": "4.0.1",
"NLog": "4.4.0-betaV15", "NLog": "4.4.0-betaV15",
"Discord.Net": "1.0.0-dev-*",
"CoreCLR-NCalc": "2.1.0",
"VideoLibrary": "1.3.4" "VideoLibrary": "1.3.4"
}, },

View File

@ -3,6 +3,26 @@
"version": 2, "version": 2,
"targets": { "targets": {
".NETCoreApp,Version=v1.0": { ".NETCoreApp,Version=v1.0": {
"CoreCLR-NCalc/2.1.0": {
"type": "package",
"dependencies": {
"NETStandard.Library": "1.6.0",
"System.Diagnostics.Debug": "4.0.11",
"System.Linq": "4.1.0",
"System.Linq.Expressions": "4.1.0",
"System.Reflection.TypeExtensions": "4.1.0",
"System.Runtime.Extensions": "4.1.0",
"System.Runtime.Serialization.Xml": "4.1.1",
"System.Text.RegularExpressions": "4.1.0",
"System.Threading.Thread": "4.0.0"
},
"compile": {
"lib/netstandard1.3/NCalc.dll": {}
},
"runtime": {
"lib/netstandard1.3/NCalc.dll": {}
}
},
"Google.Apis/1.15.0": { "Google.Apis/1.15.0": {
"type": "package", "type": "package",
"dependencies": { "dependencies": {
@ -1669,6 +1689,41 @@
"lib/netstandard1.3/System.ObjectModel.dll": {} "lib/netstandard1.3/System.ObjectModel.dll": {}
} }
}, },
"System.Private.DataContractSerialization/4.1.1": {
"type": "package",
"dependencies": {
"System.Collections": "4.0.11",
"System.Collections.Concurrent": "4.0.12",
"System.Diagnostics.Debug": "4.0.11",
"System.Globalization": "4.0.11",
"System.IO": "4.1.0",
"System.Linq": "4.1.0",
"System.Reflection": "4.1.0",
"System.Reflection.Emit.ILGeneration": "4.0.1",
"System.Reflection.Emit.Lightweight": "4.0.1",
"System.Reflection.Extensions": "4.0.1",
"System.Reflection.Primitives": "4.0.1",
"System.Reflection.TypeExtensions": "4.1.0",
"System.Resources.ResourceManager": "4.0.1",
"System.Runtime": "4.1.0",
"System.Runtime.Extensions": "4.1.0",
"System.Runtime.Serialization.Primitives": "4.1.1",
"System.Text.Encoding": "4.0.11",
"System.Text.Encoding.Extensions": "4.0.11",
"System.Text.RegularExpressions": "4.1.0",
"System.Threading": "4.0.11",
"System.Threading.Tasks": "4.0.11",
"System.Xml.ReaderWriter": "4.0.11",
"System.Xml.XmlDocument": "4.0.1",
"System.Xml.XmlSerializer": "4.0.11"
},
"compile": {
"ref/netstandard/_._": {}
},
"runtime": {
"lib/netstandard1.3/System.Private.DataContractSerialization.dll": {}
}
},
"System.Reflection/4.1.0": { "System.Reflection/4.1.0": {
"type": "package", "type": "package",
"dependencies": { "dependencies": {
@ -1986,6 +2041,23 @@
"lib/netstandard1.3/System.Runtime.Serialization.Primitives.dll": {} "lib/netstandard1.3/System.Runtime.Serialization.Primitives.dll": {}
} }
}, },
"System.Runtime.Serialization.Xml/4.1.1": {
"type": "package",
"dependencies": {
"System.IO": "4.1.0",
"System.Private.DataContractSerialization": "4.1.1",
"System.Runtime": "4.1.0",
"System.Runtime.Serialization.Primitives": "4.1.1",
"System.Text.Encoding": "4.0.11",
"System.Xml.ReaderWriter": "4.0.11"
},
"compile": {
"ref/netstandard1.3/System.Runtime.Serialization.Xml.dll": {}
},
"runtime": {
"lib/netstandard1.3/System.Runtime.Serialization.Xml.dll": {}
}
},
"System.Security.Claims/4.0.1": { "System.Security.Claims/4.0.1": {
"type": "package", "type": "package",
"dependencies": { "dependencies": {
@ -2542,6 +2614,34 @@
"lib/netstandard1.3/System.Xml.XmlDocument.dll": {} "lib/netstandard1.3/System.Xml.XmlDocument.dll": {}
} }
}, },
"System.Xml.XmlSerializer/4.0.11": {
"type": "package",
"dependencies": {
"System.Collections": "4.0.11",
"System.Globalization": "4.0.11",
"System.IO": "4.1.0",
"System.Linq": "4.1.0",
"System.Reflection": "4.1.0",
"System.Reflection.Emit": "4.0.1",
"System.Reflection.Emit.ILGeneration": "4.0.1",
"System.Reflection.Extensions": "4.0.1",
"System.Reflection.Primitives": "4.0.1",
"System.Reflection.TypeExtensions": "4.1.0",
"System.Resources.ResourceManager": "4.0.1",
"System.Runtime": "4.1.0",
"System.Runtime.Extensions": "4.1.0",
"System.Text.RegularExpressions": "4.1.0",
"System.Threading": "4.0.11",
"System.Xml.ReaderWriter": "4.0.11",
"System.Xml.XmlDocument": "4.0.1"
},
"compile": {
"ref/netstandard1.3/_._": {}
},
"runtime": {
"lib/netstandard1.3/System.Xml.XmlSerializer.dll": {}
}
},
"System.Xml.XPath/4.0.1": { "System.Xml.XPath/4.0.1": {
"type": "package", "type": "package",
"dependencies": { "dependencies": {
@ -2634,6 +2734,17 @@
} }
}, },
"libraries": { "libraries": {
"CoreCLR-NCalc/2.1.0": {
"sha512": "GUPPo99NUeAgLR5oIOLrApJx3Mx5BZEaKkK9OlDd/CmAYaACLHo68FnO+kCamsLH2+rvr6Rw3hAwzap4GVFV8Q==",
"type": "package",
"path": "CoreCLR-NCalc/2.1.0",
"files": [
"CoreCLR-NCalc.2.1.0.nupkg.sha512",
"CoreCLR-NCalc.nuspec",
"lib/net451/NCalc.dll",
"lib/netstandard1.3/NCalc.dll"
]
},
"Google.Apis/1.15.0": { "Google.Apis/1.15.0": {
"sha512": "B//vbZgUsR0jdJztCJ0ORmVcAzhoiisIsxwc1libVjoZzu+kxUKNJKUl5Wlkj7V28kauS56y3hJUj3FMsgaJZQ==", "sha512": "B//vbZgUsR0jdJztCJ0ORmVcAzhoiisIsxwc1libVjoZzu+kxUKNJKUl5Wlkj7V28kauS56y3hJUj3FMsgaJZQ==",
"type": "package", "type": "package",
@ -5777,6 +5888,20 @@
"ref/xamarinwatchos10/_._" "ref/xamarinwatchos10/_._"
] ]
}, },
"System.Private.DataContractSerialization/4.1.1": {
"sha512": "lcqFBUaCZxPiUkA4dlSOoPZGtZsAuuElH2XHgLwGLxd7ZozWetV5yiz0qGAV2AUYOqw97MtZBjbLMN16Xz4vXA==",
"type": "package",
"path": "System.Private.DataContractSerialization/4.1.1",
"files": [
"System.Private.DataContractSerialization.4.1.1.nupkg.sha512",
"System.Private.DataContractSerialization.nuspec",
"ThirdPartyNotices.txt",
"dotnet_library_license.txt",
"lib/netstandard1.3/System.Private.DataContractSerialization.dll",
"ref/netstandard/_._",
"runtimes/aot/lib/netcore50/System.Private.DataContractSerialization.dll"
]
},
"System.Reflection/4.1.0": { "System.Reflection/4.1.0": {
"sha512": "JCKANJ0TI7kzoQzuwB/OoJANy1Lg338B6+JVacPl4TpUwi3cReg3nMLplMq2uqYfHFQpKIlHAUVAJlImZz/4ng==", "sha512": "JCKANJ0TI7kzoQzuwB/OoJANy1Lg338B6+JVacPl4TpUwi3cReg3nMLplMq2uqYfHFQpKIlHAUVAJlImZz/4ng==",
"type": "package", "type": "package",
@ -6717,6 +6842,76 @@
"runtimes/aot/lib/netcore50/System.Runtime.Serialization.Primitives.dll" "runtimes/aot/lib/netcore50/System.Runtime.Serialization.Primitives.dll"
] ]
}, },
"System.Runtime.Serialization.Xml/4.1.1": {
"sha512": "yqfKHkWUAdI0hdDIdD9KDzluKtZ8IIqLF3O7xIZlt6UTs1bOvFRpCvRTvGQva3Ak/ZM9/nq9IHBJ1tC4Ybcrjg==",
"type": "package",
"path": "System.Runtime.Serialization.Xml/4.1.1",
"files": [
"System.Runtime.Serialization.Xml.4.1.1.nupkg.sha512",
"System.Runtime.Serialization.Xml.nuspec",
"ThirdPartyNotices.txt",
"dotnet_library_license.txt",
"lib/MonoAndroid10/_._",
"lib/MonoTouch10/_._",
"lib/net45/_._",
"lib/net46/System.Runtime.Serialization.Xml.dll",
"lib/netcore50/System.Runtime.Serialization.Xml.dll",
"lib/netstandard1.3/System.Runtime.Serialization.Xml.dll",
"lib/portable-net45+win8+wp8+wpa81/_._",
"lib/win8/_._",
"lib/wp80/_._",
"lib/wpa81/_._",
"lib/xamarinios10/_._",
"lib/xamarinmac20/_._",
"lib/xamarintvos10/_._",
"lib/xamarinwatchos10/_._",
"ref/MonoAndroid10/_._",
"ref/MonoTouch10/_._",
"ref/net45/_._",
"ref/net46/System.Runtime.Serialization.Xml.dll",
"ref/netcore50/System.Runtime.Serialization.Xml.dll",
"ref/netcore50/System.Runtime.Serialization.Xml.xml",
"ref/netcore50/de/System.Runtime.Serialization.Xml.xml",
"ref/netcore50/es/System.Runtime.Serialization.Xml.xml",
"ref/netcore50/fr/System.Runtime.Serialization.Xml.xml",
"ref/netcore50/it/System.Runtime.Serialization.Xml.xml",
"ref/netcore50/ja/System.Runtime.Serialization.Xml.xml",
"ref/netcore50/ko/System.Runtime.Serialization.Xml.xml",
"ref/netcore50/ru/System.Runtime.Serialization.Xml.xml",
"ref/netcore50/zh-hans/System.Runtime.Serialization.Xml.xml",
"ref/netcore50/zh-hant/System.Runtime.Serialization.Xml.xml",
"ref/netstandard1.0/System.Runtime.Serialization.Xml.dll",
"ref/netstandard1.0/System.Runtime.Serialization.Xml.xml",
"ref/netstandard1.0/de/System.Runtime.Serialization.Xml.xml",
"ref/netstandard1.0/es/System.Runtime.Serialization.Xml.xml",
"ref/netstandard1.0/fr/System.Runtime.Serialization.Xml.xml",
"ref/netstandard1.0/it/System.Runtime.Serialization.Xml.xml",
"ref/netstandard1.0/ja/System.Runtime.Serialization.Xml.xml",
"ref/netstandard1.0/ko/System.Runtime.Serialization.Xml.xml",
"ref/netstandard1.0/ru/System.Runtime.Serialization.Xml.xml",
"ref/netstandard1.0/zh-hans/System.Runtime.Serialization.Xml.xml",
"ref/netstandard1.0/zh-hant/System.Runtime.Serialization.Xml.xml",
"ref/netstandard1.3/System.Runtime.Serialization.Xml.dll",
"ref/netstandard1.3/System.Runtime.Serialization.Xml.xml",
"ref/netstandard1.3/de/System.Runtime.Serialization.Xml.xml",
"ref/netstandard1.3/es/System.Runtime.Serialization.Xml.xml",
"ref/netstandard1.3/fr/System.Runtime.Serialization.Xml.xml",
"ref/netstandard1.3/it/System.Runtime.Serialization.Xml.xml",
"ref/netstandard1.3/ja/System.Runtime.Serialization.Xml.xml",
"ref/netstandard1.3/ko/System.Runtime.Serialization.Xml.xml",
"ref/netstandard1.3/ru/System.Runtime.Serialization.Xml.xml",
"ref/netstandard1.3/zh-hans/System.Runtime.Serialization.Xml.xml",
"ref/netstandard1.3/zh-hant/System.Runtime.Serialization.Xml.xml",
"ref/portable-net45+win8+wp8+wpa81/_._",
"ref/win8/_._",
"ref/wp80/_._",
"ref/wpa81/_._",
"ref/xamarinios10/_._",
"ref/xamarinmac20/_._",
"ref/xamarintvos10/_._",
"ref/xamarinwatchos10/_._"
]
},
"System.Security.Claims/4.0.1": { "System.Security.Claims/4.0.1": {
"sha512": "4Jlp0OgJLS/Voj1kyFP6MJlIYp3crgfH8kNQk2p7+4JYfc1aAmh9PZyAMMbDhuoolGNtux9HqSOazsioRiDvCw==", "sha512": "4Jlp0OgJLS/Voj1kyFP6MJlIYp3crgfH8kNQk2p7+4JYfc1aAmh9PZyAMMbDhuoolGNtux9HqSOazsioRiDvCw==",
"type": "package", "type": "package",
@ -7866,6 +8061,75 @@
"ref/xamarinwatchos10/_._" "ref/xamarinwatchos10/_._"
] ]
}, },
"System.Xml.XmlSerializer/4.0.11": {
"sha512": "FrazwwqfIXTfq23mfv4zH+BjqkSFNaNFBtjzu3I9NRmG8EELYyrv/fJnttCIwRMFRR/YKXF1hmsMmMEnl55HGw==",
"type": "package",
"path": "System.Xml.XmlSerializer/4.0.11",
"files": [
"System.Xml.XmlSerializer.4.0.11.nupkg.sha512",
"System.Xml.XmlSerializer.nuspec",
"ThirdPartyNotices.txt",
"dotnet_library_license.txt",
"lib/MonoAndroid10/_._",
"lib/MonoTouch10/_._",
"lib/net45/_._",
"lib/netcore50/System.Xml.XmlSerializer.dll",
"lib/netstandard1.3/System.Xml.XmlSerializer.dll",
"lib/portable-net45+win8+wp8+wpa81/_._",
"lib/win8/_._",
"lib/wp80/_._",
"lib/wpa81/_._",
"lib/xamarinios10/_._",
"lib/xamarinmac20/_._",
"lib/xamarintvos10/_._",
"lib/xamarinwatchos10/_._",
"ref/MonoAndroid10/_._",
"ref/MonoTouch10/_._",
"ref/net45/_._",
"ref/netcore50/System.Xml.XmlSerializer.dll",
"ref/netcore50/System.Xml.XmlSerializer.xml",
"ref/netcore50/de/System.Xml.XmlSerializer.xml",
"ref/netcore50/es/System.Xml.XmlSerializer.xml",
"ref/netcore50/fr/System.Xml.XmlSerializer.xml",
"ref/netcore50/it/System.Xml.XmlSerializer.xml",
"ref/netcore50/ja/System.Xml.XmlSerializer.xml",
"ref/netcore50/ko/System.Xml.XmlSerializer.xml",
"ref/netcore50/ru/System.Xml.XmlSerializer.xml",
"ref/netcore50/zh-hans/System.Xml.XmlSerializer.xml",
"ref/netcore50/zh-hant/System.Xml.XmlSerializer.xml",
"ref/netstandard1.0/System.Xml.XmlSerializer.dll",
"ref/netstandard1.0/System.Xml.XmlSerializer.xml",
"ref/netstandard1.0/de/System.Xml.XmlSerializer.xml",
"ref/netstandard1.0/es/System.Xml.XmlSerializer.xml",
"ref/netstandard1.0/fr/System.Xml.XmlSerializer.xml",
"ref/netstandard1.0/it/System.Xml.XmlSerializer.xml",
"ref/netstandard1.0/ja/System.Xml.XmlSerializer.xml",
"ref/netstandard1.0/ko/System.Xml.XmlSerializer.xml",
"ref/netstandard1.0/ru/System.Xml.XmlSerializer.xml",
"ref/netstandard1.0/zh-hans/System.Xml.XmlSerializer.xml",
"ref/netstandard1.0/zh-hant/System.Xml.XmlSerializer.xml",
"ref/netstandard1.3/System.Xml.XmlSerializer.dll",
"ref/netstandard1.3/System.Xml.XmlSerializer.xml",
"ref/netstandard1.3/de/System.Xml.XmlSerializer.xml",
"ref/netstandard1.3/es/System.Xml.XmlSerializer.xml",
"ref/netstandard1.3/fr/System.Xml.XmlSerializer.xml",
"ref/netstandard1.3/it/System.Xml.XmlSerializer.xml",
"ref/netstandard1.3/ja/System.Xml.XmlSerializer.xml",
"ref/netstandard1.3/ko/System.Xml.XmlSerializer.xml",
"ref/netstandard1.3/ru/System.Xml.XmlSerializer.xml",
"ref/netstandard1.3/zh-hans/System.Xml.XmlSerializer.xml",
"ref/netstandard1.3/zh-hant/System.Xml.XmlSerializer.xml",
"ref/portable-net45+win8+wp8+wpa81/_._",
"ref/win8/_._",
"ref/wp80/_._",
"ref/wpa81/_._",
"ref/xamarinios10/_._",
"ref/xamarinmac20/_._",
"ref/xamarintvos10/_._",
"ref/xamarinwatchos10/_._",
"runtimes/aot/lib/netcore50/System.Xml.XmlSerializer.dll"
]
},
"System.Xml.XPath/4.0.1": { "System.Xml.XPath/4.0.1": {
"sha512": "UWd1H+1IJ9Wlq5nognZ/XJdyj8qPE4XufBUkAW59ijsCPjZkZe0MUzKKJFBr+ZWBe5Wq1u1d5f2CYgE93uH7DA==", "sha512": "UWd1H+1IJ9Wlq5nognZ/XJdyj8qPE4XufBUkAW59ijsCPjZkZe0MUzKKJFBr+ZWBe5Wq1u1d5f2CYgE93uH7DA==",
"type": "package", "type": "package",
@ -7963,8 +8227,9 @@
}, },
"projectFileDependencyGroups": { "projectFileDependencyGroups": {
"": [ "": [
"Discord.Net >= 1.0.0-dev", "CoreCLR-NCalc >= 2.1.0",
"Discord.Net.Commands >= 1.0.0-dev", "Discord.Net >= 1.0.0-dev-*",
"Discord.Net.Commands >= 1.0.0-dev-*",
"Google.Apis.Urlshortener.v1 >= 1.15.0.138", "Google.Apis.Urlshortener.v1 >= 1.15.0.138",
"Google.Apis.YouTube.v3 >= 1.15.0.582", "Google.Apis.YouTube.v3 >= 1.15.0.582",
"Microsoft.Extensions.DependencyInjection >= 1.0.0", "Microsoft.Extensions.DependencyInjection >= 1.0.0",