2
0
mirror of synced 2025-01-28 01:06:15 +00:00

43 lines
1.5 KiB
C#
Raw Normal View History

2023-10-24 15:25:45 +02:00
using GethPlugin;
2023-10-22 11:26:00 +02:00
using Nethereum.Util;
2023-10-20 09:49:23 +02:00
2023-10-24 15:25:45 +02:00
namespace BiblioTech.Options
2023-10-20 09:49:23 +02:00
{
public class EthAddressOption : CommandOption
{
2023-10-25 11:25:27 +02:00
public EthAddressOption(bool isRequired)
: base(name: "ethaddress",
2023-10-20 09:49:23 +02:00
description: "Ethereum address starting with '0x'.",
2023-10-22 10:10:52 +02:00
type: Discord.ApplicationCommandOptionType.String,
2023-10-25 11:25:27 +02:00
isRequired)
2023-10-20 09:49:23 +02:00
{
}
2023-10-24 15:25:45 +02:00
public async Task<EthAddress?> Parse(CommandContext context)
2023-10-20 09:49:23 +02:00
{
2023-10-24 15:25:45 +02:00
var ethOptionData = context.Options.SingleOrDefault(o => o.Name == Name);
2023-10-20 09:49:23 +02:00
if (ethOptionData == null)
{
2023-10-25 10:54:26 +02:00
await context.Followup("EthAddress option not received.");
2023-10-20 09:49:23 +02:00
return null;
}
var ethAddressStr = ethOptionData.Value as string;
if (string.IsNullOrEmpty(ethAddressStr))
{
2023-10-25 10:54:26 +02:00
await context.Followup("EthAddress is null or empty.");
2023-10-22 11:26:00 +02:00
return null;
}
if (!AddressUtil.Current.IsValidAddressLength(ethAddressStr) ||
2023-10-25 10:25:00 +02: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 11:26:00 +02:00
{
2023-10-25 10:54:26 +02:00
await context.Followup("EthAddress is not valid.");
2023-10-20 09:49:23 +02:00
return null;
}
return new EthAddress(ethAddressStr);
}
}
}