From 810947acce458ff24afc2ee9fc473218ab66b70a Mon Sep 17 00:00:00 2001 From: Emzi0767 Date: Thu, 2 Mar 2017 00:06:01 +0100 Subject: [PATCH] Implemented atomic add/remove role endpoints --- src/Discord.Net.Rest/DiscordRestApiClient.cs | 20 +++++++++++++++++++ .../Entities/Users/RestGuildUser.cs | 4 ++-- .../Entities/Users/UserHelper.cs | 13 ++++++++++++ .../Entities/Users/SocketGuildUser.cs | 4 ++-- 4 files changed, 37 insertions(+), 4 deletions(-) diff --git a/src/Discord.Net.Rest/DiscordRestApiClient.cs b/src/Discord.Net.Rest/DiscordRestApiClient.cs index 065a25b55..44cb01cf7 100644 --- a/src/Discord.Net.Rest/DiscordRestApiClient.cs +++ b/src/Discord.Net.Rest/DiscordRestApiClient.cs @@ -387,6 +387,26 @@ namespace Discord.API break; } } + public async Task AddRoleAsync(ulong guildId, ulong userId, ulong roleId, RequestOptions options = null) + { + Preconditions.NotEqual(guildId, 0, nameof(guildId)); + Preconditions.NotEqual(userId, 0, nameof(userId)); + Preconditions.NotEqual(roleId, 0, nameof(roleId)); + options = RequestOptions.CreateOrClone(options); + + var ids = new BucketIds(guildId: guildId); + await SendAsync("PUT", () => $"guilds/{guildId}/members/{userId}/roles/{roleId}", ids, options: options); + } + public async Task RemoveRoleAsync(ulong guildId, ulong userId, ulong roleId, RequestOptions options = null) + { + Preconditions.NotEqual(guildId, 0, nameof(guildId)); + Preconditions.NotEqual(userId, 0, nameof(userId)); + Preconditions.NotEqual(roleId, 0, nameof(roleId)); + options = RequestOptions.CreateOrClone(options); + + var ids = new BucketIds(guildId: guildId); + await SendAsync("DELETE", () => $"guilds/{guildId}/members/{userId}/roles/{roleId}", ids, options: options); + } //Channel Messages public async Task GetChannelMessageAsync(ulong channelId, ulong messageId, RequestOptions options = null) diff --git a/src/Discord.Net.Rest/Entities/Users/RestGuildUser.cs b/src/Discord.Net.Rest/Entities/Users/RestGuildUser.cs index d8282fe86..06d3002a2 100644 --- a/src/Discord.Net.Rest/Entities/Users/RestGuildUser.cs +++ b/src/Discord.Net.Rest/Entities/Users/RestGuildUser.cs @@ -84,10 +84,10 @@ namespace Discord.Rest } /// public Task AddRolesAsync(IEnumerable roles, RequestOptions options = null) - => UserHelper.ModifyAsync(this, Discord, x => x.RoleIds = new Optional>(_roleIds.Concat(roles.Select(xr => xr.Id))), options); + => UserHelper.AddRolesAsync(this, this.Discord, roles, options); /// public Task RemoveRolesAsync(IEnumerable roles, RequestOptions options = null) - => UserHelper.ModifyAsync(this, Discord, x => x.RoleIds = new Optional>(_roleIds.Except(roles.Select(xr => xr.Id))), options); + => UserHelper.RemoveRolesAsync(this, this.Discord, roles, options); public Task KickAsync(RequestOptions options = null) => UserHelper.KickAsync(this, Discord, options); diff --git a/src/Discord.Net.Rest/Entities/Users/UserHelper.cs b/src/Discord.Net.Rest/Entities/Users/UserHelper.cs index 5189851fd..82e59227d 100644 --- a/src/Discord.Net.Rest/Entities/Users/UserHelper.cs +++ b/src/Discord.Net.Rest/Entities/Users/UserHelper.cs @@ -1,5 +1,6 @@ using Discord.API.Rest; using System; +using System.Collections.Generic; using System.Threading.Tasks; using Model = Discord.API.User; using ImageModel = Discord.API.Image; @@ -63,5 +64,17 @@ namespace Discord.Rest var args = new CreateDMChannelParams(user.Id); return RestDMChannel.Create(client, await client.ApiClient.CreateDMChannelAsync(args, options).ConfigureAwait(false)); } + + public static async Task AddRolesAsync(IGuildUser user, BaseDiscordClient client, IEnumerable roles, RequestOptions options) + { + foreach (var role in roles) + await client.ApiClient.AddRoleAsync(user.Guild.Id, user.Id, role.Id, options); + } + + public static async Task RemoveRolesAsync(IGuildUser user, BaseDiscordClient client, IEnumerable roles, RequestOptions options) + { + foreach (var role in roles) + await client.ApiClient.RemoveRoleAsync(user.Guild.Id, user.Id, role.Id, options); + } } } diff --git a/src/Discord.Net.WebSocket/Entities/Users/SocketGuildUser.cs b/src/Discord.Net.WebSocket/Entities/Users/SocketGuildUser.cs index 66e8ff53b..903c78d6f 100644 --- a/src/Discord.Net.WebSocket/Entities/Users/SocketGuildUser.cs +++ b/src/Discord.Net.WebSocket/Entities/Users/SocketGuildUser.cs @@ -109,10 +109,10 @@ namespace Discord.WebSocket => UserHelper.KickAsync(this, Discord, options); /// public Task AddRolesAsync(IEnumerable roles, RequestOptions options = null) - => UserHelper.ModifyAsync(this, Discord, x => x.RoleIds = new Optional>(_roleIds.Concat(roles.Select(xr => xr.Id))), options); + => UserHelper.AddRolesAsync(this, this.Discord, roles, options); /// public Task RemoveRolesAsync(IEnumerable roles, RequestOptions options = null) - => UserHelper.ModifyAsync(this, Discord, x => x.RoleIds = new Optional>(_roleIds.Except(roles.Select(xr => xr.Id))), options); + => UserHelper.RemoveRolesAsync(this, this.Discord, roles, options); public ChannelPermissions GetPermissions(IGuildChannel channel) => new ChannelPermissions(Permissions.ResolveChannel(Guild, this, channel, GuildPermissions.RawValue));