* Removed GuildUserExtensions and moved the methods to IGuildUser and implementations * Made changes per fox's suggestion: Change->Modify. New Modify overload. * Oops * Per Volt: reimplemented new endpoints * Fixing broken docstrings * I forgot that docstrings are XML * Implemented atomic add/remove role endpoints * Removed so people aren't irked * Added single-item role add/remove methodstags/1.0-rc
@@ -41,16 +41,16 @@ namespace Discord | |||||
/// What roles should the user have? | /// What roles should the user have? | ||||
/// </summary> | /// </summary> | ||||
/// <remarks> | /// <remarks> | ||||
/// To add a role to a user: <see cref="GuildUserExtensions.AddRolesAsync(IGuildUser, IRole[])"/> | |||||
/// To remove a role from a user: <see cref="GuildUserExtensions.RemoveRolesAsync(IGuildUser, IRole[])"/> | |||||
/// To add a role to a user: <see cref="IGuildUser.AddRolesAsync(IEnumerable<IRole>, RequestOptions)"/> | |||||
/// To remove a role from a user: <see cref="IGuildUser.RemoveRolesAsync(IEnumerable<IRole>, RequestOptions)"/> | |||||
/// </remarks> | /// </remarks> | ||||
public Optional<IEnumerable<IRole>> Roles { get; set; } | public Optional<IEnumerable<IRole>> Roles { get; set; } | ||||
/// <summary> | /// <summary> | ||||
/// What roles should the user have? | /// What roles should the user have? | ||||
/// </summary> | /// </summary> | ||||
/// <remarks> | /// <remarks> | ||||
/// To add a role to a user: <see cref="GuildUserExtensions.AddRolesAsync(IGuildUser, IRole[])"/> | |||||
/// To remove a role from a user: <see cref="GuildUserExtensions.RemoveRolesAsync(IGuildUser, IRole[])"/> | |||||
/// To add a role to a user: <see cref="IGuildUser.AddRolesAsync(IEnumerable<IRole>, RequestOptions)"/> | |||||
/// To remove a role from a user: <see cref="IGuildUser.RemoveRolesAsync(IEnumerable<IRole>, RequestOptions)"/> | |||||
/// </remarks> | /// </remarks> | ||||
public Optional<IEnumerable<ulong>> RoleIds { get; set; } | public Optional<IEnumerable<ulong>> RoleIds { get; set; } | ||||
/// <summary> | /// <summary> | ||||
@@ -28,5 +28,14 @@ namespace Discord | |||||
Task KickAsync(RequestOptions options = null); | Task KickAsync(RequestOptions options = null); | ||||
/// <summary> Modifies this user's properties in this guild. </summary> | /// <summary> Modifies this user's properties in this guild. </summary> | ||||
Task ModifyAsync(Action<GuildUserProperties> func, RequestOptions options = null); | Task ModifyAsync(Action<GuildUserProperties> func, RequestOptions options = null); | ||||
/// <summary> Adds a role to this user in this guild. </summary> | |||||
Task AddRoleAsync(IRole role, RequestOptions options = null); | |||||
/// <summary> Adds roles to this user in this guild. </summary> | |||||
Task AddRolesAsync(IEnumerable<IRole> roles, RequestOptions options = null); | |||||
/// <summary> Removes a role from this user in this guild. </summary> | |||||
Task RemoveRoleAsync(IRole role, RequestOptions options = null); | |||||
/// <summary> Removes roles from this user in this guild. </summary> | |||||
Task RemoveRolesAsync(IEnumerable<IRole> roles, RequestOptions options = null); | |||||
} | } | ||||
} | } |
@@ -1,27 +0,0 @@ | |||||
using System.Collections.Generic; | |||||
using System.Linq; | |||||
using System.Threading.Tasks; | |||||
namespace Discord | |||||
{ | |||||
public static class GuildUserExtensions | |||||
{ | |||||
public static Task AddRolesAsync(this IGuildUser user, params IRole[] roles) | |||||
=> ChangeRolesAsync(user, add: roles); | |||||
public static Task AddRolesAsync(this IGuildUser user, IEnumerable<IRole> roles) | |||||
=> ChangeRolesAsync(user, add: roles); | |||||
public static Task RemoveRolesAsync(this IGuildUser user, params IRole[] roles) | |||||
=> ChangeRolesAsync(user, remove: roles); | |||||
public static Task RemoveRolesAsync(this IGuildUser user, IEnumerable<IRole> roles) | |||||
=> ChangeRolesAsync(user, remove: roles); | |||||
public static async Task ChangeRolesAsync(this IGuildUser user, IEnumerable<IRole> add = null, IEnumerable<IRole> remove = null) | |||||
{ | |||||
IEnumerable<ulong> 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); | |||||
} | |||||
} | |||||
} |
@@ -388,6 +388,26 @@ namespace Discord.API | |||||
break; | 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 | //Channel Messages | ||||
public async Task<Message> GetChannelMessageAsync(ulong channelId, ulong messageId, RequestOptions options = null) | public async Task<Message> GetChannelMessageAsync(ulong channelId, ulong messageId, RequestOptions options = null) | ||||
@@ -84,6 +84,18 @@ namespace Discord.Rest | |||||
} | } | ||||
public Task KickAsync(RequestOptions options = null) | public Task KickAsync(RequestOptions options = null) | ||||
=> UserHelper.KickAsync(this, Discord, options); | => UserHelper.KickAsync(this, Discord, options); | ||||
/// <inheritdoc /> | |||||
public Task AddRoleAsync(IRole role, RequestOptions options = null) | |||||
=> AddRolesAsync(new[] { role }, options); | |||||
/// <inheritdoc /> | |||||
public Task AddRolesAsync(IEnumerable<IRole> roles, RequestOptions options = null) | |||||
=> UserHelper.AddRolesAsync(this, Discord, roles, options); | |||||
/// <inheritdoc /> | |||||
public Task RemoveRoleAsync(IRole role, RequestOptions options = null) | |||||
=> RemoveRolesAsync(new[] { role }, options); | |||||
/// <inheritdoc /> | |||||
public Task RemoveRolesAsync(IEnumerable<IRole> roles, RequestOptions options = null) | |||||
=> UserHelper.RemoveRolesAsync(this, Discord, roles, options); | |||||
public ChannelPermissions GetPermissions(IGuildChannel channel) | public ChannelPermissions GetPermissions(IGuildChannel channel) | ||||
{ | { | ||||
@@ -1,5 +1,6 @@ | |||||
using Discord.API.Rest; | using Discord.API.Rest; | ||||
using System; | using System; | ||||
using System.Collections.Generic; | |||||
using System.Threading.Tasks; | using System.Threading.Tasks; | ||||
using Model = Discord.API.User; | using Model = Discord.API.User; | ||||
using ImageModel = Discord.API.Image; | using ImageModel = Discord.API.Image; | ||||
@@ -63,5 +64,17 @@ namespace Discord.Rest | |||||
var args = new CreateDMChannelParams(user.Id); | var args = new CreateDMChannelParams(user.Id); | ||||
return RestDMChannel.Create(client, await client.ApiClient.CreateDMChannelAsync(args, options).ConfigureAwait(false)); | 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); | |||||
} | |||||
} | } | ||||
} | } |
@@ -109,6 +109,18 @@ namespace Discord.WebSocket | |||||
=> UserHelper.ModifyAsync(this, Discord, func, options); | => UserHelper.ModifyAsync(this, Discord, func, options); | ||||
public Task KickAsync(RequestOptions options = null) | public Task KickAsync(RequestOptions options = null) | ||||
=> UserHelper.KickAsync(this, Discord, options); | => UserHelper.KickAsync(this, Discord, options); | ||||
/// <inheritdoc /> | |||||
public Task AddRoleAsync(IRole role, RequestOptions options = null) | |||||
=> AddRolesAsync(new[] { role }, options); | |||||
/// <inheritdoc /> | |||||
public Task AddRolesAsync(IEnumerable<IRole> roles, RequestOptions options = null) | |||||
=> UserHelper.AddRolesAsync(this, Discord, roles, options); | |||||
/// <inheritdoc /> | |||||
public Task RemoveRoleAsync(IRole role, RequestOptions options = null) | |||||
=> RemoveRolesAsync(new[] { role }, options); | |||||
/// <inheritdoc /> | |||||
public Task RemoveRolesAsync(IEnumerable<IRole> roles, RequestOptions options = null) | |||||
=> UserHelper.RemoveRolesAsync(this, Discord, roles, options); | |||||
public ChannelPermissions GetPermissions(IGuildChannel channel) | public ChannelPermissions GetPermissions(IGuildChannel channel) | ||||
=> new ChannelPermissions(Permissions.ResolveChannel(Guild, this, channel, GuildPermissions.RawValue)); | => new ChannelPermissions(Permissions.ResolveChannel(Guild, this, channel, GuildPermissions.RawValue)); | ||||