NadekoBot/src/NadekoBot/Modules/Permissions/Commands/BlacklistCommands.cs

139 lines
5.8 KiB
C#
Raw Normal View History

2016-10-03 02:34:37 +00:00
using Discord;
using Discord.Commands;
using NadekoBot.Attributes;
2016-12-10 22:58:55 +00:00
using NadekoBot.Extensions;
2016-10-03 02:34:37 +00:00
using NadekoBot.Modules.Games.Trivia;
using NadekoBot.Services;
using NadekoBot.Services.Database.Models;
using System.Collections.Concurrent;
2016-10-03 02:34:37 +00:00
using System.Linq;
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 : NadekoSubmodule
2016-10-03 02:34:37 +00:00
{
public static ConcurrentHashSet<ulong> BlacklistedUsers { get; set; }
public static ConcurrentHashSet<ulong> BlacklistedGuilds { get; set; }
public static ConcurrentHashSet<ulong> BlacklistedChannels { get; set; }
2016-10-03 02:34:37 +00:00
static BlacklistCommands()
{
using (var uow = DbHandler.UnitOfWork())
{
2017-01-07 21:31:42 +00:00
var blacklist = uow.BotConfig.GetOrCreate().Blacklist;
BlacklistedUsers = new ConcurrentHashSet<ulong>(blacklist.Where(bi => bi.Type == BlacklistType.User).Select(c => c.ItemId));
BlacklistedGuilds = new ConcurrentHashSet<ulong>(blacklist.Where(bi => bi.Type == BlacklistType.Server).Select(c => c.ItemId));
BlacklistedChannels = new ConcurrentHashSet<ulong>(blacklist.Where(bi => bi.Type == BlacklistType.Channel).Select(c => c.ItemId));
2016-10-03 02:34:37 +00:00
}
}
[NadekoCommand, Usage, Description, Aliases]
2016-10-03 02:34:37 +00:00
[OwnerOnly]
2016-12-17 04:09:04 +00:00
public Task UserBlacklist(AddRemove action, ulong id)
=> Blacklist(action, id, BlacklistType.User);
2016-10-03 02:34:37 +00:00
2016-10-05 05:01:19 +00:00
[NadekoCommand, Usage, Description, Aliases]
[OwnerOnly]
2016-12-17 04:09:04 +00:00
public Task UserBlacklist(AddRemove action, IUser usr)
=> Blacklist(action, usr.Id, BlacklistType.User);
2016-10-05 05:01:19 +00:00
[NadekoCommand, Usage, Description, Aliases]
2016-10-03 02:34:37 +00:00
[OwnerOnly]
2016-12-17 04:09:04 +00:00
public Task ChannelBlacklist(AddRemove action, ulong id)
=> Blacklist(action, id, BlacklistType.Channel);
2016-10-03 02:34:37 +00:00
[NadekoCommand, Usage, Description, Aliases]
2016-10-03 02:34:37 +00:00
[OwnerOnly]
2016-12-17 04:09:04 +00:00
public Task ServerBlacklist(AddRemove action, ulong id)
=> Blacklist(action, id, BlacklistType.Server);
2016-10-03 02:34:37 +00:00
2016-10-05 05:01:19 +00:00
[NadekoCommand, Usage, Description, Aliases]
[OwnerOnly]
2016-12-17 04:09:04 +00:00
public Task ServerBlacklist(AddRemove action, IGuild guild)
=> Blacklist(action, guild.Id, BlacklistType.Server);
2016-10-05 05:01:19 +00:00
2016-12-17 04:09:04 +00:00
private async Task Blacklist(AddRemove action, ulong id, BlacklistType type)
2016-10-03 02:34:37 +00:00
{
using (var uow = DbHandler.UnitOfWork())
{
if (action == AddRemove.Add)
{
var item = new BlacklistItem { ItemId = id, Type = type };
uow.BotConfig.GetOrCreate().Blacklist.Add(item);
2017-01-07 21:31:42 +00:00
if (type == BlacklistType.Server)
{
BlacklistedGuilds.Add(id);
}
else if (type == BlacklistType.Channel)
{
BlacklistedChannels.Add(id);
}
else if (type == BlacklistType.User)
{
BlacklistedUsers.Add(id);
}
2016-10-03 02:34:37 +00:00
}
else
{
uow.BotConfig.GetOrCreate().Blacklist.RemoveWhere(bi => bi.ItemId == id && bi.Type == type);
2017-01-07 21:31:42 +00:00
if (type == BlacklistType.Server)
{
BlacklistedGuilds.TryRemove(id);
}
else if (type == BlacklistType.Channel)
{
BlacklistedChannels.TryRemove(id);
}
else if (type == BlacklistType.User)
{
BlacklistedUsers.TryRemove(id);
}
2016-10-03 02:34:37 +00:00
}
await uow.CompleteAsync().ConfigureAwait(false);
}
2016-10-28 10:44:40 +00:00
if (action == AddRemove.Add)
2016-10-03 02:34:37 +00:00
{
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);
2016-10-03 02:34:37 +00:00
Games.Games.TriviaCommands.RunningTrivias.TryRemove(item.Key, out tg);
if (tg != null)
{
await tg.StopGame().ConfigureAwait(false);
}
break;
case BlacklistType.User:
break;
}
}
if(action == AddRemove.Add)
await ReplyConfirmLocalized("blacklisted", Format.Code(type.ToString()), Format.Code(id.ToString())).ConfigureAwait(false);
else
await ReplyConfirmLocalized("unblacklisted", Format.Code(type.ToString()), Format.Code(id.ToString())).ConfigureAwait(false);
2016-10-03 02:34:37 +00:00
}
}
}
}