This commit is contained in:
benbierens 2024-01-20 13:07:56 +01:00
parent f7fcef56c7
commit c6a7489f11
No known key found for this signature in database
GPG Key ID: FE44815D96D0A1AA
4 changed files with 22 additions and 12 deletions

View File

@ -26,7 +26,7 @@ namespace BiblioTech
Program.AdminChecker.SetGuild(guild); Program.AdminChecker.SetGuild(guild);
Program.Log.Log($"Initializing for guild: '{guild.Name}'"); Program.Log.Log($"Initializing for guild: '{guild.Name}'");
var roleController = new RoleController(guild); var roleController = new RoleController(client);
var rewardsApi = new RewardsApi(roleController); var rewardsApi = new RewardsApi(roleController);
var adminChannels = guild.TextChannels.Where(Program.AdminChecker.IsAdminChannel).ToArray(); var adminChannels = guild.TextChannels.Where(Program.AdminChecker.IsAdminChannel).ToArray();

View File

@ -1,6 +1,5 @@
using ArgsUniform; using ArgsUniform;
using BiblioTech.Commands; using BiblioTech.Commands;
using BiblioTech.Rewards;
using Discord; using Discord;
using Discord.WebSocket; using Discord.WebSocket;
using Logging; using Logging;

View File

@ -74,6 +74,7 @@ namespace BiblioTech.Rewards
private void ProcessRewards(GiveRewards rewards) private void ProcessRewards(GiveRewards rewards)
{ {
Program.Log.Log("Processing: " + JsonConvert.SerializeObject(rewards));
foreach (var reward in rewards.Rewards) ProcessReward(reward); foreach (var reward in rewards.Rewards) ProcessReward(reward);
} }
@ -85,7 +86,7 @@ namespace BiblioTech.Rewards
private void GiveRoleToUser(ulong rewardId, string userAddress) private void GiveRoleToUser(ulong rewardId, string userAddress)
{ {
var userData = Program.UserRepo.GetUserDataForAddress(new GethPlugin.EthAddress(userAddress)); var userData = Program.UserRepo.GetUserDataForAddress(new GethPlugin.EthAddress(userAddress));
if (userData == null) return; if (userData == null) { Program.Log.Log("no userdata"); return; }
roleController.GiveRole(rewardId, userData); roleController.GiveRole(rewardId, userData);
} }

View File

@ -6,7 +6,7 @@ namespace BiblioTech.Rewards
public class RoleController : IDiscordRoleController public class RoleController : IDiscordRoleController
{ {
private const string UsernameTag = "<USER>"; private const string UsernameTag = "<USER>";
private readonly SocketGuild guild; private readonly DiscordSocketClient client;
private readonly SocketTextChannel? rewardsChannel; private readonly SocketTextChannel? rewardsChannel;
private readonly RoleReward[] roleRewards = new[] private readonly RoleReward[] roleRewards = new[]
@ -14,27 +14,31 @@ namespace BiblioTech.Rewards
new RoleReward(1187039439558541498, $"Congratulations {UsernameTag}, you got the test-reward!") new RoleReward(1187039439558541498, $"Congratulations {UsernameTag}, you got the test-reward!")
}; };
public RoleController(SocketGuild guild) public RoleController(DiscordSocketClient client)
{ {
this.guild = guild; this.client = client;
if (!string.IsNullOrEmpty(Program.Config.RewardsChannelName)) if (!string.IsNullOrEmpty(Program.Config.RewardsChannelName))
{ {
rewardsChannel = guild.TextChannels.SingleOrDefault(c => c.Name == Program.Config.RewardsChannelName); rewardsChannel = GetGuild().TextChannels.SingleOrDefault(c => c.Name == Program.Config.RewardsChannelName);
} }
} }
public void GiveRole(ulong roleId, UserData userData) public void GiveRole(ulong roleId, UserData userData)
{ {
var reward = roleRewards.SingleOrDefault(r => r.RoleId == roleId); var reward = roleRewards.SingleOrDefault(r => r.RoleId == roleId);
if (reward == null) return; if (reward == null) { Program.Log.Log("no reward"); return; };
var user = guild.Users.SingleOrDefault(u => u.Id == userData.DiscordId); var guild = GetGuild();
if (user == null) return;
var role = guild.Roles.SingleOrDefault(r => r.Id == roleId); var user = guild.GetUser(userData.DiscordId);
if (role == null) return; if (user == null) { Program.Log.Log("no user"); return; };
var role = guild.GetRole(roleId);
if (role == null) { Program.Log.Log("no role"); return; };
Program.Log.Log($"User has roles: {string.Join(",", user.Roles.Select(r => r.Name + "=" + r.Id))}");
if (user.Roles.Any(r => r.Id == role.Id)) { Program.Log.Log("already has"); return; };
GiveRole(user, role); GiveRole(user, role);
SendNotification(reward, userData, user, role); SendNotification(reward, userData, user, role);
@ -44,6 +48,7 @@ namespace BiblioTech.Rewards
{ {
try try
{ {
Program.Log.Log($"Giving role {role.Name}={role.Id} to user {user.DisplayName}");
Time.Wait(user.AddRoleAsync(role)); Time.Wait(user.AddRoleAsync(role));
} }
catch (Exception ex) catch (Exception ex)
@ -66,6 +71,11 @@ namespace BiblioTech.Rewards
Program.Log.Error($"Failed to notify user '{user.DisplayName}' about role '{role.Name}': {ex}"); Program.Log.Error($"Failed to notify user '{user.DisplayName}' about role '{role.Name}': {ex}");
} }
} }
private SocketGuild GetGuild()
{
return client.Guilds.Single(g => g.Name == Program.Config.ServerName);
}
} }
public class RoleReward public class RoleReward