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

93 lines
3.0 KiB
C#
Raw Normal View History

2023-12-20 14:56:03 +00:00
using Discord.WebSocket;
using Utils;
namespace BiblioTech.Rewards
{
public class RoleController : IDiscordRoleController
{
private const string UsernameTag = "<USER>";
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 RoleReward[] roleRewards = new[]
{
new RoleReward(1187039439558541498, $"Congratulations {UsernameTag}, you got the test-reward!")
};
2024-01-20 12:07:56 +00:00
public RoleController(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
if (!string.IsNullOrEmpty(Program.Config.RewardsChannelName))
{
2024-01-20 12:07:56 +00:00
rewardsChannel = GetGuild().TextChannels.SingleOrDefault(c => c.Name == Program.Config.RewardsChannelName);
2023-12-20 14:56:03 +00:00
}
}
public void GiveRole(ulong roleId, UserData userData)
{
var reward = roleRewards.SingleOrDefault(r => r.RoleId == roleId);
2024-01-20 12:07:56 +00:00
if (reward == null) { Program.Log.Log("no reward"); return; };
2023-12-20 14:56:03 +00:00
2024-01-20 12:07:56 +00:00
var guild = GetGuild();
2023-12-20 14:56:03 +00:00
2024-01-20 12:07:56 +00:00
var user = guild.GetUser(userData.DiscordId);
if (user == null) { Program.Log.Log("no user"); return; };
2023-12-20 14:56:03 +00:00
2024-01-20 12:07:56 +00:00
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; };
2023-12-20 14:56:03 +00:00
GiveRole(user, role);
SendNotification(reward, userData, user, role);
}
private void GiveRole(SocketGuildUser user, SocketRole role)
{
try
{
2024-01-20 12:07:56 +00:00
Program.Log.Log($"Giving role {role.Name}={role.Id} to user {user.DisplayName}");
2023-12-20 14:56:03 +00:00
Time.Wait(user.AddRoleAsync(role));
}
catch (Exception ex)
{
Program.Log.Error($"Failed to give role '{role.Name}' to user '{user.DisplayName}': {ex}");
}
}
private void SendNotification(RoleReward reward, UserData userData, SocketGuildUser user, SocketRole role)
{
try
{
if (userData.NotificationsEnabled && rewardsChannel != null)
{
Time.Wait(rewardsChannel.SendMessageAsync(reward.Message.Replace(UsernameTag, user.DisplayName)));
}
}
catch (Exception ex)
{
Program.Log.Error($"Failed to notify user '{user.DisplayName}' about role '{role.Name}': {ex}");
}
}
2024-01-20 12:07:56 +00:00
private SocketGuild GetGuild()
{
return client.Guilds.Single(g => g.Name == Program.Config.ServerName);
}
2023-12-20 14:56:03 +00:00
}
public class RoleReward
{
public RoleReward(ulong roleId, string message)
{
RoleId = roleId;
Message = message;
}
public ulong RoleId { get; }
public string Message { get; }
}
}