Files
nadekobot/src/NadekoBot/Modules/Utility/CalcCommands.cs
Kwoth 723447c7d4 - Updated editorconfig rules to hopefully look a bit nicer.
- Removed configureawait(false) from everywhere as it doesnt' do anything in a console app and just makes the code look ugly
- Started using .WhenAll extension instead of Task.WhenAll to make it look nicer when chaining methods
2021-12-28 21:14:40 +01:00

56 lines
1.7 KiB
C#

#nullable disable
using System.Reflection;
namespace NadekoBot.Modules.Utility;
public partial class Utility
{
[Group]
public class CalcCommands : NadekoSubmodule
{
[NadekoCommand, Aliases]
public async Task Calculate([Leftover] string expression)
{
var expr = new NCalc.Expression(expression, NCalc.EvaluateOptions.IgnoreCase | NCalc.EvaluateOptions.NoCache);
expr.EvaluateParameter += Expr_EvaluateParameter;
var result = expr.Evaluate();
if (!expr.HasErrors())
await SendConfirmAsync("⚙ " + GetText(strs.result), result.ToString());
else
await SendErrorAsync("⚙ " + GetText(strs.error), expr.Error);
}
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;
default:
break;
}
}
[NadekoCommand, Aliases]
public async Task CalcOps()
{
var selection = typeof(Math).GetTypeInfo()
.GetMethods()
.DistinctBy(x => x.Name)
.Select(x => x.Name)
.Except(new[]
{
"ToString",
"Equals",
"GetHashCode",
"GetType"
});
await SendConfirmAsync(GetText(strs.calcops(Prefix)), string.Join(", ", selection));
}
}
}