From 200de1d7f7443d94adac736e311261fe5f406069 Mon Sep 17 00:00:00 2001 From: Ben Date: Mon, 30 Sep 2024 10:59:22 +0200 Subject: [PATCH] Update: Bot reports interactions in admin channel --- .../GethPlugin/EthTokenExtensions.cs | 8 ++- Tools/BiblioTech/AdminChecker.cs | 4 +- Tools/BiblioTech/BaseCommand.cs | 29 +++++++--- .../BiblioTech/Commands/GetBalanceCommand.cs | 1 + Tools/BiblioTech/Commands/MintCommand.cs | 11 +++- .../Commands/UserAssociateCommand.cs | 56 ++++++++++++++----- Tools/BiblioTech/Transaction.cs | 6 ++ Tools/BiblioTech/UserRepo.cs | 16 ++++-- 8 files changed, 102 insertions(+), 29 deletions(-) diff --git a/ProjectPlugins/GethPlugin/EthTokenExtensions.cs b/ProjectPlugins/GethPlugin/EthTokenExtensions.cs index f9a9d8a8..d85533a9 100644 --- a/ProjectPlugins/GethPlugin/EthTokenExtensions.cs +++ b/ProjectPlugins/GethPlugin/EthTokenExtensions.cs @@ -28,7 +28,13 @@ public override int GetHashCode() public override string ToString() { - return $"{Eth} Eth"; + var weiOnly = Wei % TokensIntExtensions.WeiPerEth; + + var tokens = new List(); + if (Eth > 0) tokens.Add($"{Eth} Eth"); + if (weiOnly > 0) tokens.Add($"{weiOnly} Wei"); + + return string.Join(" + ", tokens); } } diff --git a/Tools/BiblioTech/AdminChecker.cs b/Tools/BiblioTech/AdminChecker.cs index def2870b..00e7b6a2 100644 --- a/Tools/BiblioTech/AdminChecker.cs +++ b/Tools/BiblioTech/AdminChecker.cs @@ -27,9 +27,9 @@ public bool IsAdminChannel(IChannel channel) return channel.Id == Program.Config.AdminChannelId; } - public ISocketMessageChannel GetAdminChannel() + public async Task SendInAdminChannel(string msg) { - return adminChannel; + await adminChannel.SendMessageAsync(msg); } public void SetAdminChannel(ISocketMessageChannel adminChannel) diff --git a/Tools/BiblioTech/BaseCommand.cs b/Tools/BiblioTech/BaseCommand.cs index 44695fd9..a5864712 100644 --- a/Tools/BiblioTech/BaseCommand.cs +++ b/Tools/BiblioTech/BaseCommand.cs @@ -1,6 +1,7 @@ using Discord.WebSocket; using BiblioTech.Options; using Discord; +using k8s.KubeConfigModels; namespace BiblioTech { @@ -25,16 +26,13 @@ public async Task SlashCommandHandler(SocketSlashCommand command) catch (Exception ex) { var msg = "Failed with exception: " + ex; - if (IsInAdminChannel(command)) - { - await command.FollowupAsync(msg.Substring(0, Math.Min(1900, msg.Length))); - } - else + Program.Log.Error(msg); + + if (!IsInAdminChannel(command)) { - await command.FollowupAsync("Something failed while trying to do that...", ephemeral: true); - await Program.AdminChecker.GetAdminChannel().SendMessageAsync(msg); + await command.FollowupAsync("Something failed while trying to do that... (error details posted in admin channel)", ephemeral: true); } - Program.Log.Error(msg); + await Program.AdminChecker.SendInAdminChannel(msg); } } @@ -62,5 +60,20 @@ protected IUser GetUserFromCommand(UserOption userOption, CommandContext context if (IsSenderAdmin(context.Command) && targetUser != null) return targetUser; return context.Command.User; } + + protected string Mention(SocketUser user) + { + return Mention(user.Id); + } + + protected string Mention(IUser user) + { + return Mention(user.Id); + } + + protected string Mention(ulong userId) + { + return $"<@{userId}>"; + } } } diff --git a/Tools/BiblioTech/Commands/GetBalanceCommand.cs b/Tools/BiblioTech/Commands/GetBalanceCommand.cs index 9454c79d..8d2460d6 100644 --- a/Tools/BiblioTech/Commands/GetBalanceCommand.cs +++ b/Tools/BiblioTech/Commands/GetBalanceCommand.cs @@ -28,6 +28,7 @@ protected override async Task Execute(CommandContext context, IGethNode gethNode if (addr == null) { await context.Followup($"No address has been set for this user. Please use '/{userAssociateCommand.Name}' to set it first."); + await Program.AdminChecker.SendInAdminChannel($"User {Mention(userId)} used '/{Name}' but address has not been set."); return; } diff --git a/Tools/BiblioTech/Commands/MintCommand.cs b/Tools/BiblioTech/Commands/MintCommand.cs index d3d24519..b5cb9330 100644 --- a/Tools/BiblioTech/Commands/MintCommand.cs +++ b/Tools/BiblioTech/Commands/MintCommand.cs @@ -28,6 +28,7 @@ protected override async Task Execute(CommandContext context, IGethNode gethNode if (addr == null) { await context.Followup($"No address has been set for this user. Please use '/{userAssociateCommand.Name}' to set it first."); + await Program.AdminChecker.SendInAdminChannel($"User {Mention(userId)} used '/{Name}' but address has not been set."); return; } @@ -42,9 +43,17 @@ await Task.Run(() => mintedTokens = ProcessTokens(contracts, addr, report); }); + var reportLine = string.Join(Environment.NewLine, report); Program.UserRepo.AddMintEventForUser(userId, addr, sentEth, mintedTokens); + await Program.AdminChecker.SendInAdminChannel($"User {Mention(userId)} used '/{Name}' successfully. ({reportLine})"); - await context.Followup(string.Join(Environment.NewLine, report)); + await context.Followup(reportLine); + } + + private string Format(Transaction? transaction) + { + if (transaction == null) return "-"; + return transaction.ToString(); } private Transaction? ProcessTokens(ICodexContracts contracts, EthAddress addr, List report) diff --git a/Tools/BiblioTech/Commands/UserAssociateCommand.cs b/Tools/BiblioTech/Commands/UserAssociateCommand.cs index 81e46467..1fc6d63c 100644 --- a/Tools/BiblioTech/Commands/UserAssociateCommand.cs +++ b/Tools/BiblioTech/Commands/UserAssociateCommand.cs @@ -1,4 +1,8 @@ using BiblioTech.Options; +using Discord; +using GethPlugin; +using k8s.KubeConfigModels; +using NBitcoin.Secp256k1; namespace BiblioTech.Commands { @@ -23,30 +27,56 @@ public UserAssociateCommand(NotifyCommand notifyCommand) protected override async Task Invoke(CommandContext context) { var user = GetUserFromCommand(optionalUser, context); - var data = await ethOption.Parse(context); - if (data == null) return; + var newAddress = await ethOption.Parse(context); + if (newAddress == null) return; var currentAddress = Program.UserRepo.GetCurrentAddressForUser(user); if (currentAddress != null && !IsSenderAdmin(context.Command)) { await context.Followup($"You've already set your Ethereum address to {currentAddress}."); + await Program.AdminChecker.SendInAdminChannel($"User {Mention(user)} used '/{Name}' but already has an address set. ({currentAddress})"); return; } - var result = Program.UserRepo.AssociateUserWithAddress(user, data); - if (result) + var result = Program.UserRepo.AssociateUserWithAddress(user, newAddress); + switch (result) { - await context.Followup(new string[] - { + case SetAddressResponse.OK: + await ResponseOK(context, user, newAddress); + break; + case SetAddressResponse.AddressAlreadyInUse: + await ResponseAlreadyUsed(context, user, newAddress); + break; + case SetAddressResponse.CreateUserFailed: + await ResponseCreateUserFailed(context, user); + break; + default: + throw new Exception("Unknown SetAddressResponse mode"); + } + } + + private async Task ResponseCreateUserFailed(CommandContext context, IUser user) + { + await context.Followup("Internal error. Error details sent to admin."); + await Program.AdminChecker.SendInAdminChannel($"User {Mention(user)} used '/{Name}' but failed to create new user."); + } + + private async Task ResponseAlreadyUsed(CommandContext context, IUser user, EthAddress newAddress) + { + await context.Followup("This address is already in use by another user."); + await Program.AdminChecker.SendInAdminChannel($"User {Mention(user)} used '/{Name}' but the provided address is already in use by another user. (address: {newAddress})"); + } + + private async Task ResponseOK(CommandContext context, IUser user, GethPlugin.EthAddress newAddress) + { + await context.Followup(new string[] +{ "Done! Thank you for joining the test net!", - "By default, the bot will @-mention you with test-net reward related notifications.", + "By default, the bot will @-mention you with test-net related notifications.", $"You can enable/disable this behavior with the '/{notifyCommand.Name}' command." - }); - } - else - { - await context.Followup("That didn't work."); - } +}); + + await Program.AdminChecker.SendInAdminChannel($"User {Mention(user)} used '/{Name}' successfully. ({newAddress})"); } } } diff --git a/Tools/BiblioTech/Transaction.cs b/Tools/BiblioTech/Transaction.cs index a19814a7..be2c828e 100644 --- a/Tools/BiblioTech/Transaction.cs +++ b/Tools/BiblioTech/Transaction.cs @@ -10,5 +10,11 @@ public Transaction(T tokenAmount, string transactionHash) public T TokenAmount { get; } public string TransactionHash { get; } + + public override string ToString() + { + if (TokenAmount == null) return "NULL"; + return TokenAmount.ToString()!; + } } } diff --git a/Tools/BiblioTech/UserRepo.cs b/Tools/BiblioTech/UserRepo.cs index 7cd03fd5..35d8ec87 100644 --- a/Tools/BiblioTech/UserRepo.cs +++ b/Tools/BiblioTech/UserRepo.cs @@ -10,7 +10,7 @@ public class UserRepo private readonly object repoLock = new object(); private readonly Dictionary cache = new Dictionary(); - public bool AssociateUserWithAddress(IUser user, EthAddress address) + public SetAddressResponse AssociateUserWithAddress(IUser user, EthAddress address) { lock (repoLock) { @@ -134,18 +134,19 @@ public string[] GetUserReport(EthAddress ethAddress) return null; } - private bool SetUserAddress(IUser user, EthAddress? address) + private SetAddressResponse SetUserAddress(IUser user, EthAddress? address) { if (GetUserDataForAddress(address) != null) { - return false; + return SetAddressResponse.AddressAlreadyInUse; } var userData = GetOrCreate(user); + if (userData == null) return SetAddressResponse.CreateUserFailed; userData.CurrentAddress = address; userData.AssociateEvents.Add(new UserAssociateAddressEvent(DateTime.UtcNow, address)); SaveUserData(userData); - return true; + return SetAddressResponse.OK; } private void SetUserNotification(IUser user, bool notifyEnabled) @@ -245,4 +246,11 @@ private void LoadAllUserData() } } } + + public enum SetAddressResponse + { + OK, + AddressAlreadyInUse, + CreateUserFailed + } }