.aar cleanup, will disable itself if not enough permissions or when role gets deleted.
This commit is contained in:
		@@ -31,29 +31,14 @@ namespace NadekoBot.Modules.Administration
 | 
			
		||||
                    if (Context.User.Id != guser.Guild.OwnerId && guser.GetRoles().Max(x => x.Position) <= role.Position)
 | 
			
		||||
                        return;
 | 
			
		||||
 | 
			
		||||
                using (var uow = _db.UnitOfWork)
 | 
			
		||||
                {
 | 
			
		||||
                    var conf = uow.GuildConfigs.For(Context.Guild.Id, set => set);
 | 
			
		||||
                    if (role == null)
 | 
			
		||||
                    {
 | 
			
		||||
                        conf.AutoAssignRoleId = 0;
 | 
			
		||||
                        _service.AutoAssignedRoles.TryRemove(Context.Guild.Id, out _);
 | 
			
		||||
                    }
 | 
			
		||||
                    else
 | 
			
		||||
                    {
 | 
			
		||||
                        conf.AutoAssignRoleId = role.Id;
 | 
			
		||||
                        _service.AutoAssignedRoles.AddOrUpdate(Context.Guild.Id, role.Id, (key, val) => role.Id);
 | 
			
		||||
                    }
 | 
			
		||||
 | 
			
		||||
                    await uow.CompleteAsync().ConfigureAwait(false);
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                if (role == null)
 | 
			
		||||
                {
 | 
			
		||||
                    _service.DisableAar(Context.Guild.Id);
 | 
			
		||||
                    await ReplyConfirmLocalized("aar_disabled").ConfigureAwait(false);
 | 
			
		||||
                    return;
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                _service.EnableAar(Context.Guild.Id, role.Id);
 | 
			
		||||
                await ReplyConfirmLocalized("aar_enabled").ConfigureAwait(false);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 
 | 
			
		||||
@@ -21,7 +21,9 @@ namespace NadekoBot.Modules.Administration.Services
 | 
			
		||||
            _log = LogManager.GetCurrentClassLogger();
 | 
			
		||||
            _bot = bot;
 | 
			
		||||
 | 
			
		||||
            DeleteMessagesOnCommand = new ConcurrentHashSet<ulong>(bot.AllGuildConfigs.Where(g => g.DeleteMessageOnCommand).Select(g => g.GuildId));
 | 
			
		||||
            DeleteMessagesOnCommand = new ConcurrentHashSet<ulong>(bot.AllGuildConfigs
 | 
			
		||||
                .Where(g => g.DeleteMessageOnCommand)
 | 
			
		||||
                .Select(g => g.GuildId));
 | 
			
		||||
            cmdHandler.CommandExecuted += DelMsgOnCmd_Handler;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -5,6 +5,7 @@ using System.Threading.Tasks;
 | 
			
		||||
using Discord.WebSocket;
 | 
			
		||||
using NadekoBot.Core.Services;
 | 
			
		||||
using NLog;
 | 
			
		||||
using Discord;
 | 
			
		||||
 | 
			
		||||
namespace NadekoBot.Modules.Administration.Services
 | 
			
		||||
{
 | 
			
		||||
@@ -12,39 +13,87 @@ namespace NadekoBot.Modules.Administration.Services
 | 
			
		||||
    {
 | 
			
		||||
        private readonly Logger _log;
 | 
			
		||||
        private readonly DiscordSocketClient _client;
 | 
			
		||||
        private readonly DbService _db;
 | 
			
		||||
 | 
			
		||||
        //guildid/roleid
 | 
			
		||||
        public ConcurrentDictionary<ulong, ulong> AutoAssignedRoles { get; }
 | 
			
		||||
        public BlockingCollection<(IGuildUser, ulong)> AutoAssignQueue { get; } = new BlockingCollection<(IGuildUser, ulong)>();
 | 
			
		||||
 | 
			
		||||
        public AutoAssignRoleService(DiscordSocketClient client, NadekoBot bot)
 | 
			
		||||
        public AutoAssignRoleService(DiscordSocketClient client, NadekoBot bot, DbService db)
 | 
			
		||||
        {
 | 
			
		||||
            _log = LogManager.GetCurrentClassLogger();
 | 
			
		||||
            _client = client;
 | 
			
		||||
            _db = db;
 | 
			
		||||
 | 
			
		||||
            AutoAssignedRoles = new ConcurrentDictionary<ulong, ulong>(
 | 
			
		||||
                bot.AllGuildConfigs.Where(x => x.AutoAssignRoleId != 0)
 | 
			
		||||
                bot.AllGuildConfigs
 | 
			
		||||
                    .Where(x => x.AutoAssignRoleId != 0)
 | 
			
		||||
                    .ToDictionary(k => k.GuildId, v => v.AutoAssignRoleId));
 | 
			
		||||
 | 
			
		||||
            _client.UserJoined += (user) =>
 | 
			
		||||
            var _queueRunner = Task.Run(async () =>
 | 
			
		||||
            {
 | 
			
		||||
                var _ = Task.Run(async () =>
 | 
			
		||||
                while (true)
 | 
			
		||||
                {
 | 
			
		||||
                    var (user, roleId) = AutoAssignQueue.Take();
 | 
			
		||||
                    try
 | 
			
		||||
                    {
 | 
			
		||||
                        AutoAssignedRoles.TryGetValue(user.Guild.Id, out ulong roleId);
 | 
			
		||||
 | 
			
		||||
                        if (roleId == 0)
 | 
			
		||||
                            return;
 | 
			
		||||
 | 
			
		||||
                        var role = user.Guild.Roles.FirstOrDefault(r => r.Id == roleId);
 | 
			
		||||
 | 
			
		||||
                        if (role != null)
 | 
			
		||||
                            await user.AddRoleAsync(role).ConfigureAwait(false);
 | 
			
		||||
                        else
 | 
			
		||||
                        {
 | 
			
		||||
                            _log.Info($"Disabled 'Auto assign role' feature on {0} server the role doesn't exist.",
 | 
			
		||||
                               roleId);
 | 
			
		||||
                            DisableAar(user.GuildId);
 | 
			
		||||
                        }
 | 
			
		||||
                    }
 | 
			
		||||
                    catch (Exception ex) { _log.Warn(ex); }
 | 
			
		||||
                });
 | 
			
		||||
                    catch (Discord.Net.HttpException ex) when (ex.HttpCode == System.Net.HttpStatusCode.Forbidden)
 | 
			
		||||
                    {
 | 
			
		||||
                        _log.Info($"Disabled 'Auto assign role' feature on {0} server because I don't have role management permissions.",
 | 
			
		||||
                            roleId);
 | 
			
		||||
                        DisableAar(user.GuildId);
 | 
			
		||||
                    }
 | 
			
		||||
                    catch (Exception ex)
 | 
			
		||||
                    {
 | 
			
		||||
                        _log.Warn(ex);
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
            });
 | 
			
		||||
 | 
			
		||||
            _client.UserJoined += (user) =>
 | 
			
		||||
            {
 | 
			
		||||
                if (AutoAssignedRoles.TryGetValue(user.Guild.Id, out ulong roleId)
 | 
			
		||||
                    && roleId != 0)
 | 
			
		||||
                {
 | 
			
		||||
                    AutoAssignQueue.Add((user, roleId));
 | 
			
		||||
                }
 | 
			
		||||
                return Task.CompletedTask;
 | 
			
		||||
            };
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public void EnableAar(ulong guildId, ulong roleId)
 | 
			
		||||
        {
 | 
			
		||||
            using (var uow = _db.UnitOfWork)
 | 
			
		||||
            {
 | 
			
		||||
                var gc = uow.GuildConfigs.For(guildId, set => set);
 | 
			
		||||
                gc.AutoAssignRoleId = roleId;
 | 
			
		||||
                uow.Complete();
 | 
			
		||||
            }
 | 
			
		||||
            AutoAssignedRoles.AddOrUpdate(guildId, 
 | 
			
		||||
                roleId, 
 | 
			
		||||
                delegate { return roleId; });
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public void DisableAar(ulong guildId)
 | 
			
		||||
        {
 | 
			
		||||
            using (var uow = _db.UnitOfWork)
 | 
			
		||||
            {
 | 
			
		||||
                var gc = uow.GuildConfigs.For(guildId, set => set);
 | 
			
		||||
                gc.AutoAssignRoleId = 0;
 | 
			
		||||
                uow.Complete();
 | 
			
		||||
            }
 | 
			
		||||
            AutoAssignedRoles.TryRemove(guildId, out _);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user