using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Threading.Tasks; using CompatBot.Database; namespace CompatBot.Providers { internal static class ModProvider { private static readonly Dictionary mods; public static ReadOnlyDictionary Mods => new ReadOnlyDictionary(mods); static ModProvider() { mods = BotDb.Instance.Moderator.ToDictionary(m => m.DiscordId, m => m); } public static bool IsMod(ulong userId) => mods.ContainsKey(userId); public static bool IsSudoer(ulong userId) => mods.TryGetValue(userId, out var mod) && mod.Sudoer; public static async Task AddAsync(ulong userId) { if (IsMod(userId)) return false; var db = BotDb.Instance; var result = await db.Moderator.AddAsync(new Moderator {DiscordId = userId}).ConfigureAwait(false); await db.SaveChangesAsync().ConfigureAwait(false); lock (mods) { if (IsMod(userId)) return false; mods[userId] = result.Entity; } return true; } public static async Task RemoveAsync(ulong userId) { if (!mods.TryGetValue(userId, out var mod)) return false; var db = BotDb.Instance; db.Moderator.Remove(mod); await db.SaveChangesAsync().ConfigureAwait(false); lock (mods) { if (IsMod(userId)) mods.Remove(userId); else return false; } return true; } public static async Task MakeSudoerAsync(ulong userId) { if (!mods.TryGetValue(userId, out var mod) || mod.Sudoer) return false; mod.Sudoer = true; await BotDb.Instance.SaveChangesAsync().ConfigureAwait(false); return true; } public static async Task UnmakeSudoerAsync(ulong userId) { if (!mods.TryGetValue(userId, out var mod) || !mod.Sudoer) return false; mod.Sudoer = false; await BotDb.Instance.SaveChangesAsync().ConfigureAwait(false); return true; } } }