2024-01-22 09:27:07 +00:00
|
|
|
|
using Discord;
|
|
|
|
|
using Discord.WebSocket;
|
2024-01-29 16:02:47 +00:00
|
|
|
|
using DiscordRewards;
|
2024-06-26 09:27:19 +00:00
|
|
|
|
using Logging;
|
2024-04-02 11:32:06 +00:00
|
|
|
|
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;
|
2024-06-26 09:27:19 +00:00
|
|
|
|
private readonly ILog log;
|
2023-12-20 14:56:03 +00:00
|
|
|
|
private readonly SocketTextChannel? rewardsChannel;
|
2024-06-27 08:14:23 +00:00
|
|
|
|
private readonly ChainEventsSender eventsSender;
|
2024-01-29 16:02:47 +00:00
|
|
|
|
private readonly RewardRepo repo = new RewardRepo();
|
2023-12-20 14:56:03 +00:00
|
|
|
|
|
2024-06-27 09:16:17 +00:00
|
|
|
|
public RoleDriver(DiscordSocketClient client, ILog log, CustomReplacement replacement)
|
2023-12-20 14:56:03 +00:00
|
|
|
|
{
|
2024-01-20 12:07:56 +00:00
|
|
|
|
this.client = client;
|
2024-06-26 09:27:19 +00:00
|
|
|
|
this.log = log;
|
2024-05-16 12:05:58 +00:00
|
|
|
|
rewardsChannel = GetChannel(Program.Config.RewardsChannelId);
|
2024-06-27 09:16:17 +00:00
|
|
|
|
eventsSender = new ChainEventsSender(log, replacement, GetChannel(Program.Config.ChainEventsChannelId));
|
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
|
|
|
|
{
|
2024-06-26 09:27:19 +00:00
|
|
|
|
log.Log($"Processing rewards command: '{JsonConvert.SerializeObject(rewards)}'");
|
2024-04-02 11:32:06 +00:00
|
|
|
|
|
2024-04-13 06:57:46 +00:00
|
|
|
|
if (rewards.Rewards.Any())
|
|
|
|
|
{
|
|
|
|
|
await ProcessRewards(rewards);
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-27 08:14:23 +00:00
|
|
|
|
await eventsSender.ProcessChainEvents(rewards.EventsOverview);
|
2024-04-13 06:57:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task ProcessRewards(GiveRewardsCommand rewards)
|
|
|
|
|
{
|
2024-06-26 09:27:19 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var guild = GetGuild();
|
|
|
|
|
// 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));
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
log.Error("Failed to process rewards: " + ex);
|
|
|
|
|
}
|
2024-04-08 11:55:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-16 12:05:58 +00:00
|
|
|
|
private SocketTextChannel? GetChannel(ulong id)
|
2024-04-08 11:55:39 +00:00
|
|
|
|
{
|
2024-05-16 12:05:58 +00:00
|
|
|
|
if (id == 0) return null;
|
|
|
|
|
return GetGuild().TextChannels.SingleOrDefault(c => c.Id == id);
|
2024-04-08 11:55:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-01-22 09:27:07 +00:00
|
|
|
|
private async Task<Dictionary<ulong, IGuildUser>> LoadAllUsers(SocketGuild guild)
|
|
|
|
|
{
|
2024-06-26 09:27:19 +00:00
|
|
|
|
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);
|
2024-06-26 09:27:19 +00:00
|
|
|
|
//var roleIds = string.Join(",", u.RoleIds.Select(r => r.ToString()).ToArray());
|
|
|
|
|
//log.Log($" > {u.Id}({u.DisplayName}) has [{roleIds}]");
|
2024-01-22 09:27:07 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-29 16:02:47 +00:00
|
|
|
|
private Dictionary<ulong, RoleReward> LookUpAllRoles(SocketGuild guild, GiveRewardsCommand rewards)
|
2024-01-22 09:27:07 +00:00
|
|
|
|
{
|
2024-01-29 16:02:47 +00:00
|
|
|
|
var result = new Dictionary<ulong, RoleReward>();
|
2024-01-22 09:27:07 +00:00
|
|
|
|
foreach (var r in rewards.Rewards)
|
|
|
|
|
{
|
2024-01-29 16:02:47 +00:00
|
|
|
|
if (!result.ContainsKey(r.RewardId))
|
2024-01-22 09:27:07 +00:00
|
|
|
|
{
|
2024-01-29 16:02:47 +00:00
|
|
|
|
var rewardConfig = repo.Rewards.SingleOrDefault(rr => rr.RoleId == r.RewardId);
|
|
|
|
|
if (rewardConfig == null)
|
|
|
|
|
{
|
2024-06-26 09:27:19 +00:00
|
|
|
|
log.Log($"No Reward is configured for id '{r.RewardId}'.");
|
2024-01-29 16:02:47 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
2024-01-22 09:27:07 +00:00
|
|
|
|
{
|
|
|
|
|
var socketRole = guild.GetRole(r.RewardId);
|
|
|
|
|
if (socketRole == null)
|
|
|
|
|
{
|
2024-06-26 09:27:19 +00:00
|
|
|
|
log.Log($"Guild Role by id '{r.RewardId}' not found.");
|
2024-01-22 09:27:07 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-01-29 16:02:47 +00:00
|
|
|
|
result.Add(r.RewardId, new RoleReward(socketRole, rewardConfig));
|
2024-01-22 09:27:07 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-29 16:02:47 +00:00
|
|
|
|
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
|
|
|
|
|
{
|
2024-04-02 11:32:06 +00:00
|
|
|
|
var userData = Program.UserRepo.GetUserDataForAddress(new GethPlugin.EthAddress(address));
|
2024-06-26 09:27:19 +00:00
|
|
|
|
if (userData != null) log.Log($"User '{userData.Name}' was looked up.");
|
|
|
|
|
else log.Log($"Lookup for user was unsuccessful. EthAddress: '{address}'");
|
2024-04-02 11:32:06 +00:00
|
|
|
|
return userData;
|
2024-01-29 16:02:47 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2024-06-26 09:27:19 +00:00
|
|
|
|
log.Error("Error during UserData lookup: " + ex);
|
2024-01-29 16:02:47 +00:00
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-22 09:27:07 +00:00
|
|
|
|
private SocketGuild GetGuild()
|
|
|
|
|
{
|
2024-05-16 12:05:58 +00:00
|
|
|
|
var guild = client.Guilds.SingleOrDefault(g => g.Id == Program.Config.ServerId);
|
2024-02-19 13:56:49 +00:00
|
|
|
|
if (guild == null)
|
|
|
|
|
{
|
2024-05-16 12:05:58 +00:00
|
|
|
|
throw new Exception($"Unable to find guild by id: '{Program.Config.ServerId}'. " +
|
|
|
|
|
$"Known guilds: [{string.Join(",", client.Guilds.Select(g => g.Name + " (" + g.Id + ")"))}]");
|
2024-02-19 13:56:49 +00:00
|
|
|
|
}
|
|
|
|
|
return guild;
|
2024-01-22 09:27:07 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-12-20 14:56:03 +00:00
|
|
|
|
|
2024-01-29 16:02:47 +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
|
|
|
|
}
|