Browse Source

Implemented atomic add/remove role endpoints

pull/540/head
Emzi0767 8 years ago
parent
commit
810947acce
4 changed files with 37 additions and 4 deletions
  1. +20
    -0
      src/Discord.Net.Rest/DiscordRestApiClient.cs
  2. +2
    -2
      src/Discord.Net.Rest/Entities/Users/RestGuildUser.cs
  3. +13
    -0
      src/Discord.Net.Rest/Entities/Users/UserHelper.cs
  4. +2
    -2
      src/Discord.Net.WebSocket/Entities/Users/SocketGuildUser.cs

+ 20
- 0
src/Discord.Net.Rest/DiscordRestApiClient.cs View File

@@ -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<Message> GetChannelMessageAsync(ulong channelId, ulong messageId, RequestOptions options = null)


+ 2
- 2
src/Discord.Net.Rest/Entities/Users/RestGuildUser.cs View File

@@ -84,10 +84,10 @@ namespace Discord.Rest
}
/// <inheritdoc />
public Task AddRolesAsync(IEnumerable<IRole> roles, RequestOptions options = null)
=> UserHelper.ModifyAsync(this, Discord, x => x.RoleIds = new Optional<IEnumerable<ulong>>(_roleIds.Concat(roles.Select(xr => xr.Id))), options);
=> UserHelper.AddRolesAsync(this, this.Discord, roles, options);
/// <inheritdoc />
public Task RemoveRolesAsync(IEnumerable<IRole> roles, RequestOptions options = null)
=> UserHelper.ModifyAsync(this, Discord, x => x.RoleIds = new Optional<IEnumerable<ulong>>(_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);



+ 13
- 0
src/Discord.Net.Rest/Entities/Users/UserHelper.cs View File

@@ -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<IRole> 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<IRole> roles, RequestOptions options)
{
foreach (var role in roles)
await client.ApiClient.RemoveRoleAsync(user.Guild.Id, user.Id, role.Id, options);
}
}
}

+ 2
- 2
src/Discord.Net.WebSocket/Entities/Users/SocketGuildUser.cs View File

@@ -109,10 +109,10 @@ namespace Discord.WebSocket
=> UserHelper.KickAsync(this, Discord, options);
/// <inheritdoc />
public Task AddRolesAsync(IEnumerable<IRole> roles, RequestOptions options = null)
=> UserHelper.ModifyAsync(this, Discord, x => x.RoleIds = new Optional<IEnumerable<ulong>>(_roleIds.Concat(roles.Select(xr => xr.Id))), options);
=> UserHelper.AddRolesAsync(this, this.Discord, roles, options);
/// <inheritdoc />
public Task RemoveRolesAsync(IEnumerable<IRole> roles, RequestOptions options = null)
=> UserHelper.ModifyAsync(this, Discord, x => x.RoleIds = new Optional<IEnumerable<ulong>>(_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));


Loading…
Cancel
Save