wip
This commit is contained in:
parent
f7fcef56c7
commit
c6a7489f11
|
@ -26,7 +26,7 @@ namespace BiblioTech
|
|||
Program.AdminChecker.SetGuild(guild);
|
||||
Program.Log.Log($"Initializing for guild: '{guild.Name}'");
|
||||
|
||||
var roleController = new RoleController(guild);
|
||||
var roleController = new RoleController(client);
|
||||
var rewardsApi = new RewardsApi(roleController);
|
||||
|
||||
var adminChannels = guild.TextChannels.Where(Program.AdminChecker.IsAdminChannel).ToArray();
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
using ArgsUniform;
|
||||
using BiblioTech.Commands;
|
||||
using BiblioTech.Rewards;
|
||||
using Discord;
|
||||
using Discord.WebSocket;
|
||||
using Logging;
|
||||
|
|
|
@ -74,6 +74,7 @@ namespace BiblioTech.Rewards
|
|||
|
||||
private void ProcessRewards(GiveRewards rewards)
|
||||
{
|
||||
Program.Log.Log("Processing: " + JsonConvert.SerializeObject(rewards));
|
||||
foreach (var reward in rewards.Rewards) ProcessReward(reward);
|
||||
}
|
||||
|
||||
|
@ -85,7 +86,7 @@ namespace BiblioTech.Rewards
|
|||
private void GiveRoleToUser(ulong rewardId, string 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);
|
||||
}
|
||||
|
|
|
@ -6,7 +6,7 @@ namespace BiblioTech.Rewards
|
|||
public class RoleController : IDiscordRoleController
|
||||
{
|
||||
private const string UsernameTag = "<USER>";
|
||||
private readonly SocketGuild guild;
|
||||
private readonly DiscordSocketClient client;
|
||||
private readonly SocketTextChannel? rewardsChannel;
|
||||
|
||||
private readonly RoleReward[] roleRewards = new[]
|
||||
|
@ -14,27 +14,31 @@ namespace BiblioTech.Rewards
|
|||
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))
|
||||
{
|
||||
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)
|
||||
{
|
||||
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);
|
||||
if (user == null) return;
|
||||
var guild = GetGuild();
|
||||
|
||||
var role = guild.Roles.SingleOrDefault(r => r.Id == roleId);
|
||||
if (role == null) return;
|
||||
var user = guild.GetUser(userData.DiscordId);
|
||||
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);
|
||||
SendNotification(reward, userData, user, role);
|
||||
|
@ -44,6 +48,7 @@ namespace BiblioTech.Rewards
|
|||
{
|
||||
try
|
||||
{
|
||||
Program.Log.Log($"Giving role {role.Name}={role.Id} to user {user.DisplayName}");
|
||||
Time.Wait(user.AddRoleAsync(role));
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
@ -66,6 +71,11 @@ namespace BiblioTech.Rewards
|
|||
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
|
||||
|
|
Loading…
Reference in New Issue