2016-10-03 02:34:37 +00:00
|
|
|
|
using Discord;
|
|
|
|
|
using Discord.Commands;
|
|
|
|
|
using NadekoBot.Services;
|
|
|
|
|
using NadekoBot.Services.Database.Models;
|
|
|
|
|
using System.Threading.Tasks;
|
2017-07-17 19:42:36 +00:00
|
|
|
|
using NadekoBot.Common.Attributes;
|
|
|
|
|
using NadekoBot.Common.Collections;
|
|
|
|
|
using NadekoBot.Modules.Permissions.Services;
|
2017-07-19 08:38:14 +00:00
|
|
|
|
using NadekoBot.Common.TypeReaders;
|
2016-10-03 02:34:37 +00:00
|
|
|
|
|
|
|
|
|
namespace NadekoBot.Modules.Permissions
|
|
|
|
|
{
|
|
|
|
|
public partial class Permissions
|
|
|
|
|
{
|
|
|
|
|
[Group]
|
2017-02-24 09:53:42 +00:00
|
|
|
|
public class BlacklistCommands : NadekoSubmodule
|
2016-10-03 02:34:37 +00:00
|
|
|
|
{
|
2017-05-27 23:51:22 +00:00
|
|
|
|
private readonly BlacklistService _bs;
|
2017-05-29 04:13:22 +00:00
|
|
|
|
private readonly DbService _db;
|
2017-05-27 23:51:22 +00:00
|
|
|
|
private readonly IBotCredentials _creds;
|
2016-10-03 02:34:37 +00:00
|
|
|
|
|
2017-05-27 23:51:22 +00:00
|
|
|
|
private ConcurrentHashSet<ulong> BlacklistedUsers => _bs.BlacklistedUsers;
|
|
|
|
|
private ConcurrentHashSet<ulong> BlacklistedGuilds => _bs.BlacklistedGuilds;
|
|
|
|
|
private ConcurrentHashSet<ulong> BlacklistedChannels => _bs.BlacklistedChannels;
|
|
|
|
|
|
2017-05-29 04:13:22 +00:00
|
|
|
|
public BlacklistCommands(BlacklistService bs, DbService db, IBotCredentials creds)
|
2016-10-03 02:34:37 +00:00
|
|
|
|
{
|
2017-05-27 23:51:22 +00:00
|
|
|
|
_bs = bs;
|
|
|
|
|
_db = db;
|
|
|
|
|
_creds = creds;
|
2016-10-03 02:34:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-05 03:09:44 +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
|
|
|
|
|
2016-10-05 03:09:44 +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
|
|
|
|
|
2016-10-05 03:09:44 +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
|
|
|
|
{
|
2017-05-27 23:51:22 +00:00
|
|
|
|
if(action == AddRemove.Add && _creds.OwnerIds.Contains(id))
|
2017-05-16 22:55:59 +00:00
|
|
|
|
return;
|
|
|
|
|
|
2017-05-27 23:51:22 +00:00
|
|
|
|
using (var uow = _db.UnitOfWork)
|
2016-10-03 02:34:37 +00:00
|
|
|
|
{
|
|
|
|
|
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-12-22 11:16:14 +00:00
|
|
|
|
if(action == AddRemove.Add)
|
2017-02-24 09:53:42 +00:00
|
|
|
|
await ReplyConfirmLocalized("blacklisted", Format.Code(type.ToString()), Format.Code(id.ToString())).ConfigureAwait(false);
|
2016-12-22 11:16:14 +00:00
|
|
|
|
else
|
2017-02-24 09:53:42 +00:00
|
|
|
|
await ReplyConfirmLocalized("unblacklisted", Format.Code(type.ToString()), Format.Code(id.ToString())).ConfigureAwait(false);
|
2016-10-03 02:34:37 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|