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

67 lines
1.9 KiB
C#
Raw Permalink Normal View History

using BiblioTech.Options;
using Discord;
2023-12-18 10:27:28 +00:00
using Discord.WebSocket;
using Org.BouncyCastle.Utilities;
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.Id == Program.Config.AdminChannelId;
}
public async Task SendInAdminChannel(string msg)
2023-12-18 10:27:28 +00:00
{
await SendInAdminChannel(msg.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries));
}
public async Task SendInAdminChannel(string[] lines)
{
var chunker = new LineChunker(lines);
var chunks = chunker.GetChunks();
if (!chunks.Any()) return;
foreach (var chunk in chunks)
{
await adminChannel.SendMessageAsync(string.Join(Environment.NewLine, chunk));
}
2023-12-18 10:27:28 +00:00
}
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.Id == Program.Config.AdminRoleId);
2023-10-22 08:38:46 +00:00
adminIds = adminRole.Members.Select(m => m.Id).ToArray();
}
}
}