commit
542b2948d1
85
NadekoBot/Modules/Searches/Commands/EvalCommand.cs
Normal file
85
NadekoBot/Modules/Searches/Commands/EvalCommand.cs
Normal file
@ -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<CommandEventArgs, Task> 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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
@ -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();
|
||||
}
|
||||
|
@ -99,6 +99,10 @@
|
||||
<HintPath>..\packages\Manatee.Trello.WebApi.1.0.1\lib\net45\Manatee.Trello.WebApi.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="MathParser, Version=1.0.10.1, Culture=neutral, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\MathosParser.1.0.10.1\lib\MathParser.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="Newtonsoft.Json, Version=8.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Newtonsoft.Json.8.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
@ -138,6 +142,7 @@
|
||||
<Compile Include="Modules\CustomReactions\CustomReactions.cs" />
|
||||
<Compile Include="Modules\Programming\Commands\HaskellRepl.cs" />
|
||||
<Compile Include="Modules\Programming\ProgrammingModule.cs" />
|
||||
<Compile Include="Modules\Searches\Commands\EvalCommand.cs" />
|
||||
<Compile Include="Modules\Searches\Commands\IMDB\ImdbMovie.cs" />
|
||||
<Compile Include="Modules\Searches\Commands\IMDB\ImdbScraper.cs" />
|
||||
<Compile Include="Classes\IncidentsHandler.cs" />
|
||||
|
@ -5,6 +5,7 @@
|
||||
<package id="Manatee.Trello" version="1.8.2" targetFramework="net452" />
|
||||
<package id="Manatee.Trello.ManateeJson" version="1.4.0" targetFramework="net452" />
|
||||
<package id="Manatee.Trello.WebApi" version="1.0.1" targetFramework="net452" />
|
||||
<package id="MathosParser" version="1.0.10.1" targetFramework="net452" />
|
||||
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net452" />
|
||||
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net452" />
|
||||
<package id="Newtonsoft.Json" version="8.0.3" targetFramework="net46" />
|
||||
|
Loading…
Reference in New Issue
Block a user