.aar fixed

This commit is contained in:
Kwoth 2016-12-22 06:32:39 +01:00
parent a4b1df029b
commit 935d43f04f

View File

@ -6,6 +6,7 @@ using NadekoBot.Services;
using NadekoBot.Services.Database.Models; using NadekoBot.Services.Database.Models;
using NLog; using NLog;
using System; using System;
using System.Collections.Concurrent;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -17,9 +18,13 @@ namespace NadekoBot.Modules.Administration
public class AutoAssignRoleCommands public class AutoAssignRoleCommands
{ {
private static Logger _log { get; } private static Logger _log { get; }
//guildid/roleid
private static ConcurrentDictionary<ulong, ulong> AutoAssignedRoles { get; }
static AutoAssignRoleCommands() static AutoAssignRoleCommands()
{ {
AutoAssignedRoles = new ConcurrentDictionary<ulong, ulong>(NadekoBot.AllGuildConfigs.Where(x => x.AutoAssignRoleId != 0)
.ToDictionary(k => k.GuildId, v => v.AutoAssignRoleId));
_log = LogManager.GetCurrentClassLogger(); _log = LogManager.GetCurrentClassLogger();
NadekoBot.Client.UserJoined += (user) => NadekoBot.Client.UserJoined += (user) =>
{ {
@ -27,15 +32,16 @@ namespace NadekoBot.Modules.Administration
{ {
try try
{ {
GuildConfig conf = NadekoBot.AllGuildConfigs.FirstOrDefault(gc => gc.GuildId == user.Guild.Id); ulong roleId = 0;
AutoAssignedRoles.TryGetValue(user.Guild.Id, out roleId);
if (conf.AutoAssignRoleId == 0) if (roleId == 0)
return; return;
var role = user.Guild.Roles.FirstOrDefault(r => r.Id == conf.AutoAssignRoleId); var role = user.Guild.Roles.FirstOrDefault(r => r.Id == roleId);
if (role != null) if (role != null)
await user.AddRolesAsync(role); await user.AddRolesAsync(role).ConfigureAwait(false);
} }
catch (Exception ex) { _log.Warn(ex); } catch (Exception ex) { _log.Warn(ex); }
}); });
@ -55,9 +61,16 @@ namespace NadekoBot.Modules.Administration
{ {
conf = uow.GuildConfigs.For(channel.Guild.Id, set => set); conf = uow.GuildConfigs.For(channel.Guild.Id, set => set);
if (role == null) if (role == null)
{
conf.AutoAssignRoleId = 0; conf.AutoAssignRoleId = 0;
ulong throwaway;
AutoAssignedRoles.TryRemove(channel.Guild.Id, out throwaway);
}
else else
{
conf.AutoAssignRoleId = role.Id; conf.AutoAssignRoleId = role.Id;
AutoAssignedRoles.AddOrUpdate(channel.Guild.Id, role.Id, (key, val) => role.Id);
}
await uow.CompleteAsync().ConfigureAwait(false); await uow.CompleteAsync().ConfigureAwait(false);
} }