2024-06-27 08:14:23 +00:00
|
|
|
|
using Discord.WebSocket;
|
|
|
|
|
using Logging;
|
|
|
|
|
|
|
|
|
|
namespace BiblioTech.Rewards
|
|
|
|
|
{
|
|
|
|
|
public class ChainEventsSender
|
|
|
|
|
{
|
|
|
|
|
private readonly ILog log;
|
|
|
|
|
private SocketTextChannel? eventsChannel;
|
|
|
|
|
|
|
|
|
|
public ChainEventsSender(ILog log, SocketTextChannel? eventsChannel)
|
|
|
|
|
{
|
|
|
|
|
this.log = log;
|
|
|
|
|
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);
|
2024-06-27 08:14:23 +00:00
|
|
|
|
await Task.Delay(3000);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
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;
|
|
|
|
|
}
|
2024-06-27 08:14:23 +00:00
|
|
|
|
}
|
|
|
|
|
}
|