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

168 lines
5.8 KiB
C#
Raw Normal View History

2024-01-22 09:27:07 +00:00
using Discord;
using Discord.WebSocket;
using DiscordRewards;
using Newtonsoft.Json;
2023-12-20 14:56:03 +00:00
namespace BiblioTech.Rewards
{
2024-02-19 13:56:49 +00:00
public class RoleDriver : IDiscordRoleDriver
2023-12-20 14:56:03 +00:00
{
2024-01-20 12:07:56 +00:00
private readonly DiscordSocketClient client;
2023-12-20 14:56:03 +00:00
private readonly SocketTextChannel? rewardsChannel;
private readonly SocketTextChannel? eventsChannel;
private readonly RewardRepo repo = new RewardRepo();
2023-12-20 14:56:03 +00:00
2024-02-19 13:56:49 +00:00
public RoleDriver(DiscordSocketClient client)
2023-12-20 14:56:03 +00:00
{
2024-01-20 12:07:56 +00:00
this.client = client;
2023-12-20 14:56:03 +00:00
rewardsChannel = GetChannel(Program.Config.RewardsChannelName);
eventsChannel = GetChannel(Program.Config.ChainEventsChannelName);
2023-12-20 14:56:03 +00:00
}
2024-01-22 09:27:07 +00:00
public async Task GiveRewards(GiveRewardsCommand rewards)
2023-12-20 14:56:03 +00:00
{
Program.Log.Log($"Processing rewards command: '{JsonConvert.SerializeObject(rewards)}'");
2024-01-20 12:07:56 +00:00
var guild = GetGuild();
2024-01-22 09:27:07 +00:00
// We load all role and user information first,
// so we don't ask the server for the same info multiple times.
var context = new RewardContext(
await LoadAllUsers(guild),
LookUpAllRoles(guild, rewards),
rewardsChannel);
await context.ProcessGiveRewardsCommand(LookUpUsers(rewards));
await ProcessChainEvents(rewards.EventsOverview);
}
private SocketTextChannel? GetChannel(string name)
{
if (string.IsNullOrEmpty(name)) return null;
return GetGuild().TextChannels.SingleOrDefault(c => c.Name == name);
}
private async Task ProcessChainEvents(string[] eventsOverview)
{
if (eventsChannel == null || eventsOverview == null || !eventsOverview.Any()) return;
foreach (var e in eventsOverview)
{
await eventsChannel.SendMessageAsync(e);
}
2024-01-22 09:27:07 +00:00
}
private async Task<Dictionary<ulong, IGuildUser>> LoadAllUsers(SocketGuild guild)
{
Program.Log.Log("Loading all users:");
2024-01-22 09:27:07 +00:00
var result = new Dictionary<ulong, IGuildUser>();
var users = guild.GetUsersAsync();
await foreach (var ulist in users)
{
foreach (var u in ulist)
{
result.Add(u.Id, u);
var roleIds = string.Join(",", u.RoleIds.Select(r => r.ToString()).ToArray());
Program.Log.Log($" > {u.Id}({u.DisplayName}) has [{roleIds}]");
2024-01-22 09:27:07 +00:00
}
}
return result;
}
private Dictionary<ulong, RoleReward> LookUpAllRoles(SocketGuild guild, GiveRewardsCommand rewards)
2024-01-22 09:27:07 +00:00
{
var result = new Dictionary<ulong, RoleReward>();
2024-01-22 09:27:07 +00:00
foreach (var r in rewards.Rewards)
{
if (!result.ContainsKey(r.RewardId))
2024-01-22 09:27:07 +00:00
{
var rewardConfig = repo.Rewards.SingleOrDefault(rr => rr.RoleId == r.RewardId);
if (rewardConfig == null)
{
Program.Log.Log($"No Reward is configured for id '{r.RewardId}'.");
}
else
2024-01-22 09:27:07 +00:00
{
var socketRole = guild.GetRole(r.RewardId);
if (socketRole == null)
{
Program.Log.Log($"Guild Role by id '{r.RewardId}' not found.");
}
else
{
result.Add(r.RewardId, new RoleReward(socketRole, rewardConfig));
2024-01-22 09:27:07 +00:00
}
}
}
}
return result;
}
private UserReward[] LookUpUsers(GiveRewardsCommand rewards)
{
return rewards.Rewards.Select(LookUpUserData).ToArray();
}
private UserReward LookUpUserData(RewardUsersCommand command)
{
return new UserReward(command,
command.UserAddresses
.Select(LookUpUserDataForAddress)
.Where(d => d != null)
.Cast<UserData>()
.ToArray());
}
private UserData? LookUpUserDataForAddress(string address)
{
try
{
var userData = Program.UserRepo.GetUserDataForAddress(new GethPlugin.EthAddress(address));
if (userData != null) Program.Log.Log($"User '{userData.Name}' was looked up.");
else Program.Log.Log($"Lookup for user was unsuccessful. EthAddress: '{address}'");
return userData;
}
catch (Exception ex)
{
Program.Log.Error("Error during UserData lookup: " + ex);
return null;
}
}
2024-01-22 09:27:07 +00:00
private SocketGuild GetGuild()
{
2024-02-19 13:56:49 +00:00
var guild = client.Guilds.SingleOrDefault(g => g.Name == Program.Config.ServerName);
if (guild == null)
{
throw new Exception($"Unable to find guild by name: '{Program.Config.ServerName}'. " +
$"Known guilds: [{string.Join(",", client.Guilds.Select(g => g.Name))}]");
}
return guild;
2024-01-22 09:27:07 +00:00
}
}
2023-12-20 14:56:03 +00:00
public class RoleReward
{
public RoleReward(SocketRole socketRole, RewardConfig reward)
{
SocketRole = socketRole;
Reward = reward;
}
public SocketRole SocketRole { get; }
public RewardConfig Reward { get; }
}
public class UserReward
{
public UserReward(RewardUsersCommand rewardCommand, UserData[] users)
{
RewardCommand = rewardCommand;
Users = users;
}
public RewardUsersCommand RewardCommand { get; }
public UserData[] Users { get; }
}
2023-12-20 14:56:03 +00:00
}