feature: add Add Guild Member endpoint (#1183)
* Add AddGuildMember Oauth endpoint support * Concat RoleIds if already exists. * Use local ids variable.
This commit is contained in:
committed by
Christopher F
parent
7dd2268982
commit
8ef5f8120f
@@ -535,6 +535,18 @@ namespace Discord
|
|||||||
/// </returns>
|
/// </returns>
|
||||||
Task<IRole> CreateRoleAsync(string name, GuildPermissions? permissions = null, Color? color = null, bool isHoisted = false, RequestOptions options = null);
|
Task<IRole> CreateRoleAsync(string name, GuildPermissions? permissions = null, Color? color = null, bool isHoisted = false, RequestOptions options = null);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Adds a user to this guild.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// This method requires you have an OAuth2 access token for the user, requested with the guilds.join scope, and that the bot have the MANAGE_INVITES permission in the guild.
|
||||||
|
/// </remarks>
|
||||||
|
/// <param name="id">The snowflake identifier of the user.</param>
|
||||||
|
/// <param name="accessToken">The OAuth2 access token for the user, requested with the guilds.join scope.</param>
|
||||||
|
/// <param name="func">The delegate containing the properties to be applied to the user upon being added to the guild.</param>
|
||||||
|
/// <param name="options">The options to be used when sending the request.</param>
|
||||||
|
/// <returns>A guild user associated with the specified <paramref name="id" />; <c>null</c> if the user is already in the guild.</returns>
|
||||||
|
Task<IGuildUser> AddGuildUserAsync(ulong userId, string accessToken, Action<AddGuildUserProperties> func = null, RequestOptions options = null);
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets a collection of all users in this guild.
|
/// Gets a collection of all users in this guild.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -0,0 +1,62 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace Discord
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Properties that are used to add a new <see cref="IGuildUser"/> to the guild with the following parameters.
|
||||||
|
/// </summary>
|
||||||
|
/// <seealso cref="IGuild.AddGuildUserAsync" />
|
||||||
|
public class AddGuildUserProperties
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the user's nickname.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// To clear the user's nickname, this value can be set to <c>null</c> or
|
||||||
|
/// <see cref="string.Empty"/>.
|
||||||
|
/// </remarks>
|
||||||
|
public Optional<string> Nickname { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets whether the user should be muted in a voice channel.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// If this value is set to <c>true</c>, no user will be able to hear this user speak in the guild.
|
||||||
|
/// </remarks>
|
||||||
|
public Optional<bool> Mute { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets whether the user should be deafened in a voice channel.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// If this value is set to <c>true</c>, this user will not be able to hear anyone speak in the guild.
|
||||||
|
/// </remarks>
|
||||||
|
public Optional<bool> Deaf { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the roles the user should have.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// <para>
|
||||||
|
/// To add a role to a user:
|
||||||
|
/// <see cref="IGuildUser.AddRolesAsync(IEnumerable{IRole},RequestOptions)" />
|
||||||
|
/// </para>
|
||||||
|
/// <para>
|
||||||
|
/// To remove a role from a user:
|
||||||
|
/// <see cref="IGuildUser.RemoveRolesAsync(IEnumerable{IRole},RequestOptions)" />
|
||||||
|
/// </para>
|
||||||
|
/// </remarks>
|
||||||
|
public Optional<IEnumerable<IRole>> Roles { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the roles the user should have.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// <para>
|
||||||
|
/// To add a role to a user:
|
||||||
|
/// <see cref="IGuildUser.AddRolesAsync(IEnumerable{IRole},RequestOptions)" />
|
||||||
|
/// </para>
|
||||||
|
/// <para>
|
||||||
|
/// To remove a role from a user:
|
||||||
|
/// <see cref="IGuildUser.RemoveRolesAsync(IEnumerable{IRole},RequestOptions)" />
|
||||||
|
/// </para>
|
||||||
|
/// </remarks>
|
||||||
|
public Optional<IEnumerable<ulong>> RoleIds { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
19
src/Discord.Net.Rest/API/Rest/AddGuildMemberParams.cs
Normal file
19
src/Discord.Net.Rest/API/Rest/AddGuildMemberParams.cs
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
using Newtonsoft.Json;
|
||||||
|
|
||||||
|
namespace Discord.API.Rest
|
||||||
|
{
|
||||||
|
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
|
||||||
|
internal class AddGuildMemberParams
|
||||||
|
{
|
||||||
|
[JsonProperty("access_token")]
|
||||||
|
public string AccessToken { get; set; }
|
||||||
|
[JsonProperty("nick")]
|
||||||
|
public Optional<string> Nickname { get; set; }
|
||||||
|
[JsonProperty("roles")]
|
||||||
|
public Optional<ulong[]> RoleIds { get; set; }
|
||||||
|
[JsonProperty("mute")]
|
||||||
|
public Optional<bool> IsMuted { get; set; }
|
||||||
|
[JsonProperty("deaf")]
|
||||||
|
public Optional<bool> IsDeafened { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -994,6 +994,25 @@ namespace Discord.API
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Guild Members
|
//Guild Members
|
||||||
|
public async Task<GuildMember> AddGuildMemberAsync(ulong guildId, ulong userId, AddGuildMemberParams args, RequestOptions options = null)
|
||||||
|
{
|
||||||
|
Preconditions.NotEqual(guildId, 0, nameof(guildId));
|
||||||
|
Preconditions.NotEqual(userId, 0, nameof(userId));
|
||||||
|
Preconditions.NotNull(args, nameof(args));
|
||||||
|
Preconditions.NotNullOrWhitespace(args.AccessToken, nameof(args.AccessToken));
|
||||||
|
|
||||||
|
if (args.RoleIds.IsSpecified)
|
||||||
|
{
|
||||||
|
foreach (var roleId in args.RoleIds.Value)
|
||||||
|
Preconditions.NotEqual(roleId, 0, nameof(roleId));
|
||||||
|
}
|
||||||
|
|
||||||
|
options = RequestOptions.CreateOrClone(options);
|
||||||
|
|
||||||
|
var ids = new BucketIds(guildId: guildId);
|
||||||
|
|
||||||
|
return await SendJsonAsync<GuildMember>("PUT", () => $"guilds/{guildId}/members/{userId}", args, ids, options: options);
|
||||||
|
}
|
||||||
public async Task<GuildMember> GetGuildMemberAsync(ulong guildId, ulong userId, RequestOptions options = null)
|
public async Task<GuildMember> GetGuildMemberAsync(ulong guildId, ulong userId, RequestOptions options = null)
|
||||||
{
|
{
|
||||||
Preconditions.NotEqual(guildId, 0, nameof(guildId));
|
Preconditions.NotEqual(guildId, 0, nameof(guildId));
|
||||||
|
|||||||
@@ -257,6 +257,34 @@ namespace Discord.Rest
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Users
|
//Users
|
||||||
|
public static async Task<RestGuildUser> AddGuildUserAsync(IGuild guild, BaseDiscordClient client, ulong userId, string accessToken,
|
||||||
|
Action<AddGuildUserProperties> func, RequestOptions options)
|
||||||
|
{
|
||||||
|
var args = new AddGuildUserProperties();
|
||||||
|
func?.Invoke(args);
|
||||||
|
|
||||||
|
if (args.Roles.IsSpecified)
|
||||||
|
{
|
||||||
|
var ids = args.Roles.Value.Select(r => r.Id);
|
||||||
|
|
||||||
|
if (args.RoleIds.IsSpecified)
|
||||||
|
args.RoleIds.Value.Concat(ids);
|
||||||
|
else
|
||||||
|
args.RoleIds = Optional.Create(ids);
|
||||||
|
}
|
||||||
|
var apiArgs = new AddGuildMemberParams
|
||||||
|
{
|
||||||
|
AccessToken = accessToken,
|
||||||
|
Nickname = args.Nickname,
|
||||||
|
IsDeafened = args.Deaf,
|
||||||
|
IsMuted = args.Mute,
|
||||||
|
RoleIds = args.RoleIds.IsSpecified ? args.RoleIds.Value.Distinct().ToArray() : Optional.Create<ulong[]>()
|
||||||
|
};
|
||||||
|
|
||||||
|
var model = await client.ApiClient.AddGuildMemberAsync(guild.Id, userId, apiArgs, options);
|
||||||
|
|
||||||
|
return model is null ? null : RestGuildUser.Create(client, guild, model);
|
||||||
|
}
|
||||||
public static async Task<RestGuildUser> GetUserAsync(IGuild guild, BaseDiscordClient client,
|
public static async Task<RestGuildUser> GetUserAsync(IGuild guild, BaseDiscordClient client,
|
||||||
ulong id, RequestOptions options)
|
ulong id, RequestOptions options)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -537,6 +537,10 @@ namespace Discord.Rest
|
|||||||
public IAsyncEnumerable<IReadOnlyCollection<RestGuildUser>> GetUsersAsync(RequestOptions options = null)
|
public IAsyncEnumerable<IReadOnlyCollection<RestGuildUser>> GetUsersAsync(RequestOptions options = null)
|
||||||
=> GuildHelper.GetUsersAsync(this, Discord, null, null, options);
|
=> GuildHelper.GetUsersAsync(this, Discord, null, null, options);
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public Task<RestGuildUser> AddGuildUserAsync(ulong id, string accessToken, Action<AddGuildUserProperties> func = null, RequestOptions options = null)
|
||||||
|
=> GuildHelper.AddGuildUserAsync(this, Discord, id, accessToken, func, options);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets a user from this guild.
|
/// Gets a user from this guild.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -800,6 +804,10 @@ namespace Discord.Rest
|
|||||||
async Task<IRole> IGuild.CreateRoleAsync(string name, GuildPermissions? permissions, Color? color, bool isHoisted, RequestOptions options)
|
async Task<IRole> IGuild.CreateRoleAsync(string name, GuildPermissions? permissions, Color? color, bool isHoisted, RequestOptions options)
|
||||||
=> await CreateRoleAsync(name, permissions, color, isHoisted, options).ConfigureAwait(false);
|
=> await CreateRoleAsync(name, permissions, color, isHoisted, options).ConfigureAwait(false);
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
async Task<IGuildUser> IGuild.AddGuildUserAsync(ulong userId, string accessToken, Action<AddGuildUserProperties> func, RequestOptions options)
|
||||||
|
=> await AddGuildUserAsync(userId, accessToken, func, options);
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
async Task<IGuildUser> IGuild.GetUserAsync(ulong id, CacheMode mode, RequestOptions options)
|
async Task<IGuildUser> IGuild.GetUserAsync(ulong id, CacheMode mode, RequestOptions options)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -669,6 +669,10 @@ namespace Discord.WebSocket
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Users
|
//Users
|
||||||
|
/// <inheritdoc />
|
||||||
|
public Task<RestGuildUser> AddGuildUserAsync(ulong id, string accessToken, Action<AddGuildUserProperties> func = null, RequestOptions options = null)
|
||||||
|
=> GuildHelper.AddGuildUserAsync(this, Discord, id, accessToken, func, options);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets a user from this guild.
|
/// Gets a user from this guild.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -1096,6 +1100,10 @@ namespace Discord.WebSocket
|
|||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
Task<IReadOnlyCollection<IGuildUser>> IGuild.GetUsersAsync(CacheMode mode, RequestOptions options)
|
Task<IReadOnlyCollection<IGuildUser>> IGuild.GetUsersAsync(CacheMode mode, RequestOptions options)
|
||||||
=> Task.FromResult<IReadOnlyCollection<IGuildUser>>(Users);
|
=> Task.FromResult<IReadOnlyCollection<IGuildUser>>(Users);
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
async Task<IGuildUser> IGuild.AddGuildUserAsync(ulong userId, string accessToken, Action<AddGuildUserProperties> func, RequestOptions options)
|
||||||
|
=> await AddGuildUserAsync(userId, accessToken, func, options);
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
Task<IGuildUser> IGuild.GetUserAsync(ulong id, CacheMode mode, RequestOptions options)
|
Task<IGuildUser> IGuild.GetUserAsync(ulong id, CacheMode mode, RequestOptions options)
|
||||||
=> Task.FromResult<IGuildUser>(GetUser(id));
|
=> Task.FromResult<IGuildUser>(GetUser(id));
|
||||||
|
|||||||
Reference in New Issue
Block a user