forked from RPCS3/discord-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModProvider.cs
118 lines (103 loc) · 3.84 KB
/
ModProvider.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
using DSharpPlus.Entities;
using Microsoft.EntityFrameworkCore;
namespace CompatBot.Database.Providers;
internal static class ModProvider
{
private static readonly Dictionary<ulong, Moderator> Moderators;
private static readonly BotDb Db = new();
public static ReadOnlyDictionary<ulong, Moderator> Mods => new(Moderators);
static ModProvider()
{
Moderators = Db.Moderator.AsNoTracking().ToDictionary(m => m.DiscordId, m => m);
}
public static bool IsMod(ulong userId) => Moderators.ContainsKey(userId);
public static bool IsSudoer(ulong userId) => Moderators.TryGetValue(userId, out var mod) && mod.Sudoer;
public static async Task<bool> AddAsync(ulong userId)
{
if (IsMod(userId))
return false;
var newMod = new Moderator {DiscordId = userId};
await Db.Moderator.AddAsync(newMod).ConfigureAwait(false);
await Db.SaveChangesAsync().ConfigureAwait(false);
lock (Moderators)
{
if (IsMod(userId))
return false;
Moderators[userId] = newMod;
}
return true;
}
public static async Task<bool> RemoveAsync(ulong userId)
{
if (!Moderators.ContainsKey(userId))
return false;
var mod = await Db.Moderator.FirstOrDefaultAsync(m => m.DiscordId == userId).ConfigureAwait(false);
if (mod is not null)
{
Db.Moderator.Remove(mod);
await Db.SaveChangesAsync().ConfigureAwait(false);
}
lock (Moderators)
{
if (IsMod(userId))
Moderators.Remove(userId);
else
return false;
}
return true;
}
public static async Task<bool> MakeSudoerAsync(ulong userId)
{
if (!Moderators.TryGetValue(userId, out var mod) || mod.Sudoer)
return false;
var dbMod = await Db.Moderator.FirstOrDefaultAsync(m => m.DiscordId == userId).ConfigureAwait(false);
if (dbMod is not null)
{
dbMod.Sudoer = true;
await Db.SaveChangesAsync().ConfigureAwait(false);
}
mod.Sudoer = true;
return true;
}
public static async Task<bool> UnmakeSudoerAsync(ulong userId)
{
if (!Moderators.TryGetValue(userId, out var mod) || !mod.Sudoer)
return false;
var dbMod = await Db.Moderator.FirstOrDefaultAsync(m => m.DiscordId == userId).ConfigureAwait(false);
if (dbMod is not null)
{
dbMod.Sudoer = false;
await Db.SaveChangesAsync().ConfigureAwait(false);
}
mod.Sudoer = false;
await Db.SaveChangesAsync().ConfigureAwait(false);
return true;
}
public static async Task SyncRolesAsync(DiscordGuild guild)
{
Config.Log.Debug("Syncing moderator list to the sudoer role");
var modRoleList = guild.Roles.Where(kvp => kvp.Value.Name.Equals("Moderator")).ToList();
if (modRoleList.Count == 0)
return;
var modRole = modRoleList.First().Value;
var members = await guild.GetAllMembersAsync().ConfigureAwait(false);
var guildMods = members.Where(m => m.Roles.Any(r => r.Id == modRole.Id) && !m.IsBot && !m.IsCurrent).ToList();
foreach (var mod in guildMods)
{
if (!IsMod(mod.Id))
{
Config.Log.Debug($"Making {mod.Username}#{mod.Discriminator} a bot mod");
await AddAsync(mod.Id).ConfigureAwait(false);
}
if (!IsSudoer(mod.Id))
{
Config.Log.Debug($"Making {mod.Username}#{mod.Discriminator} a bot sudoer");
await MakeSudoerAsync(mod.Id).ConfigureAwait(false);
}
}
}
}