NadekoBot/NadekoBot.Core/Modules/Permissions/BlacklistCommands.cs

109 lines
4.3 KiB
C#
Raw Normal View History

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]
public class BlacklistCommands : NadekoSubmodule
2016-10-03 02:34:37 +00:00
{
private readonly BlacklistService _bs;
private readonly DbService _db;
private readonly IBotCredentials _creds;
2016-10-03 02:34:37 +00:00
private ConcurrentHashSet<ulong> BlacklistedUsers => _bs.BlacklistedUsers;
private ConcurrentHashSet<ulong> BlacklistedGuilds => _bs.BlacklistedGuilds;
private ConcurrentHashSet<ulong> BlacklistedChannels => _bs.BlacklistedChannels;
public BlacklistCommands(BlacklistService bs, DbService db, IBotCredentials creds)
2016-10-03 02:34:37 +00:00
{
_bs = bs;
_db = db;
_creds = creds;
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
{
if(action == AddRemove.Add && _creds.OwnerIds.Contains(id))
return;
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);
}
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
}
}
}
}