Pulls out chain events sender

This commit is contained in:
Ben 2024-06-27 10:14:23 +02:00
parent 0ef55abdf4
commit 1eb30329c6
No known key found for this signature in database
GPG Key ID: 541B9D8C9F1426A1
2 changed files with 43 additions and 26 deletions

View File

@ -0,0 +1,40 @@
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 () =>
{
foreach (var e in eventsOverview)
{
if (!string.IsNullOrEmpty(e))
{
await eventsChannel.SendMessageAsync(e);
await Task.Delay(3000);
}
}
});
}
catch (Exception ex)
{
log.Error("Failed to process chain events: " + ex);
}
}
}
}

View File

@ -11,7 +11,7 @@ namespace BiblioTech.Rewards
private readonly DiscordSocketClient client;
private readonly ILog log;
private readonly SocketTextChannel? rewardsChannel;
private readonly SocketTextChannel? eventsChannel;
private readonly ChainEventsSender eventsSender;
private readonly RewardRepo repo = new RewardRepo();
public RoleDriver(DiscordSocketClient client, ILog log)
@ -19,7 +19,7 @@ namespace BiblioTech.Rewards
this.client = client;
this.log = log;
rewardsChannel = GetChannel(Program.Config.RewardsChannelId);
eventsChannel = GetChannel(Program.Config.ChainEventsChannelId);
eventsSender = new ChainEventsSender(log, GetChannel(Program.Config.ChainEventsChannelId));
}
public async Task GiveRewards(GiveRewardsCommand rewards)
@ -31,7 +31,7 @@ namespace BiblioTech.Rewards
await ProcessRewards(rewards);
}
await ProcessChainEvents(rewards.EventsOverview);
await eventsSender.ProcessChainEvents(rewards.EventsOverview);
}
private async Task ProcessRewards(GiveRewardsCommand rewards)
@ -60,29 +60,6 @@ namespace BiblioTech.Rewards
return GetGuild().TextChannels.SingleOrDefault(c => c.Id == id);
}
private async Task ProcessChainEvents(string[] eventsOverview)
{
if (eventsChannel == null || eventsOverview == null || !eventsOverview.Any()) return;
try
{
await Task.Run(async () =>
{
foreach (var e in eventsOverview)
{
if (!string.IsNullOrEmpty(e))
{
await eventsChannel.SendMessageAsync(e);
await Task.Delay(3000);
}
}
});
}
catch (Exception ex)
{
log.Error("Failed to process chain events: " + ex);
}
}
private async Task<Dictionary<ulong, IGuildUser>> LoadAllUsers(SocketGuild guild)
{
log.Log("Loading all users..");