119 lines
3.8 KiB
C#
Raw Normal View History

2024-01-22 10:27:07 +01:00
using Discord;
using Discord.WebSocket;
using DiscordRewards;
using k8s.KubeConfigModels;
using Logging;
using Newtonsoft.Json;
2025-01-16 15:13:16 +01:00
using Utils;
2023-12-20 15:56:03 +01:00
namespace BiblioTech.Rewards
{
2024-02-19 14:56:49 +01:00
public class RoleDriver : IDiscordRoleDriver
2023-12-20 15:56:03 +01:00
{
2024-01-20 13:07:56 +01:00
private readonly DiscordSocketClient client;
private readonly UserRepo userRepo;
private readonly ILog log;
2023-12-20 15:56:03 +01:00
private readonly SocketTextChannel? rewardsChannel;
public RoleDriver(DiscordSocketClient client, UserRepo userRepo, ILog log, SocketTextChannel? rewardsChannel)
2023-12-20 15:56:03 +01:00
{
2024-01-20 13:07:56 +01:00
this.client = client;
this.userRepo = userRepo;
this.log = log;
this.rewardsChannel = rewardsChannel;
2023-12-20 15:56:03 +01:00
}
public async Task RunRoleGiver(Func<IRoleGiver, Task> action)
2023-12-20 15:56:03 +01:00
{
var context = OpenRoleModifyContext();
var mapper = new RoleMapper(context);
await action(mapper);
2024-01-22 10:27:07 +01:00
}
public async Task IterateUsersWithRoles(Func<IRoleGiver, IUser, ulong, Task> onUserWithRole, params ulong[] rolesToIterate)
{
await IterateUsersWithRoles(onUserWithRole, g => Task.CompletedTask, rolesToIterate);
}
public async Task IterateUsersWithRoles(Func<IRoleGiver, IUser, ulong, Task> onUserWithRole, Func<IRoleGiver, Task> whenDone, params ulong[] rolesToIterate)
2024-01-22 10:27:07 +01:00
{
var context = OpenRoleModifyContext();
var mapper = new RoleMapper(context);
foreach (var user in context.Users)
2024-01-22 10:27:07 +01:00
{
foreach (var role in rolesToIterate)
2024-01-22 10:27:07 +01:00
{
if (user.RoleIds.Contains(role))
2024-01-22 10:27:07 +01:00
{
await onUserWithRole(mapper, user, role);
2024-01-22 10:27:07 +01:00
}
}
}
await whenDone(mapper);
}
private RoleModifyContext OpenRoleModifyContext()
{
var context = new RoleModifyContext(GetGuild(), userRepo, log, rewardsChannel);
context.Initialize();
return context;
}
2024-01-22 10:27:07 +01:00
private SocketGuild GetGuild()
{
var guild = client.Guilds.SingleOrDefault(g => g.Id == Program.Config.ServerId);
2024-02-19 14:56:49 +01:00
if (guild == null)
{
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 14:56:49 +01:00
}
return guild;
2024-01-22 10:27:07 +01:00
}
}
2023-12-20 15:56:03 +01:00
public class RoleMapper : IRoleGiver
{
private readonly RoleModifyContext context;
public RoleMapper(RoleModifyContext context)
{
this.context = context;
}
public async Task GiveActiveClient(ulong userId)
{
await context.GiveRole(userId, Program.Config.ActiveClientRoleId);
}
public async Task GiveActiveHost(ulong userId)
{
await context.GiveRole(userId, Program.Config.ActiveHostRoleId);
}
public async Task GiveActiveP2pParticipant(ulong userId)
{
await context.GiveRole(userId, Program.Config.ActiveP2pParticipantRoleId);
}
public async Task RemoveActiveP2pParticipant(ulong userId)
{
await context.RemoveRole(userId, Program.Config.ActiveP2pParticipantRoleId);
}
public async Task GiveAltruisticRole(ulong userId)
{
await context.GiveRole(userId, Program.Config.AltruisticRoleId);
}
public async Task RemoveActiveClient(ulong userId)
{
await context.RemoveRole(userId, Program.Config.ActiveClientRoleId);
}
public async Task RemoveActiveHost(ulong userId)
{
await context.RemoveRole(userId, Program.Config.ActiveHostRoleId);
}
}
2023-12-20 15:56:03 +01:00
}