forked from RPCS3/discord-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDisabledCommandsProvider.cs
59 lines (51 loc) · 1.58 KB
/
DisabledCommandsProvider.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
using System;
using System.Collections.Generic;
using System.Linq;
namespace CompatBot.Database.Providers;
internal static class DisabledCommandsProvider
{
private static readonly HashSet<string> DisabledCommands = new(StringComparer.InvariantCultureIgnoreCase);
static DisabledCommandsProvider()
{
lock (DisabledCommands)
{
using var db = new BotDb();
foreach (var cmd in db.DisabledCommands.ToList())
DisabledCommands.Add(cmd.Command);
}
}
public static HashSet<string> Get() => DisabledCommands;
public static void Disable(string command)
{
lock (DisabledCommands)
if (DisabledCommands.Add(command))
{
using var db = new BotDb();
db.DisabledCommands.Add(new() {Command = command});
db.SaveChanges();
}
}
public static void Enable(string command)
{
lock (DisabledCommands)
if (DisabledCommands.Remove(command))
{
using var db = new BotDb();
var cmd = db.DisabledCommands.FirstOrDefault(c => c.Command == command);
if (cmd == null)
return;
db.DisabledCommands.Remove(cmd);
db.SaveChanges();
}
}
public static void Clear()
{
lock (DisabledCommands)
{
DisabledCommands.Clear();
using var db = new BotDb();
db.DisabledCommands.RemoveRange(db.DisabledCommands);
db.SaveChanges();
}
}
}