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