vassago/Behavior/UnitConvert.cs

34 lines
1.2 KiB
C#
Raw Normal View History

2023-06-19 01:14:04 -04:00
namespace vassago.Behavior;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using vassago.Models;
2023-06-20 21:26:44 -04:00
[StaticPlz]
2023-06-19 01:14:04 -04:00
public class UnitConvert : Behavior
{
public override string Name => "Unit conversion";
public override string Trigger => "!freedomunits";
public override string Description => "convert between many units.";
2023-06-19 11:03:06 -04:00
public override async Task<bool> ActOn(Message message)
2023-06-19 01:14:04 -04:00
{
2023-12-01 14:19:55 -05:00
var theseMatches = Regex.Matches(message.Content, "\\s(-?[\\d]+\\.?\\d*) ?([^\\d\\s].*) (in|to|as) ([^\\d\\s].*)$", RegexOptions.IgnoreCase);
2023-06-19 01:14:04 -04:00
if (theseMatches != null && theseMatches.Count > 0 && theseMatches[0].Groups != null && theseMatches[0].Groups.Count == 5)
{
decimal asNumeric = 0;
if (decimal.TryParse(theseMatches[0].Groups[1].Value, out asNumeric))
{
await message.Channel.SendMessage(Conversion.Converter.Convert(asNumeric, theseMatches[0].Groups[2].Value, theseMatches[0].Groups[4].Value.ToLower()));
2023-06-20 21:38:25 -04:00
return true;
2023-06-19 01:14:04 -04:00
}
await message.Channel.SendMessage("mysteriously semi-parsable");
2023-06-20 21:38:25 -04:00
return true;
2023-06-19 01:14:04 -04:00
}
await message.Channel.SendMessage( "unparsable");
return true;
}
}