Files
archived-discord-bot/CompatBot/Database/Providers/DisabledCommandsProvider.cs
13xforever 92751ba6e9 use file-scoped namespaces to reduce nesting
some formatting might be fucked
2022-06-30 00:59:46 +05:00

59 lines
1.6 KiB
C#

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 DisabledCommand {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();
}
}
}