diff --git a/Tools/BiblioTech/Commands/ClearUserAssociationCommand.cs b/Tools/BiblioTech/Commands/ClearUserAssociationCommand.cs index 62eb649..cc49829 100644 --- a/Tools/BiblioTech/Commands/ClearUserAssociationCommand.cs +++ b/Tools/BiblioTech/Commands/ClearUserAssociationCommand.cs @@ -11,6 +11,7 @@ namespace BiblioTech.Commands public override string Name => "clear"; public override string StartingMessage => "Hold on..."; public override string Description => "Admin only. Clears current Eth address for a user, allowing them to set a new one."; + public override CommandOption[] Options => new[] { user }; protected override async Task Invoke(SocketSlashCommand command) { diff --git a/Tools/BiblioTech/Commands/DeploymentsCommand.cs b/Tools/BiblioTech/Commands/DeploymentsCommand.cs index 3bc44f8..a3cca79 100644 --- a/Tools/BiblioTech/Commands/DeploymentsCommand.cs +++ b/Tools/BiblioTech/Commands/DeploymentsCommand.cs @@ -13,7 +13,7 @@ namespace BiblioTech.Commands public override string Name => "deployments"; public override string StartingMessage => "Fetching deployments information..."; - public override string Description => "Lists known deployments"; + public override string Description => "Lists active TestNet deployments"; protected override async Task Invoke(SocketSlashCommand command) { diff --git a/Tools/BiblioTech/Commands/MintCommand.cs b/Tools/BiblioTech/Commands/MintCommand.cs index 664cba8..31a5f8e 100644 --- a/Tools/BiblioTech/Commands/MintCommand.cs +++ b/Tools/BiblioTech/Commands/MintCommand.cs @@ -7,16 +7,17 @@ namespace BiblioTech.Commands { public class MintCommand : BaseNetCommand { - private readonly string nl = Environment.NewLine; private readonly Ether defaultEthToSend = 10.Eth(); private readonly TestToken defaultTestTokensToMint = 1024.TestTokens(); private readonly UserOption optionalUser = new UserOption( description: "If set, mint tokens for this user. (Optional, admin-only)", - isRequired: true); + isRequired: false); + private readonly UserAssociateCommand userAssociateCommand; - public MintCommand(DeploymentsFilesMonitor monitor, CoreInterface ci) + public MintCommand(DeploymentsFilesMonitor monitor, CoreInterface ci, UserAssociateCommand userAssociateCommand) : base(monitor, ci) { + this.userAssociateCommand = userAssociateCommand; } public override string Name => "mint"; @@ -28,36 +29,57 @@ namespace BiblioTech.Commands { var userId = GetUserId(optionalUser, command); var addr = Program.UserRepo.GetCurrentAddressForUser(userId); - if (addr == null) return; + if (addr == null) + { + await command.FollowupAsync($"No address has been set for this user. Please use '/{userAssociateCommand.Name}' to set it first."); + return; + } - var report = - ProcessEth(gethNode, addr) + - ProcessTestTokens(gethNode, contracts, addr); + var report = new List(); - await command.FollowupAsync(report); + var sentEth = ProcessEth(gethNode, addr, report); + var mintedTokens = ProcessTokens(gethNode, contracts, addr, report); + + Program.UserRepo.AddMintEventForUser(userId, addr, sentEth, mintedTokens); + + await command.FollowupAsync(string.Join(Environment.NewLine, report)); } - private string ProcessTestTokens(IGethNode gethNode, ICodexContracts contracts, EthAddress addr) + private TestToken ProcessTokens(IGethNode gethNode, ICodexContracts contracts, EthAddress addr, List report) { - var testTokens = contracts.GetTestTokenBalance(gethNode, addr); - if (testTokens.Amount < 64m) + if (ShouldMintTestTokens(gethNode, contracts, addr)) { contracts.MintTestTokens(gethNode, addr, defaultTestTokensToMint); - return $"Minted {defaultTestTokensToMint}." + nl; + report.Add($"Minted {defaultTestTokensToMint}."); + return defaultTestTokensToMint; } - return "TestToken balance over threshold." + nl; + + report.Add("TestToken balance over threshold."); + return 0.TestTokens(); } - private string ProcessEth(IGethNode gethNode, EthAddress addr) + private Ether ProcessEth(IGethNode gethNode, EthAddress addr, List report) { - var eth = gethNode.GetEthBalance(addr); - if (eth.Eth < 1.0m) + if (ShouldSendEth(gethNode, addr)) { gethNode.SendEth(addr, defaultEthToSend); - return $"Sent {defaultEthToSend}." + nl; + report.Add($"Sent {defaultEthToSend}."); + return defaultEthToSend; } + report.Add("Eth balance is over threshold."); + return 0.Eth(); + } - return "Eth balance over threshold." + nl; + private bool ShouldMintTestTokens(IGethNode gethNode, ICodexContracts contracts, EthAddress addr) + { + var testTokens = contracts.GetTestTokenBalance(gethNode, addr); + return testTokens.Amount < 64m; + } + + private bool ShouldSendEth(IGethNode gethNode, EthAddress addr) + { + var eth = gethNode.GetEthBalance(addr); + return eth.Eth < 1.0m; } } } diff --git a/Tools/BiblioTech/Commands/ReportHistoryCommand.cs b/Tools/BiblioTech/Commands/ReportHistoryCommand.cs index 61f5da8..ccad6f7 100644 --- a/Tools/BiblioTech/Commands/ReportHistoryCommand.cs +++ b/Tools/BiblioTech/Commands/ReportHistoryCommand.cs @@ -11,6 +11,7 @@ namespace BiblioTech.Commands public override string Name => "report"; public override string StartingMessage => "Getting that data..."; public override string Description => "Admin only. Reports bot-interaction history for a user."; + public override CommandOption[] Options => new[] { user }; protected override async Task Invoke(SocketSlashCommand command) { diff --git a/Tools/BiblioTech/Commands/UserAssociateCommand.cs b/Tools/BiblioTech/Commands/UserAssociateCommand.cs index 58fc32a..8ab25f2 100644 --- a/Tools/BiblioTech/Commands/UserAssociateCommand.cs +++ b/Tools/BiblioTech/Commands/UserAssociateCommand.cs @@ -11,10 +11,8 @@ namespace BiblioTech.Commands public override string Name => "set"; public override string StartingMessage => "hold on..."; - public override string Description => "Associates a Discord user with an Ethereum address in the TestNet. " + - "Warning: You can set your Ethereum address only once! Double-check before hitting enter."; - - public override CommandOption[] Options => new[] { ethOption }; + public override string Description => "Associates a Discord user with an Ethereum address."; + public override CommandOption[] Options => new CommandOption[] { ethOption, optionalUser }; protected override async Task Invoke(SocketSlashCommand command) { diff --git a/Tools/BiblioTech/Configuration.cs b/Tools/BiblioTech/Configuration.cs index 00b7979..decf006 100644 --- a/Tools/BiblioTech/Configuration.cs +++ b/Tools/BiblioTech/Configuration.cs @@ -15,5 +15,8 @@ namespace BiblioTech [Uniform("userdata", "u", "USERDATA", false, "Path where user data files will be saved.")] public string UserDataPath { get; set; } = "userdata"; + + [Uniform("admin-role", "a", "ADMINROLE", true, "Name of the Discord server admin role")] + public string AdminRoleName { get; set; } = string.Empty; } } diff --git a/Tools/BiblioTech/Program.cs b/Tools/BiblioTech/Program.cs index 725b304..3a8d997 100644 --- a/Tools/BiblioTech/Program.cs +++ b/Tools/BiblioTech/Program.cs @@ -21,6 +21,11 @@ namespace BiblioTech var uniformArgs = new ArgsUniform(PrintHelp, args); Config = uniformArgs.Parse(); + if (!Directory.Exists(Config.UserDataPath)) + { + Directory.CreateDirectory(Config.UserDataPath); + } + return new Program().MainAsync(); } @@ -48,7 +53,7 @@ namespace BiblioTech var handler = new CommandHandler(client, new ClearUserAssociationCommand(), new GetBalanceCommand(monitor, ci, associateCommand), - new MintCommand(monitor, ci), + new MintCommand(monitor, ci, associateCommand), new ReportHistoryCommand(), associateCommand, new DeploymentsCommand(monitor)