cs-codex-dist-tests/Tools/BiblioTech/AdminChecker.cs

53 lines
1.4 KiB
C#
Raw Normal View History

2023-12-18 10:27:28 +00:00
using Discord;
using Discord.WebSocket;
2023-10-22 08:38:46 +00:00
namespace BiblioTech
{
public class AdminChecker
{
private SocketGuild guild = null!;
private ulong[] adminIds = Array.Empty<ulong>();
private DateTime lastUpdate = DateTime.MinValue;
2023-12-18 10:27:28 +00:00
private ISocketMessageChannel adminChannel = null!;
2023-10-22 08:38:46 +00:00
public void SetGuild(SocketGuild guild)
{
this.guild = guild;
}
public bool IsUserAdmin(ulong userId)
{
if (ShouldUpdate()) UpdateAdminIds();
return adminIds.Contains(userId);
}
2023-12-18 10:27:28 +00:00
public bool IsAdminChannel(IChannel channel)
{
return channel.Name == Program.Config.AdminChannelName;
}
2023-12-18 10:27:28 +00:00
public ISocketMessageChannel GetAdminChannel()
{
return adminChannel;
}
public void SetAdminChannel(ISocketMessageChannel adminChannel)
{
this.adminChannel = adminChannel;
}
2023-10-22 08:38:46 +00:00
private bool ShouldUpdate()
{
return !adminIds.Any() || (DateTime.UtcNow - lastUpdate) > TimeSpan.FromMinutes(10);
}
private void UpdateAdminIds()
{
lastUpdate = DateTime.UtcNow;
var adminRole = guild.Roles.Single(r => r.Name == Program.Config.AdminRoleName);
adminIds = adminRole.Members.Select(m => m.Id).ToArray();
}
}
}