(ifcbrk) Add SearchUsersAsync (#1556)
This commit is contained in:
@@ -710,6 +710,22 @@ namespace Discord
|
|||||||
/// be or has been removed from this guild.
|
/// be or has been removed from this guild.
|
||||||
/// </returns>
|
/// </returns>
|
||||||
Task<int> PruneUsersAsync(int days = 30, bool simulate = false, RequestOptions options = null);
|
Task<int> PruneUsersAsync(int days = 30, bool simulate = false, RequestOptions options = null);
|
||||||
|
/// <summary>
|
||||||
|
/// Gets a collection of users in this guild that the name or nickname starts with the
|
||||||
|
/// provided <see cref="string"/> at <paramref name="query"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// The <paramref name="limit"/> can not be higher than <see cref="DiscordConfig.MaxUsersPerBatch"/>.
|
||||||
|
/// </remarks>
|
||||||
|
/// <param name="query">The partial name or nickname to search.</param>
|
||||||
|
/// <param name="limit">The maximum number of users to be gotten.</param>
|
||||||
|
/// <param name="mode">The <see cref="CacheMode" /> that determines whether the object should be fetched from cache.</param>
|
||||||
|
/// <param name="options">The options to be used when sending the request.</param>
|
||||||
|
/// <returns>
|
||||||
|
/// A task that represents the asynchronous get operation. The task result contains a collection of guild
|
||||||
|
/// users that the name or nickname starts with the provided <see cref="string"/> at <paramref name="query"/>.
|
||||||
|
/// </returns>
|
||||||
|
Task<IReadOnlyCollection<IGuildUser>> SearchUsersAsync(string query, int limit = DiscordConfig.MaxUsersPerBatch, CacheMode mode = CacheMode.AllowDownload, RequestOptions options = null);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the specified number of audit log entries for this guild.
|
/// Gets the specified number of audit log entries for this guild.
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
#pragma warning disable CS1591
|
||||||
|
namespace Discord.API.Rest
|
||||||
|
{
|
||||||
|
internal class SearchGuildMembersParams
|
||||||
|
{
|
||||||
|
public string Query { get; set; }
|
||||||
|
public Optional<int> Limit { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1136,6 +1136,22 @@ namespace Discord.API
|
|||||||
await SendJsonAsync("PATCH", () => $"guilds/{guildId}/members/{userId}", args, ids, options: options).ConfigureAwait(false);
|
await SendJsonAsync("PATCH", () => $"guilds/{guildId}/members/{userId}", args, ids, options: options).ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
public async Task<IReadOnlyCollection<GuildMember>> SearchGuildMembersAsync(ulong guildId, SearchGuildMembersParams args, RequestOptions options = null)
|
||||||
|
{
|
||||||
|
Preconditions.NotEqual(guildId, 0, nameof(guildId));
|
||||||
|
Preconditions.NotNull(args, nameof(args));
|
||||||
|
Preconditions.GreaterThan(args.Limit, 0, nameof(args.Limit));
|
||||||
|
Preconditions.AtMost(args.Limit, DiscordConfig.MaxUsersPerBatch, nameof(args.Limit));
|
||||||
|
Preconditions.NotNullOrEmpty(args.Query, nameof(args.Query));
|
||||||
|
options = RequestOptions.CreateOrClone(options);
|
||||||
|
|
||||||
|
int limit = args.Limit.GetValueOrDefault(DiscordConfig.MaxUsersPerBatch);
|
||||||
|
string query = args.Query;
|
||||||
|
|
||||||
|
var ids = new BucketIds(guildId: guildId);
|
||||||
|
Expression<Func<string>> endpoint = () => $"guilds/{guildId}/members/search?limit={limit}&query={query}";
|
||||||
|
return await SendAsync<IReadOnlyCollection<GuildMember>>("GET", endpoint, ids, options: options).ConfigureAwait(false);
|
||||||
|
}
|
||||||
|
|
||||||
//Guild Roles
|
//Guild Roles
|
||||||
public async Task<IReadOnlyCollection<Role>> GetGuildRolesAsync(ulong guildId, RequestOptions options = null)
|
public async Task<IReadOnlyCollection<Role>> GetGuildRolesAsync(ulong guildId, RequestOptions options = null)
|
||||||
|
|||||||
@@ -387,6 +387,17 @@ namespace Discord.Rest
|
|||||||
model = await client.ApiClient.BeginGuildPruneAsync(guild.Id, args, options).ConfigureAwait(false);
|
model = await client.ApiClient.BeginGuildPruneAsync(guild.Id, args, options).ConfigureAwait(false);
|
||||||
return model.Pruned;
|
return model.Pruned;
|
||||||
}
|
}
|
||||||
|
public static async Task<IReadOnlyCollection<RestGuildUser>> SearchUsersAsync(IGuild guild, BaseDiscordClient client,
|
||||||
|
string query, int? limit, RequestOptions options)
|
||||||
|
{
|
||||||
|
var apiArgs = new SearchGuildMembersParams
|
||||||
|
{
|
||||||
|
Query = query,
|
||||||
|
Limit = limit ?? Optional.Create<int>()
|
||||||
|
};
|
||||||
|
var models = await client.ApiClient.SearchGuildMembersAsync(guild.Id, apiArgs, options).ConfigureAwait(false);
|
||||||
|
return models.Select(x => RestGuildUser.Create(client, guild, x)).ToImmutableArray();
|
||||||
|
}
|
||||||
|
|
||||||
// Audit logs
|
// Audit logs
|
||||||
public static IAsyncEnumerable<IReadOnlyCollection<RestAuditLogEntry>> GetAuditLogsAsync(IGuild guild, BaseDiscordClient client,
|
public static IAsyncEnumerable<IReadOnlyCollection<RestAuditLogEntry>> GetAuditLogsAsync(IGuild guild, BaseDiscordClient client,
|
||||||
|
|||||||
@@ -634,6 +634,23 @@ namespace Discord.Rest
|
|||||||
public Task<int> PruneUsersAsync(int days = 30, bool simulate = false, RequestOptions options = null)
|
public Task<int> PruneUsersAsync(int days = 30, bool simulate = false, RequestOptions options = null)
|
||||||
=> GuildHelper.PruneUsersAsync(this, Discord, days, simulate, options);
|
=> GuildHelper.PruneUsersAsync(this, Discord, days, simulate, options);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets a collection of users in this guild that the name or nickname starts with the
|
||||||
|
/// provided <see cref="string"/> at <paramref name="query"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// The <paramref name="limit"/> can not be higher than <see cref="DiscordConfig.MaxUsersPerBatch"/>.
|
||||||
|
/// </remarks>
|
||||||
|
/// <param name="query">The partial name or nickname to search.</param>
|
||||||
|
/// <param name="limit">The maximum number of users to be gotten.</param>
|
||||||
|
/// <param name="options">The options to be used when sending the request.</param>
|
||||||
|
/// <returns>
|
||||||
|
/// A task that represents the asynchronous get operation. The task result contains a collection of guild
|
||||||
|
/// users that the name or nickname starts with the provided <see cref="string"/> at <paramref name="query"/>.
|
||||||
|
/// </returns>
|
||||||
|
public Task<IReadOnlyCollection<RestGuildUser>> SearchUsersAsync(string query, int limit = DiscordConfig.MaxUsersPerBatch, RequestOptions options = null)
|
||||||
|
=> GuildHelper.SearchUsersAsync(this, Discord, query, limit, options);
|
||||||
|
|
||||||
//Audit logs
|
//Audit logs
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the specified number of audit log entries for this guild.
|
/// Gets the specified number of audit log entries for this guild.
|
||||||
@@ -884,6 +901,14 @@ namespace Discord.Rest
|
|||||||
/// <exception cref="NotSupportedException">Downloading users is not supported for a REST-based guild.</exception>
|
/// <exception cref="NotSupportedException">Downloading users is not supported for a REST-based guild.</exception>
|
||||||
Task IGuild.DownloadUsersAsync() =>
|
Task IGuild.DownloadUsersAsync() =>
|
||||||
throw new NotSupportedException();
|
throw new NotSupportedException();
|
||||||
|
/// <inheritdoc />
|
||||||
|
async Task<IReadOnlyCollection<IGuildUser>> IGuild.SearchUsersAsync(string query, int limit, CacheMode mode, RequestOptions options)
|
||||||
|
{
|
||||||
|
if (mode == CacheMode.AllowDownload)
|
||||||
|
return await SearchUsersAsync(query, limit, options).ConfigureAwait(false);
|
||||||
|
else
|
||||||
|
return ImmutableArray.Create<IGuildUser>();
|
||||||
|
}
|
||||||
|
|
||||||
async Task<IReadOnlyCollection<IAuditLogEntry>> IGuild.GetAuditLogsAsync(int limit, CacheMode cacheMode, RequestOptions options,
|
async Task<IReadOnlyCollection<IAuditLogEntry>> IGuild.GetAuditLogsAsync(int limit, CacheMode cacheMode, RequestOptions options,
|
||||||
ulong? beforeId, ulong? userId, ActionType? actionType)
|
ulong? beforeId, ulong? userId, ActionType? actionType)
|
||||||
|
|||||||
@@ -850,6 +850,23 @@ namespace Discord.WebSocket
|
|||||||
_downloaderPromise.TrySetResultAsync(true);
|
_downloaderPromise.TrySetResultAsync(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets a collection of users in this guild that the name or nickname starts with the
|
||||||
|
/// provided <see cref="string"/> at <paramref name="query"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// The <paramref name="limit"/> can not be higher than <see cref="DiscordConfig.MaxUsersPerBatch"/>.
|
||||||
|
/// </remarks>
|
||||||
|
/// <param name="query">The partial name or nickname to search.</param>
|
||||||
|
/// <param name="limit">The maximum number of users to be gotten.</param>
|
||||||
|
/// <param name="options">The options to be used when sending the request.</param>
|
||||||
|
/// <returns>
|
||||||
|
/// A task that represents the asynchronous get operation. The task result contains a collection of guild
|
||||||
|
/// users that the name or nickname starts with the provided <see cref="string"/> at <paramref name="query"/>.
|
||||||
|
/// </returns>
|
||||||
|
public Task<IReadOnlyCollection<RestGuildUser>> SearchUsersAsync(string query, int limit = DiscordConfig.MaxUsersPerBatch, RequestOptions options = null)
|
||||||
|
=> GuildHelper.SearchUsersAsync(this, Discord, query, limit, options);
|
||||||
|
|
||||||
//Audit logs
|
//Audit logs
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the specified number of audit log entries for this guild.
|
/// Gets the specified number of audit log entries for this guild.
|
||||||
@@ -1224,6 +1241,14 @@ namespace Discord.WebSocket
|
|||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
Task<IGuildUser> IGuild.GetOwnerAsync(CacheMode mode, RequestOptions options)
|
Task<IGuildUser> IGuild.GetOwnerAsync(CacheMode mode, RequestOptions options)
|
||||||
=> Task.FromResult<IGuildUser>(Owner);
|
=> Task.FromResult<IGuildUser>(Owner);
|
||||||
|
/// <inheritdoc />
|
||||||
|
async Task<IReadOnlyCollection<IGuildUser>> IGuild.SearchUsersAsync(string query, int limit, CacheMode mode, RequestOptions options)
|
||||||
|
{
|
||||||
|
if (mode == CacheMode.AllowDownload)
|
||||||
|
return await SearchUsersAsync(query, limit, options).ConfigureAwait(false);
|
||||||
|
else
|
||||||
|
return ImmutableArray.Create<IGuildUser>();
|
||||||
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
async Task<IReadOnlyCollection<IAuditLogEntry>> IGuild.GetAuditLogsAsync(int limit, CacheMode cacheMode, RequestOptions options,
|
async Task<IReadOnlyCollection<IAuditLogEntry>> IGuild.GetAuditLogsAsync(int limit, CacheMode cacheMode, RequestOptions options,
|
||||||
|
|||||||
Reference in New Issue
Block a user