2016-10-03 04:34:37 +02:00
|
|
|
|
using Discord;
|
|
|
|
|
using Discord.Commands;
|
2017-10-13 06:14:54 +02:00
|
|
|
|
using NadekoBot.Core.Services;
|
|
|
|
|
using NadekoBot.Core.Services.Database.Models;
|
2016-10-03 04:34:37 +02:00
|
|
|
|
using System.Threading.Tasks;
|
2017-07-17 21:42:36 +02:00
|
|
|
|
using NadekoBot.Common.Attributes;
|
|
|
|
|
using NadekoBot.Common.Collections;
|
|
|
|
|
using NadekoBot.Modules.Permissions.Services;
|
2017-07-19 10:38:14 +02:00
|
|
|
|
using NadekoBot.Common.TypeReaders;
|
2016-10-03 04:34:37 +02:00
|
|
|
|
|
|
|
|
|
namespace NadekoBot.Modules.Permissions
|
|
|
|
|
{
|
|
|
|
|
public partial class Permissions
|
|
|
|
|
{
|
|
|
|
|
[Group]
|
2017-02-24 10:53:42 +01:00
|
|
|
|
public class BlacklistCommands : NadekoSubmodule
|
2016-10-03 04:34:37 +02:00
|
|
|
|
{
|
2017-05-28 01:51:22 +02:00
|
|
|
|
private readonly BlacklistService _bs;
|
2017-05-29 06:13:22 +02:00
|
|
|
|
private readonly DbService _db;
|
2017-05-28 01:51:22 +02:00
|
|
|
|
private readonly IBotCredentials _creds;
|
2016-10-03 04:34:37 +02:00
|
|
|
|
|
2017-05-28 01:51:22 +02:00
|
|
|
|
private ConcurrentHashSet<ulong> BlacklistedUsers => _bs.BlacklistedUsers;
|
|
|
|
|
private ConcurrentHashSet<ulong> BlacklistedGuilds => _bs.BlacklistedGuilds;
|
|
|
|
|
private ConcurrentHashSet<ulong> BlacklistedChannels => _bs.BlacklistedChannels;
|
|
|
|
|
|
2017-05-29 06:13:22 +02:00
|
|
|
|
public BlacklistCommands(BlacklistService bs, DbService db, IBotCredentials creds)
|
2016-10-03 04:34:37 +02:00
|
|
|
|
{
|
2017-05-28 01:51:22 +02:00
|
|
|
|
_bs = bs;
|
|
|
|
|
_db = db;
|
|
|
|
|
_creds = creds;
|
2016-10-03 04:34:37 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-05 05:09:44 +02:00
|
|
|
|
[NadekoCommand, Usage, Description, Aliases]
|
2016-10-03 04:34:37 +02:00
|
|
|
|
[OwnerOnly]
|
2016-12-17 05:09:04 +01:00
|
|
|
|
public Task UserBlacklist(AddRemove action, ulong id)
|
|
|
|
|
=> Blacklist(action, id, BlacklistType.User);
|
2016-10-03 04:34:37 +02:00
|
|
|
|
|
2016-10-05 07:01:19 +02:00
|
|
|
|
[NadekoCommand, Usage, Description, Aliases]
|
|
|
|
|
[OwnerOnly]
|
2016-12-17 05:09:04 +01:00
|
|
|
|
public Task UserBlacklist(AddRemove action, IUser usr)
|
|
|
|
|
=> Blacklist(action, usr.Id, BlacklistType.User);
|
2016-10-05 07:01:19 +02:00
|
|
|
|
|
2016-10-05 05:09:44 +02:00
|
|
|
|
[NadekoCommand, Usage, Description, Aliases]
|
2016-10-03 04:34:37 +02:00
|
|
|
|
[OwnerOnly]
|
2016-12-17 05:09:04 +01:00
|
|
|
|
public Task ChannelBlacklist(AddRemove action, ulong id)
|
|
|
|
|
=> Blacklist(action, id, BlacklistType.Channel);
|
2016-10-03 04:34:37 +02:00
|
|
|
|
|
2016-10-05 05:09:44 +02:00
|
|
|
|
[NadekoCommand, Usage, Description, Aliases]
|
2016-10-03 04:34:37 +02:00
|
|
|
|
[OwnerOnly]
|
2016-12-17 05:09:04 +01:00
|
|
|
|
public Task ServerBlacklist(AddRemove action, ulong id)
|
|
|
|
|
=> Blacklist(action, id, BlacklistType.Server);
|
2016-10-03 04:34:37 +02:00
|
|
|
|
|
2016-10-05 07:01:19 +02:00
|
|
|
|
[NadekoCommand, Usage, Description, Aliases]
|
|
|
|
|
[OwnerOnly]
|
2016-12-17 05:09:04 +01:00
|
|
|
|
public Task ServerBlacklist(AddRemove action, IGuild guild)
|
|
|
|
|
=> Blacklist(action, guild.Id, BlacklistType.Server);
|
2016-10-05 07:01:19 +02:00
|
|
|
|
|
2016-12-17 05:09:04 +01:00
|
|
|
|
private async Task Blacklist(AddRemove action, ulong id, BlacklistType type)
|
2016-10-03 04:34:37 +02:00
|
|
|
|
{
|
2017-05-28 01:51:22 +02:00
|
|
|
|
if(action == AddRemove.Add && _creds.OwnerIds.Contains(id))
|
2017-05-17 00:55:59 +02:00
|
|
|
|
return;
|
|
|
|
|
|
2017-05-28 01:51:22 +02:00
|
|
|
|
using (var uow = _db.UnitOfWork)
|
2016-10-03 04:34:37 +02:00
|
|
|
|
{
|
|
|
|
|
if (action == AddRemove.Add)
|
|
|
|
|
{
|
|
|
|
|
var item = new BlacklistItem { ItemId = id, Type = type };
|
|
|
|
|
uow.BotConfig.GetOrCreate().Blacklist.Add(item);
|
2017-01-07 22:31:42 +01: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 04:34:37 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
uow.BotConfig.GetOrCreate().Blacklist.RemoveWhere(bi => bi.ItemId == id && bi.Type == type);
|
2017-01-07 22:31:42 +01: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 04:34:37 +02:00
|
|
|
|
}
|
|
|
|
|
await uow.CompleteAsync().ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-22 12:16:14 +01:00
|
|
|
|
if(action == AddRemove.Add)
|
2017-02-24 10:53:42 +01:00
|
|
|
|
await ReplyConfirmLocalized("blacklisted", Format.Code(type.ToString()), Format.Code(id.ToString())).ConfigureAwait(false);
|
2016-12-22 12:16:14 +01:00
|
|
|
|
else
|
2017-02-24 10:53:42 +01:00
|
|
|
|
await ReplyConfirmLocalized("unblacklisted", Format.Code(type.ToString()), Format.Code(id.ToString())).ConfigureAwait(false);
|
2016-10-03 04:34:37 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|