(ifcbrk) feature: Add ability to add/remove roles by id (#1757)

* Scaffold Add/Remove roles by id. (needs impl & docs)

* Add docs

* Add implementation

* Expose Add/Remove role endpoints

* Formatting

* Fix wrong method call
This commit is contained in:
Zack Broderson
2021-04-29 10:34:52 -04:00
committed by GitHub
parent 365a848f7a
commit 4c9910cf71
8 changed files with 134 additions and 39 deletions

View File

@@ -113,7 +113,15 @@ namespace Discord
/// A task that represents the asynchronous modification operation. /// A task that represents the asynchronous modification operation.
/// </returns> /// </returns>
Task ModifyAsync(Action<GuildUserProperties> func, RequestOptions options = null); Task ModifyAsync(Action<GuildUserProperties> func, RequestOptions options = null);
/// <summary>
/// Adds the specified role to this user in the guild.
/// </summary>
/// <param name="roleId">The role to be added to the user.</param>
/// <param name="options">The options to be used when sending the request.</param>
/// <returns>
/// A task that represents the asynchronous role addition operation.
/// </returns>
Task AddRoleAsync(ulong roleId, RequestOptions options = null);
/// <summary> /// <summary>
/// Adds the specified role to this user in the guild. /// Adds the specified role to this user in the guild.
/// </summary> /// </summary>
@@ -124,6 +132,15 @@ namespace Discord
/// </returns> /// </returns>
Task AddRoleAsync(IRole role, RequestOptions options = null); Task AddRoleAsync(IRole role, RequestOptions options = null);
/// <summary> /// <summary>
/// Adds the specified <paramref name="roleIds"/> to this user in the guild.
/// </summary>
/// <param name="roleIds">The roles to be added to the user.</param>
/// <param name="options">The options to be used when sending the request.</param>
/// <returns>
/// A task that represents the asynchronous role addition operation.
/// </returns>
Task AddRolesAsync(IEnumerable<ulong> roleIds, RequestOptions options = null);
/// <summary>
/// Adds the specified <paramref name="roles"/> to this user in the guild. /// Adds the specified <paramref name="roles"/> to this user in the guild.
/// </summary> /// </summary>
/// <param name="roles">The roles to be added to the user.</param> /// <param name="roles">The roles to be added to the user.</param>
@@ -133,6 +150,15 @@ namespace Discord
/// </returns> /// </returns>
Task AddRolesAsync(IEnumerable<IRole> roles, RequestOptions options = null); Task AddRolesAsync(IEnumerable<IRole> roles, RequestOptions options = null);
/// <summary> /// <summary>
/// Removes the specified <paramref name="roleId"/> from this user in the guild.
/// </summary>
/// <param name="roleId">The role to be removed from the user.</param>
/// <param name="options">The options to be used when sending the request.</param>
/// <returns>
/// A task that represents the asynchronous role removal operation.
/// </returns>
Task RemoveRoleAsync(ulong roleId, RequestOptions options = null);
/// <summary>
/// Removes the specified <paramref name="role"/> from this user in the guild. /// Removes the specified <paramref name="role"/> from this user in the guild.
/// </summary> /// </summary>
/// <param name="role">The role to be removed from the user.</param> /// <param name="role">The role to be removed from the user.</param>
@@ -142,6 +168,15 @@ namespace Discord
/// </returns> /// </returns>
Task RemoveRoleAsync(IRole role, RequestOptions options = null); Task RemoveRoleAsync(IRole role, RequestOptions options = null);
/// <summary> /// <summary>
/// Removes the specified <paramref name="roleIds"/> from this user in the guild.
/// </summary>
/// <param name="roleIds">The roles to be removed from the user.</param>
/// <param name="options">The options to be used when sending the request.</param>
/// <returns>
/// A task that represents the asynchronous role removal operation.
/// </returns>
Task RemoveRolesAsync(IEnumerable<ulong> roleIds, RequestOptions options = null);
/// <summary>
/// Removes the specified <paramref name="roles"/> from this user in the guild. /// Removes the specified <paramref name="roles"/> from this user in the guild.
/// </summary> /// </summary>
/// <param name="roles">The roles to be removed from the user.</param> /// <param name="roles">The roles to be removed from the user.</param>

View File

@@ -201,5 +201,9 @@ namespace Discord.Rest
} }
}; };
} }
public static Task AddRoleAsync(BaseDiscordClient client, ulong guildId, ulong userId, ulong roleId, RequestOptions options = null)
=> client.ApiClient.AddRoleAsync(guildId, userId, roleId, options);
public static Task RemoveRoleAsync(BaseDiscordClient client, ulong guildId, ulong userId, ulong roleId, RequestOptions options = null)
=> client.ApiClient.RemoveRoleAsync(guildId, userId, roleId, options);
} }
} }

View File

@@ -107,6 +107,10 @@ namespace Discord.Rest
=> ClientHelper.GetVoiceRegionAsync(this, id, options); => ClientHelper.GetVoiceRegionAsync(this, id, options);
public Task<RestWebhook> GetWebhookAsync(ulong id, RequestOptions options = null) public Task<RestWebhook> GetWebhookAsync(ulong id, RequestOptions options = null)
=> ClientHelper.GetWebhookAsync(this, id, options); => ClientHelper.GetWebhookAsync(this, id, options);
public Task AddRoleAsync(ulong guildId, ulong userId, ulong roleId)
=> ClientHelper.AddRoleAsync(this, guildId, userId, roleId);
public Task RemoveRoleAsync(ulong guildId, ulong userId, ulong roleId)
=> ClientHelper.RemoveRoleAsync(this, guildId, userId, roleId);
public Task AddReactionAsync(ulong channelId, ulong messageId, IEmote emote, RequestOptions options = null) public Task AddReactionAsync(ulong channelId, ulong messageId, IEmote emote, RequestOptions options = null)
=> MessageHelper.AddReactionAsync(channelId, messageId, emote, this, options); => MessageHelper.AddReactionAsync(channelId, messageId, emote, this, options);

View File

@@ -112,17 +112,29 @@ namespace Discord.Rest
public Task KickAsync(string reason = null, RequestOptions options = null) public Task KickAsync(string reason = null, RequestOptions options = null)
=> UserHelper.KickAsync(this, Discord, reason, options); => UserHelper.KickAsync(this, Discord, reason, options);
/// <inheritdoc /> /// <inheritdoc />
public Task AddRoleAsync(ulong roleId, RequestOptions options = null)
=> AddRolesAsync(new[] { roleId }, options);
/// <inheritdoc />
public Task AddRoleAsync(IRole role, RequestOptions options = null) public Task AddRoleAsync(IRole role, RequestOptions options = null)
=> AddRolesAsync(new[] { role }, options); => AddRoleAsync(role.Id, options);
/// <inheritdoc />
public Task AddRolesAsync(IEnumerable<ulong> roleIds, RequestOptions options = null)
=> UserHelper.AddRolesAsync(this, Discord, roleIds, options);
/// <inheritdoc /> /// <inheritdoc />
public Task AddRolesAsync(IEnumerable<IRole> roles, RequestOptions options = null) public Task AddRolesAsync(IEnumerable<IRole> roles, RequestOptions options = null)
=> UserHelper.AddRolesAsync(this, Discord, roles, options); => AddRolesAsync(roles.Select(x => x.Id), options);
/// <inheritdoc />
public Task RemoveRoleAsync(ulong roleId, RequestOptions options = null)
=> RemoveRolesAsync(new[] { roleId }, options);
/// <inheritdoc /> /// <inheritdoc />
public Task RemoveRoleAsync(IRole role, RequestOptions options = null) public Task RemoveRoleAsync(IRole role, RequestOptions options = null)
=> RemoveRolesAsync(new[] { role }, options); => RemoveRoleAsync(role.Id, options);
/// <inheritdoc />
public Task RemoveRolesAsync(IEnumerable<ulong> roleIds, RequestOptions options = null)
=> UserHelper.RemoveRolesAsync(this, Discord, roleIds, options);
/// <inheritdoc /> /// <inheritdoc />
public Task RemoveRolesAsync(IEnumerable<IRole> roles, RequestOptions options = null) public Task RemoveRolesAsync(IEnumerable<IRole> roles, RequestOptions options = null)
=> UserHelper.RemoveRolesAsync(this, Discord, roles, options); => RemoveRolesAsync(roles.Select(x => x.Id));
/// <inheritdoc /> /// <inheritdoc />
/// <exception cref="InvalidOperationException">Resolving permissions requires the parent guild to be downloaded.</exception> /// <exception cref="InvalidOperationException">Resolving permissions requires the parent guild to be downloaded.</exception>

View File

@@ -65,19 +65,27 @@ namespace Discord.Rest
/// <inheritdoc /> /// <inheritdoc />
Task IGuildUser.ModifyAsync(Action<GuildUserProperties> func, RequestOptions options) => Task IGuildUser.ModifyAsync(Action<GuildUserProperties> func, RequestOptions options) =>
throw new NotSupportedException("Webhook users cannot be modified."); throw new NotSupportedException("Webhook users cannot be modified.");
/// <inheritdoc />
Task IGuildUser.AddRoleAsync(ulong role, RequestOptions options) =>
throw new NotSupportedException("Roles are not supported on webhook users.");
/// <inheritdoc /> /// <inheritdoc />
Task IGuildUser.AddRoleAsync(IRole role, RequestOptions options) => Task IGuildUser.AddRoleAsync(IRole role, RequestOptions options) =>
throw new NotSupportedException("Roles are not supported on webhook users."); throw new NotSupportedException("Roles are not supported on webhook users.");
/// <inheritdoc />
Task IGuildUser.AddRolesAsync(IEnumerable<ulong> roles, RequestOptions options) =>
throw new NotSupportedException("Roles are not supported on webhook users.");
/// <inheritdoc /> /// <inheritdoc />
Task IGuildUser.AddRolesAsync(IEnumerable<IRole> roles, RequestOptions options) => Task IGuildUser.AddRolesAsync(IEnumerable<IRole> roles, RequestOptions options) =>
throw new NotSupportedException("Roles are not supported on webhook users."); throw new NotSupportedException("Roles are not supported on webhook users.");
/// <inheritdoc />
Task IGuildUser.RemoveRoleAsync(ulong role, RequestOptions options) =>
throw new NotSupportedException("Roles are not supported on webhook users.");
/// <inheritdoc /> /// <inheritdoc />
Task IGuildUser.RemoveRoleAsync(IRole role, RequestOptions options) => Task IGuildUser.RemoveRoleAsync(IRole role, RequestOptions options) =>
throw new NotSupportedException("Roles are not supported on webhook users."); throw new NotSupportedException("Roles are not supported on webhook users.");
/// <inheritdoc />
Task IGuildUser.RemoveRolesAsync(IEnumerable<ulong> roles, RequestOptions options) =>
throw new NotSupportedException("Roles are not supported on webhook users.");
/// <inheritdoc /> /// <inheritdoc />
Task IGuildUser.RemoveRolesAsync(IEnumerable<IRole> roles, RequestOptions options) => Task IGuildUser.RemoveRolesAsync(IEnumerable<IRole> roles, RequestOptions options) =>
throw new NotSupportedException("Roles are not supported on webhook users."); throw new NotSupportedException("Roles are not supported on webhook users.");

View File

@@ -73,16 +73,16 @@ namespace Discord.Rest
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) public static async Task AddRolesAsync(IGuildUser user, BaseDiscordClient client, IEnumerable<ulong> roleIds, RequestOptions options)
{ {
foreach (var role in roles) foreach (var roleId in roleIds)
await client.ApiClient.AddRoleAsync(user.Guild.Id, user.Id, role.Id, options).ConfigureAwait(false); await client.ApiClient.AddRoleAsync(user.Guild.Id, user.Id, roleId, options).ConfigureAwait(false);
} }
public static async Task RemoveRolesAsync(IGuildUser user, BaseDiscordClient client, IEnumerable<IRole> roles, RequestOptions options) public static async Task RemoveRolesAsync(IGuildUser user, BaseDiscordClient client, IEnumerable<ulong> roleIds, RequestOptions options)
{ {
foreach (var role in roles) foreach (var roleId in roleIds)
await client.ApiClient.RemoveRoleAsync(user.Guild.Id, user.Id, role.Id, options).ConfigureAwait(false); await client.ApiClient.RemoveRoleAsync(user.Guild.Id, user.Id, roleId, options).ConfigureAwait(false);
} }
} }
} }

View File

@@ -177,17 +177,29 @@ namespace Discord.WebSocket
public Task KickAsync(string reason = null, RequestOptions options = null) public Task KickAsync(string reason = null, RequestOptions options = null)
=> UserHelper.KickAsync(this, Discord, reason, options); => UserHelper.KickAsync(this, Discord, reason, options);
/// <inheritdoc /> /// <inheritdoc />
public Task AddRoleAsync(ulong roleId, RequestOptions options = null)
=> AddRolesAsync(new[] { roleId }, options);
/// <inheritdoc />
public Task AddRoleAsync(IRole role, RequestOptions options = null) public Task AddRoleAsync(IRole role, RequestOptions options = null)
=> AddRolesAsync(new[] { role }, options); => AddRoleAsync(role.Id, options);
/// <inheritdoc />
public Task AddRolesAsync(IEnumerable<ulong> roleIds, RequestOptions options = null)
=> UserHelper.AddRolesAsync(this, Discord, roleIds, options);
/// <inheritdoc /> /// <inheritdoc />
public Task AddRolesAsync(IEnumerable<IRole> roles, RequestOptions options = null) public Task AddRolesAsync(IEnumerable<IRole> roles, RequestOptions options = null)
=> UserHelper.AddRolesAsync(this, Discord, roles, options); => AddRolesAsync(roles.Select(x => x.Id), options);
/// <inheritdoc />
public Task RemoveRoleAsync(ulong roleId, RequestOptions options = null)
=> RemoveRolesAsync(new[] { roleId }, options);
/// <inheritdoc /> /// <inheritdoc />
public Task RemoveRoleAsync(IRole role, RequestOptions options = null) public Task RemoveRoleAsync(IRole role, RequestOptions options = null)
=> RemoveRolesAsync(new[] { role }, options); => RemoveRoleAsync(role.Id, options);
/// <inheritdoc />
public Task RemoveRolesAsync(IEnumerable<ulong> roleIds, RequestOptions options = null)
=> UserHelper.RemoveRolesAsync(this, Discord, roleIds, options);
/// <inheritdoc /> /// <inheritdoc />
public Task RemoveRolesAsync(IEnumerable<IRole> roles, RequestOptions options = null) public Task RemoveRolesAsync(IEnumerable<IRole> roles, RequestOptions options = null)
=> UserHelper.RemoveRolesAsync(this, Discord, roles, options); => RemoveRolesAsync(roles.Select(x => x.Id));
/// <inheritdoc /> /// <inheritdoc />
public ChannelPermissions GetPermissions(IGuildChannel channel) public ChannelPermissions GetPermissions(IGuildChannel channel)

View File

@@ -81,21 +81,41 @@ namespace Discord.WebSocket
Task IGuildUser.ModifyAsync(Action<GuildUserProperties> func, RequestOptions options) => Task IGuildUser.ModifyAsync(Action<GuildUserProperties> func, RequestOptions options) =>
throw new NotSupportedException("Webhook users cannot be modified."); throw new NotSupportedException("Webhook users cannot be modified.");
/// <inheritdoc />
/// <exception cref="NotSupportedException">Roles are not supported on webhook users.</exception>
Task IGuildUser.AddRoleAsync(ulong roleId, RequestOptions options) =>
throw new NotSupportedException("Roles are not supported on webhook users.");
/// <inheritdoc /> /// <inheritdoc />
/// <exception cref="NotSupportedException">Roles are not supported on webhook users.</exception> /// <exception cref="NotSupportedException">Roles are not supported on webhook users.</exception>
Task IGuildUser.AddRoleAsync(IRole role, RequestOptions options) => Task IGuildUser.AddRoleAsync(IRole role, RequestOptions options) =>
throw new NotSupportedException("Roles are not supported on webhook users."); throw new NotSupportedException("Roles are not supported on webhook users.");
/// <inheritdoc />
/// <exception cref="NotSupportedException">Roles are not supported on webhook users.</exception>
Task IGuildUser.AddRolesAsync(IEnumerable<ulong> roleIds, RequestOptions options) =>
throw new NotSupportedException("Roles are not supported on webhook users.");
/// <inheritdoc /> /// <inheritdoc />
/// <exception cref="NotSupportedException">Roles are not supported on webhook users.</exception> /// <exception cref="NotSupportedException">Roles are not supported on webhook users.</exception>
Task IGuildUser.AddRolesAsync(IEnumerable<IRole> roles, RequestOptions options) => Task IGuildUser.AddRolesAsync(IEnumerable<IRole> roles, RequestOptions options) =>
throw new NotSupportedException("Roles are not supported on webhook users."); throw new NotSupportedException("Roles are not supported on webhook users.");
/// <inheritdoc />
/// <exception cref="NotSupportedException">Roles are not supported on webhook users.</exception>
Task IGuildUser.RemoveRoleAsync(ulong roleId, RequestOptions options) =>
throw new NotSupportedException("Roles are not supported on webhook users.");
/// <inheritdoc /> /// <inheritdoc />
/// <exception cref="NotSupportedException">Roles are not supported on webhook users.</exception> /// <exception cref="NotSupportedException">Roles are not supported on webhook users.</exception>
Task IGuildUser.RemoveRoleAsync(IRole role, RequestOptions options) => Task IGuildUser.RemoveRoleAsync(IRole role, RequestOptions options) =>
throw new NotSupportedException("Roles are not supported on webhook users."); throw new NotSupportedException("Roles are not supported on webhook users.");
/// <inheritdoc />
/// <exception cref="NotSupportedException">Roles are not supported on webhook users.</exception>
Task IGuildUser.RemoveRolesAsync(IEnumerable<ulong> roles, RequestOptions options) =>
throw new NotSupportedException("Roles are not supported on webhook users.");
/// <inheritdoc /> /// <inheritdoc />
/// <exception cref="NotSupportedException">Roles are not supported on webhook users.</exception> /// <exception cref="NotSupportedException">Roles are not supported on webhook users.</exception>
Task IGuildUser.RemoveRolesAsync(IEnumerable<IRole> roles, RequestOptions options) => Task IGuildUser.RemoveRolesAsync(IEnumerable<IRole> roles, RequestOptions options) =>