forgot migrations
This commit is contained in:
104
src/NadekoBot/Modules/Permissions/Commands/BlacklistCommands.cs
Normal file
104
src/NadekoBot/Modules/Permissions/Commands/BlacklistCommands.cs
Normal file
@@ -0,0 +1,104 @@
|
||||
using Discord;
|
||||
using Discord.Commands;
|
||||
using NadekoBot.Attributes;
|
||||
using NadekoBot.Modules.Games.Trivia;
|
||||
using NadekoBot.Services;
|
||||
using NadekoBot.Services.Database;
|
||||
using NadekoBot.Services.Database.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using static NadekoBot.Services.Database.Models.BlacklistItem;
|
||||
|
||||
namespace NadekoBot.Modules.Permissions
|
||||
{
|
||||
public partial class Permissions
|
||||
{
|
||||
public enum AddRemove
|
||||
{
|
||||
Add,
|
||||
Rem
|
||||
}
|
||||
|
||||
[Group]
|
||||
public class BlacklistCommands
|
||||
{
|
||||
public static HashSet<BlacklistItem> BlacklistedItems { get; set; } = new HashSet<BlacklistItem>();
|
||||
|
||||
static BlacklistCommands()
|
||||
{
|
||||
using (var uow = DbHandler.UnitOfWork())
|
||||
{
|
||||
BlacklistedItems = uow.BotConfig.GetOrCreate().Blacklist;
|
||||
}
|
||||
}
|
||||
|
||||
[LocalizedCommand, LocalizedRemarks, LocalizedSummary, LocalizedAlias]
|
||||
[OwnerOnly]
|
||||
public Task UserBlacklist(IUserMessage imsg, AddRemove action, ulong id)
|
||||
=> Blacklist(imsg, action, id, BlacklistType.User);
|
||||
|
||||
[LocalizedCommand, LocalizedRemarks, LocalizedSummary, LocalizedAlias]
|
||||
[OwnerOnly]
|
||||
public Task ChannelBlacklist(IUserMessage imsg, AddRemove action, ulong id)
|
||||
=> Blacklist(imsg, action, id, BlacklistType.Channel);
|
||||
|
||||
[LocalizedCommand, LocalizedRemarks, LocalizedSummary, LocalizedAlias]
|
||||
[OwnerOnly]
|
||||
public Task ServerBlacklist(IUserMessage imsg, AddRemove action, ulong id)
|
||||
=> Blacklist(imsg, action, id, BlacklistType.Server);
|
||||
|
||||
private async Task Blacklist(IUserMessage imsg, AddRemove action, ulong id, BlacklistType type)
|
||||
{
|
||||
var channel = imsg.Channel;
|
||||
|
||||
using (var uow = DbHandler.UnitOfWork())
|
||||
{
|
||||
if (action == AddRemove.Add)
|
||||
{
|
||||
var item = new BlacklistItem { ItemId = id, Type = type };
|
||||
uow.BotConfig.GetOrCreate().Blacklist.Add(item);
|
||||
BlacklistedItems.Add(item);
|
||||
}
|
||||
else
|
||||
{
|
||||
uow.BotConfig.GetOrCreate().Blacklist.RemoveWhere(bi => bi.ItemId == id && bi.Type == type);
|
||||
BlacklistedItems.RemoveWhere(bi => bi.ItemId == id && bi.Type == type);
|
||||
}
|
||||
await uow.CompleteAsync().ConfigureAwait(false);
|
||||
}
|
||||
if (action == AddRemove.Rem)
|
||||
{
|
||||
TriviaGame tg;
|
||||
switch (type)
|
||||
{
|
||||
case BlacklistType.Server:
|
||||
Games.Games.TriviaCommands.RunningTrivias.TryRemove(id, out tg);
|
||||
if (tg != null)
|
||||
{
|
||||
await tg.StopGame().ConfigureAwait(false);
|
||||
}
|
||||
break;
|
||||
case BlacklistType.Channel:
|
||||
var item = Games.Games.TriviaCommands.RunningTrivias.FirstOrDefault(kvp => kvp.Value.channel.Id == id);
|
||||
Games.Games.TriviaCommands.RunningTrivias.TryRemove(item.Key, out tg);
|
||||
if (tg != null)
|
||||
{
|
||||
await tg.StopGame().ConfigureAwait(false);
|
||||
}
|
||||
break;
|
||||
case BlacklistType.User:
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
await channel.SendMessageAsync(":ok:").ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user