diff --git a/NadekoBot/Modules/Searches/Commands/EvalCommand.cs b/NadekoBot/Modules/Searches/Commands/EvalCommand.cs new file mode 100644 index 00000000..6ff59345 --- /dev/null +++ b/NadekoBot/Modules/Searches/Commands/EvalCommand.cs @@ -0,0 +1,85 @@ +using NadekoBot.Classes; +using System; +using Mathos.Parser; +using System.Threading.Tasks; +using Discord.Commands; +using System.Text.RegularExpressions; + +namespace NadekoBot.Modules.Searches.Commands +{ + class EvalCommand : DiscordCommand + { + public EvalCommand(DiscordModule module) : base(module) + { + } + + internal override void Init(CommandGroupBuilder cgb) + { + cgb.CreateCommand(Module.Prefix + "calculate") + .Alias(Module.Prefix + "calc") + .Description("Evaluate a mathematical expression.\n**Usage**: ~calc 1+1") + .Parameter("expression", ParameterType.Unparsed) + .Do(EvalFunc()); + } + + + private CustomParser parser = new CustomParser(); + private Func EvalFunc() => async e => + { + string expression = e.GetArg("expression")?.Trim(); + if (string.IsNullOrWhiteSpace(expression)) + { + return; + } + string answer = Evaluate(expression); + if (answer == null) + { + await e.Channel.SendMessage($"Expression {expression} failed to evaluate"); + return; + } + await e.Channel.SendMessage($"`result: {answer}`"); + }; + + private string Evaluate(string expression) + { + //check for factorial + expression = Regex.Replace(expression, @"\d+!", x => x.Value + "0"); + try + { + string result = parser.Parse(expression).ToString(); + return result; + } + catch (OverflowException e) + { + return $"Overflow error on {expression}"; + } + catch (FormatException e) + { + return $"\"{expression}\" was not formatted correctly"; + } + } + + + + class CustomParser : MathParser + { + public CustomParser() : base() + { + OperatorList.Add("!"); + OperatorAction.Add("!", (x, y) => Factorial(x)); + } + + static decimal Factorial(decimal x) + { + decimal y = x-1; + while (y >0) + { + x = x * y--; + } + return x; + } + } + + + } +} diff --git a/NadekoBot/Modules/Searches/SearchesModule.cs b/NadekoBot/Modules/Searches/SearchesModule.cs index 5eeeef50..ce6afbf4 100644 --- a/NadekoBot/Modules/Searches/SearchesModule.cs +++ b/NadekoBot/Modules/Searches/SearchesModule.cs @@ -27,6 +27,8 @@ namespace NadekoBot.Modules.Searches commands.Add(new StreamNotifications(this)); commands.Add(new ConverterCommand(this)); commands.Add(new RedditCommand(this)); + commands.Add(new WowJokeCommand(this)); + commands.Add(new EvalCommand(this)); commands.Add(new WowJokeCommand(this)); rng = new Random(); } diff --git a/NadekoBot/NadekoBot.csproj b/NadekoBot/NadekoBot.csproj index 234f5b30..ad1ed62c 100644 --- a/NadekoBot/NadekoBot.csproj +++ b/NadekoBot/NadekoBot.csproj @@ -99,6 +99,10 @@ ..\packages\Manatee.Trello.WebApi.1.0.1\lib\net45\Manatee.Trello.WebApi.dll True + + ..\packages\MathosParser.1.0.10.1\lib\MathParser.dll + True + ..\packages\Newtonsoft.Json.8.0.3\lib\net45\Newtonsoft.Json.dll True @@ -138,6 +142,7 @@ + diff --git a/NadekoBot/packages.config b/NadekoBot/packages.config index 055f7b1d..9a7951bf 100644 --- a/NadekoBot/packages.config +++ b/NadekoBot/packages.config @@ -5,6 +5,7 @@ + diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj index 0ddc6cc5..2a99410a 100644 --- a/Tests/Tests.csproj +++ b/Tests/Tests.csproj @@ -87,4 +87,4 @@ --> - \ No newline at end of file +