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

44 lines
1.4 KiB
C#
Raw Normal View History

2023-10-20 07:49:23 +00:00
using Discord.WebSocket;
using GethPlugin;
2023-10-22 09:26:00 +00:00
using Nethereum.Util;
2023-10-20 07:49:23 +00:00
2023-10-22 08:38:46 +00:00
namespace BiblioTech.Commands
2023-10-20 07:49:23 +00:00
{
public class EthAddressOption : CommandOption
{
public EthAddressOption()
: 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,
isRequired: true)
2023-10-20 07:49:23 +00:00
{
}
public async Task<EthAddress?> Parse(SocketSlashCommand command)
{
var ethOptionData = command.Data.Options.SingleOrDefault(o => o.Name == Name);
if (ethOptionData == null)
{
2023-10-22 09:26:00 +00:00
await command.FollowupAsync("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-22 09:26:00 +00:00
await command.FollowupAsync("EthAddress is null or empty.");
return null;
}
if (!AddressUtil.Current.IsValidAddressLength(ethAddressStr) ||
!AddressUtil.Current.IsValidEthereumAddressHexFormat(ethAddressStr) ||
!AddressUtil.Current.IsChecksumAddress(ethAddressStr))
{
await command.FollowupAsync("EthAddress is not valid.");
2023-10-20 07:49:23 +00:00
return null;
}
return new EthAddress(ethAddressStr);
}
}
}