2016-12-17 00:16:14 +00:00
|
|
|
|
using Discord.Commands;
|
2016-10-10 01:12:37 +00:00
|
|
|
|
using NadekoBot.Extensions;
|
|
|
|
|
using System;
|
2016-12-08 15:40:59 +00:00
|
|
|
|
using System.Collections.Generic;
|
2016-10-10 01:12:37 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Threading.Tasks;
|
2017-07-17 19:42:36 +00:00
|
|
|
|
using NadekoBot.Common.Attributes;
|
2016-10-10 01:12:37 +00:00
|
|
|
|
|
|
|
|
|
namespace NadekoBot.Modules.Utility
|
|
|
|
|
{
|
|
|
|
|
public partial class Utility
|
|
|
|
|
{
|
2016-12-17 00:21:05 +00:00
|
|
|
|
[Group]
|
2017-02-23 02:06:37 +00:00
|
|
|
|
public class CalcCommands : NadekoSubmodule
|
2016-10-10 01:12:37 +00:00
|
|
|
|
{
|
2016-12-17 00:21:05 +00:00
|
|
|
|
[NadekoCommand, Usage, Description, Aliases]
|
|
|
|
|
public async Task Calculate([Remainder] string expression)
|
|
|
|
|
{
|
|
|
|
|
var expr = new NCalc.Expression(expression, NCalc.EvaluateOptions.IgnoreCase);
|
|
|
|
|
expr.EvaluateParameter += Expr_EvaluateParameter;
|
|
|
|
|
var result = expr.Evaluate();
|
|
|
|
|
if (expr.Error == null)
|
2017-02-23 02:06:37 +00:00
|
|
|
|
await Context.Channel.SendConfirmAsync("⚙ " + GetText("result"), result.ToString());
|
2016-12-17 00:21:05 +00:00
|
|
|
|
else
|
2017-02-23 02:06:37 +00:00
|
|
|
|
await Context.Channel.SendErrorAsync("⚙ " + GetText("error"), expr.Error);
|
2016-12-17 00:21:05 +00:00
|
|
|
|
}
|
2016-10-10 01:12:37 +00:00
|
|
|
|
|
2016-12-17 00:21:05 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
2016-10-10 01:12:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-12-17 00:21:05 +00:00
|
|
|
|
[NadekoCommand, Usage, Description, Aliases]
|
|
|
|
|
public async Task CalcOps()
|
2016-10-10 01:12:37 +00:00
|
|
|
|
{
|
2017-02-23 02:06:37 +00:00
|
|
|
|
var selection = typeof(Math).GetTypeInfo()
|
|
|
|
|
.GetMethods()
|
|
|
|
|
.Distinct(new MethodInfoEqualityComparer())
|
|
|
|
|
.Select(x => x.Name)
|
|
|
|
|
.Except(new[]
|
|
|
|
|
{
|
|
|
|
|
"ToString",
|
|
|
|
|
"Equals",
|
|
|
|
|
"GetHashCode",
|
|
|
|
|
"GetType"
|
|
|
|
|
});
|
2017-03-03 02:08:55 +00:00
|
|
|
|
await Context.Channel.SendConfirmAsync(GetText("calcops", Prefix), string.Join(", ", selection));
|
2016-12-17 00:21:05 +00:00
|
|
|
|
}
|
2016-10-10 01:12:37 +00:00
|
|
|
|
}
|
2016-12-08 15:40:59 +00:00
|
|
|
|
|
2017-02-23 02:06:37 +00:00
|
|
|
|
private class MethodInfoEqualityComparer : IEqualityComparer<MethodInfo>
|
2016-12-17 00:21:05 +00:00
|
|
|
|
{
|
|
|
|
|
public bool Equals(MethodInfo x, MethodInfo y) => x.Name == y.Name;
|
2016-12-08 15:40:59 +00:00
|
|
|
|
|
2016-12-17 00:21:05 +00:00
|
|
|
|
public int GetHashCode(MethodInfo obj) => obj.Name.GetHashCode();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|