.aar cleanup, will disable itself if not enough permissions or when role gets deleted.
This commit is contained in:
@ -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