This repository has been archived on 2024-06-23. You can view files and clone it, but cannot push or open issues or pull requests.
OctobotStealth/src/Data/Options/SnowflakeOption.cs
Octol1ttle f47ebe81c5
Do not wrap result errors when returning them (#73)
This PR removes the wrapping of in-house created result errors with
`Result.FromError` (I did not know this was possible until today, lol),
which reduces code verbosity and makes it easier to read, and replaces
`ArgumentNullError` with `ArgumentInvalidError` when retrieving IDs from
a command context, which, in my opinion, is more correct.

---------

Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com>
2023-07-28 16:58:55 +00:00

31 lines
1.1 KiB
C#

using System.Text.Json.Nodes;
using System.Text.RegularExpressions;
using Remora.Discord.Extensions.Formatting;
using Remora.Rest.Core;
using Remora.Results;
namespace Boyfriend.Data.Options;
public partial class SnowflakeOption : Option<Snowflake> {
public SnowflakeOption(string name) : base(name, 0UL.ToSnowflake()) { }
public override string Display(JsonNode settings) {
return Name.EndsWith("Channel") ? Mention.Channel(Get(settings)) : Mention.Role(Get(settings));
}
public override Snowflake Get(JsonNode settings) {
var property = settings[Name];
return property != null ? property.GetValue<ulong>().ToSnowflake() : DefaultValue;
}
public override Result Set(JsonNode settings, string from) {
if (!ulong.TryParse(NonNumbers().Replace(from, ""), out var parsed))
return new ArgumentInvalidError(nameof(from), Messages.InvalidSettingValue);
settings[Name] = parsed;
return Result.FromSuccess();
}
[GeneratedRegex("[^0-9]")]
private static partial Regex NonNumbers();
}