2024-10-22 11:55:52 +00:00
|
|
|
|
using BiblioTech.Options;
|
|
|
|
|
using Discord;
|
2023-12-18 10:27:28 +00:00
|
|
|
|
using Discord.WebSocket;
|
2024-10-22 11:55:52 +00:00
|
|
|
|
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)
|
2023-10-25 08:38:21 +00:00
|
|
|
|
{
|
2024-05-16 12:05:58 +00:00
|
|
|
|
return channel.Id == Program.Config.AdminChannelId;
|
2023-10-25 08:38:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-30 08:59:22 +00:00
|
|
|
|
public async Task SendInAdminChannel(string msg)
|
2023-12-18 10:27:28 +00:00
|
|
|
|
{
|
2024-10-22 11:55:52 +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;
|
2024-05-16 12:05:58 +00:00
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|