cs-codex-dist-tests/Tools/BiblioTech/Rewards/ChainEventsSender.cs

73 lines
2.2 KiB
C#
Raw Normal View History

2024-06-27 08:14:23 +00:00
using Discord.WebSocket;
using Logging;
namespace BiblioTech.Rewards
{
public class ChainEventsSender
{
private readonly ILog log;
private readonly CustomReplacement replacement;
private readonly SocketTextChannel? eventsChannel;
2024-06-27 08:14:23 +00:00
public ChainEventsSender(ILog log, CustomReplacement replacement, SocketTextChannel? eventsChannel)
2024-06-27 08:14:23 +00:00
{
this.log = log;
this.replacement = replacement;
2024-06-27 08:14:23 +00:00
this.eventsChannel = eventsChannel;
}
public async Task ProcessChainEvents(string[] eventsOverview)
{
if (eventsChannel == null || eventsOverview == null || !eventsOverview.Any()) return;
try
{
await Task.Run(async () =>
{
2024-06-27 08:44:37 +00:00
var users = Program.UserRepo.GetAllUserData();
2024-06-27 08:14:23 +00:00
foreach (var e in eventsOverview)
{
if (!string.IsNullOrEmpty(e))
{
2024-06-27 08:44:37 +00:00
var @event = ApplyReplacements(users, e);
await eventsChannel.SendMessageAsync(@event);
await Task.Delay(1000);
2024-06-27 08:14:23 +00:00
}
}
});
}
catch (Exception ex)
{
log.Error("Failed to process chain events: " + ex);
}
}
2024-06-27 08:44:37 +00:00
private string ApplyReplacements(UserData[] users, string msg)
{
var result = ApplyUserAddressReplacements(users, msg);
result = ApplyCustomReplacements(result);
2024-06-27 08:44:37 +00:00
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;
}
private string ApplyCustomReplacements(string result)
{
return replacement.Apply(result);
}
2024-06-27 08:14:23 +00:00
}
}