Skip to content

Commit

Permalink
Update: Bot reports interactions in admin channel
Browse files Browse the repository at this point in the history
  • Loading branch information
benbierens committed Sep 30, 2024
1 parent f801cb0 commit 200de1d
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 29 deletions.
8 changes: 7 additions & 1 deletion ProjectPlugins/GethPlugin/EthTokenExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@ public override int GetHashCode()

public override string ToString()
{
return $"{Eth} Eth";
var weiOnly = Wei % TokensIntExtensions.WeiPerEth;

var tokens = new List<string>();
if (Eth > 0) tokens.Add($"{Eth} Eth");
if (weiOnly > 0) tokens.Add($"{weiOnly} Wei");

return string.Join(" + ", tokens);
}
}

Expand Down
4 changes: 2 additions & 2 deletions Tools/BiblioTech/AdminChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
29 changes: 21 additions & 8 deletions Tools/BiblioTech/BaseCommand.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Discord.WebSocket;
using BiblioTech.Options;
using Discord;
using k8s.KubeConfigModels;

namespace BiblioTech
{
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -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}>";
}
}
}
1 change: 1 addition & 0 deletions Tools/BiblioTech/Commands/GetBalanceCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
11 changes: 10 additions & 1 deletion Tools/BiblioTech/Commands/MintCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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<T>(Transaction<T>? transaction)
{
if (transaction == null) return "-";
return transaction.ToString();
}

private Transaction<TestToken>? ProcessTokens(ICodexContracts contracts, EthAddress addr, List<string> report)
Expand Down
56 changes: 43 additions & 13 deletions Tools/BiblioTech/Commands/UserAssociateCommand.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
using BiblioTech.Options;
using Discord;
using GethPlugin;
using k8s.KubeConfigModels;
using NBitcoin.Secp256k1;

namespace BiblioTech.Commands
{
Expand All @@ -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})");
}
}
}
6 changes: 6 additions & 0 deletions Tools/BiblioTech/Transaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()!;
}
}
}
16 changes: 12 additions & 4 deletions Tools/BiblioTech/UserRepo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class UserRepo
private readonly object repoLock = new object();
private readonly Dictionary<ulong, UserData> cache = new Dictionary<ulong, UserData>();

public bool AssociateUserWithAddress(IUser user, EthAddress address)
public SetAddressResponse AssociateUserWithAddress(IUser user, EthAddress address)
{
lock (repoLock)
{
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -245,4 +246,11 @@ private void LoadAllUserData()
}
}
}

public enum SetAddressResponse
{
OK,
AddressAlreadyInUse,
CreateUserFailed
}
}

0 comments on commit 200de1d

Please sign in to comment.