Implements known username replacement

This commit is contained in:
Ben 2024-06-27 10:44:37 +02:00
parent 1eb30329c6
commit 01ee514c73
No known key found for this signature in database
GPG Key ID: 541B9D8C9F1426A1
3 changed files with 135 additions and 59 deletions

View File

@ -21,11 +21,14 @@ namespace BiblioTech.Rewards
{
await Task.Run(async () =>
{
var users = Program.UserRepo.GetAllUserData();
foreach (var e in eventsOverview)
{
if (!string.IsNullOrEmpty(e))
{
await eventsChannel.SendMessageAsync(e);
var @event = ApplyReplacements(users, e);
await eventsChannel.SendMessageAsync(@event);
await Task.Delay(3000);
}
}
@ -36,5 +39,26 @@ namespace BiblioTech.Rewards
log.Error("Failed to process chain events: " + ex);
}
}
private string ApplyReplacements(UserData[] users, string msg)
{
var result = ApplyUserAddressReplacements(users, msg);
return result;
}
private string ApplyUserAddressReplacements(UserData[] users, string msg)
{
foreach (var user in users)
{
if (user.CurrentAddress != null &&
!string.IsNullOrEmpty(user.CurrentAddress.Address) &&
!string.IsNullOrEmpty(user.Name))
{
msg = msg.Replace(user.CurrentAddress.Address, user.Name);
}
}
return msg;
}
}
}

View File

@ -0,0 +1,66 @@
using CodexContractsPlugin;
using GethPlugin;
namespace BiblioTech
{
public class UserData
{
public UserData(ulong discordId, string name, DateTime createdUtc, EthAddress? currentAddress, List<UserAssociateAddressEvent> associateEvents, List<UserMintEvent> mintEvents, bool notificationsEnabled)
{
DiscordId = discordId;
Name = name;
CreatedUtc = createdUtc;
CurrentAddress = currentAddress;
AssociateEvents = associateEvents;
MintEvents = mintEvents;
NotificationsEnabled = notificationsEnabled;
}
public ulong DiscordId { get; }
public string Name { get; }
public DateTime CreatedUtc { get; }
public EthAddress? CurrentAddress { get; set; }
public List<UserAssociateAddressEvent> AssociateEvents { get; }
public List<UserMintEvent> MintEvents { get; }
public bool NotificationsEnabled { get; set; }
public string[] CreateOverview()
{
return new[]
{
$"name: '{Name}' - id:{DiscordId}",
$"joined: {CreatedUtc.ToString("o")}",
$"current address: {CurrentAddress}",
$"{AssociateEvents.Count + MintEvents.Count} total bot events."
};
}
}
public class UserAssociateAddressEvent
{
public UserAssociateAddressEvent(DateTime utc, EthAddress? newAddress)
{
Utc = utc;
NewAddress = newAddress;
}
public DateTime Utc { get; }
public EthAddress? NewAddress { get; }
}
public class UserMintEvent
{
public UserMintEvent(DateTime utc, EthAddress usedAddress, Transaction<Ether>? ethReceived, Transaction<TestToken>? testTokensMinted)
{
Utc = utc;
UsedAddress = usedAddress;
EthReceived = ethReceived;
TestTokensMinted = testTokensMinted;
}
public DateTime Utc { get; }
public EthAddress UsedAddress { get; }
public Transaction<Ether>? EthReceived { get; }
public Transaction<TestToken>? TestTokensMinted { get; }
}
}

View File

@ -8,6 +8,7 @@ namespace BiblioTech
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)
{
@ -33,6 +34,12 @@ namespace BiblioTech
}
}
public UserData[] GetAllUserData()
{
if (cache.Count == 0) LoadAllUserData();
return cache.Values.ToArray();
}
public void AddMintEventForUser(IUser user, EthAddress usedAddress, Transaction<Ether>? eth, Transaction<TestToken>? tokens)
{
lock (repoLock)
@ -151,12 +158,19 @@ namespace BiblioTech
private UserData? GetUserData(IUser user)
{
if (cache.ContainsKey(user.Id))
{
return cache[user.Id];
}
var filename = GetFilename(user);
if (!File.Exists(filename))
{
return null;
}
return JsonConvert.DeserializeObject<UserData>(File.ReadAllText(filename))!;
var userData = JsonConvert.DeserializeObject<UserData>(File.ReadAllText(filename))!;
cache.Add(userData.DiscordId, userData);
return userData;
}
private UserData GetOrCreate(IUser user)
@ -181,6 +195,15 @@ namespace BiblioTech
var filename = GetFilename(userData);
if (File.Exists(filename)) File.Delete(filename);
File.WriteAllText(filename, JsonConvert.SerializeObject(userData));
if (cache.ContainsKey(userData.DiscordId))
{
cache[userData.DiscordId] = userData;
}
else
{
cache.Add(userData.DiscordId, userData);
}
}
private static string GetFilename(IUser user)
@ -197,66 +220,29 @@ namespace BiblioTech
{
return Path.Combine(Program.Config.UserDataPath, discordId.ToString() + ".json");
}
}
public class UserData
{
public UserData(ulong discordId, string name, DateTime createdUtc, EthAddress? currentAddress, List<UserAssociateAddressEvent> associateEvents, List<UserMintEvent> mintEvents, bool notificationsEnabled)
private void LoadAllUserData()
{
DiscordId = discordId;
Name = name;
CreatedUtc = createdUtc;
CurrentAddress = currentAddress;
AssociateEvents = associateEvents;
MintEvents = mintEvents;
NotificationsEnabled = notificationsEnabled;
}
public ulong DiscordId { get; }
public string Name { get; }
public DateTime CreatedUtc { get; }
public EthAddress? CurrentAddress { get; set; }
public List<UserAssociateAddressEvent> AssociateEvents { get; }
public List<UserMintEvent> MintEvents { get; }
public bool NotificationsEnabled { get; set; }
public string[] CreateOverview()
{
return new[]
try
{
$"name: '{Name}' - id:{DiscordId}",
$"joined: {CreatedUtc.ToString("o")}",
$"current address: {CurrentAddress}",
$"{AssociateEvents.Count + MintEvents.Count} total bot events."
};
var files = Directory.GetFiles(Program.Config.UserDataPath);
foreach (var file in files)
{
try
{
var userData = JsonConvert.DeserializeObject<UserData>(File.ReadAllText(file))!;
if (userData != null && userData.DiscordId > 0)
{
cache.Add(userData.DiscordId, userData);
}
}
catch { }
}
}
catch (Exception ex)
{
Program.Log.Error("Exception while trying to load all user data: " + ex);
}
}
}
public class UserAssociateAddressEvent
{
public UserAssociateAddressEvent(DateTime utc, EthAddress? newAddress)
{
Utc = utc;
NewAddress = newAddress;
}
public DateTime Utc { get; }
public EthAddress? NewAddress { get; }
}
public class UserMintEvent
{
public UserMintEvent(DateTime utc, EthAddress usedAddress, Transaction<Ether>? ethReceived, Transaction<TestToken>? testTokensMinted)
{
Utc = utc;
UsedAddress = usedAddress;
EthReceived = ethReceived;
TestTokensMinted = testTokensMinted;
}
public DateTime Utc { get; }
public EthAddress UsedAddress { get; }
public Transaction<Ether>? EthReceived { get; }
public Transaction<TestToken>? TestTokensMinted { get; }
}
}