cs-codex-dist-tests/Tools/BiblioTech/Options/EthAddressOption.cs

43 lines
1.5 KiB
C#
Raw Permalink Normal View History

2023-10-24 13:25:45 +00:00
using GethPlugin;
2023-10-22 09:26:00 +00:00
using Nethereum.Util;
2023-10-20 07:49:23 +00:00
2023-10-24 13:25:45 +00:00
namespace BiblioTech.Options
2023-10-20 07:49:23 +00:00
{
public class EthAddressOption : CommandOption
{
2023-10-25 09:25:27 +00:00
public EthAddressOption(bool isRequired)
: base(name: "ethaddress",
2023-10-20 07:49:23 +00:00
description: "Ethereum address starting with '0x'.",
2023-10-22 08:10:52 +00:00
type: Discord.ApplicationCommandOptionType.String,
2023-10-25 09:25:27 +00:00
isRequired)
2023-10-20 07:49:23 +00:00
{
}
2023-10-24 13:25:45 +00:00
public async Task<EthAddress?> Parse(CommandContext context)
2023-10-20 07:49:23 +00:00
{
2023-10-24 13:25:45 +00:00
var ethOptionData = context.Options.SingleOrDefault(o => o.Name == Name);
2023-10-20 07:49:23 +00:00
if (ethOptionData == null)
{
2023-10-25 08:54:26 +00:00
await context.Followup("EthAddress option not received.");
2023-10-20 07:49:23 +00:00
return null;
}
var ethAddressStr = ethOptionData.Value as string;
if (string.IsNullOrEmpty(ethAddressStr))
{
2023-10-25 08:54:26 +00:00
await context.Followup("EthAddress is null or empty.");
2023-10-22 09:26:00 +00:00
return null;
}
if (!AddressUtil.Current.IsValidAddressLength(ethAddressStr) ||
2023-10-25 08:25:00 +00:00
!AddressUtil.Current.IsValidEthereumAddressHexFormat(ethAddressStr))
// !AddressUtil.Current.IsChecksumAddress(ethAddressStr)) - this might make a good option later, but for now it might just annoy users.
2023-10-22 09:26:00 +00:00
{
2023-10-25 08:54:26 +00:00
await context.Followup("EthAddress is not valid.");
2023-10-20 07:49:23 +00:00
return null;
}
return new EthAddress(ethAddressStr);
}
}
}