From 3a5e4b23da213ee3e047b968b33bfe78337de6d0 Mon Sep 17 00:00:00 2001 From: RogueException Date: Fri, 18 Nov 2016 08:59:33 -0400 Subject: [PATCH] Added IGuildUser.ChangeRolesAsync --- .../Extensions/GuildUserExtensions.cs | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/Discord.Net.Core/Extensions/GuildUserExtensions.cs b/src/Discord.Net.Core/Extensions/GuildUserExtensions.cs index 57d6a13dc..855551c3d 100644 --- a/src/Discord.Net.Core/Extensions/GuildUserExtensions.cs +++ b/src/Discord.Net.Core/Extensions/GuildUserExtensions.cs @@ -6,14 +6,23 @@ namespace Discord { public static class GuildUserExtensions { + //TODO: Should we remove Add/Remove? Encourages race conditions. public static Task AddRolesAsync(this IGuildUser user, params IRole[] roles) - => AddRolesAsync(user, (IEnumerable)roles); + => ChangeRolesAsync(user, add: roles); public static Task AddRolesAsync(this IGuildUser user, IEnumerable roles) - => user.ModifyAsync(x => x.RoleIds = user.RoleIds.Concat(roles.Select(y => y.Id)).ToArray()); - + => ChangeRolesAsync(user, add: roles); public static Task RemoveRolesAsync(this IGuildUser user, params IRole[] roles) - => RemoveRolesAsync(user, (IEnumerable)roles); + => ChangeRolesAsync(user, remove: roles); public static Task RemoveRolesAsync(this IGuildUser user, IEnumerable roles) - => user.ModifyAsync(x => x.RoleIds = user.RoleIds.Except(roles.Select(y => y.Id)).ToArray()); + => ChangeRolesAsync(user, remove: roles); + public static async Task ChangeRolesAsync(this IGuildUser user, IEnumerable add = null, IEnumerable remove = null) + { + IEnumerable roleIds = user.RoleIds; + if (remove != null) + roleIds = roleIds.Except(remove.Select(x => x.Id)); + if (add != null) + roleIds = roleIds.Concat(add.Select(x => x.Id)); + await user.ModifyAsync(x => x.RoleIds = roleIds.ToArray()).ConfigureAwait(false); + } } }