Support filtering audit log entries by after id (#2620)
Adds support for filtering audit log entires with GetAuditLogsAsync. Adds the ability to specify an afterId as specified on [Discord developers docs](https://discord.com/developers/docs/resources/audit-log#get-guild-audit-log).
This commit is contained in:
@@ -1003,13 +1003,14 @@ namespace Discord
|
|||||||
/// <param name="beforeId">The audit log entry ID to get entries before.</param>
|
/// <param name="beforeId">The audit log entry ID to get entries before.</param>
|
||||||
/// <param name="actionType">The type of actions to filter.</param>
|
/// <param name="actionType">The type of actions to filter.</param>
|
||||||
/// <param name="userId">The user ID to filter entries for.</param>
|
/// <param name="userId">The user ID to filter entries for.</param>
|
||||||
|
/// <param name="afterId">The audit log entry ID to get entries after.</param>
|
||||||
/// <returns>
|
/// <returns>
|
||||||
/// A task that represents the asynchronous get operation. The task result contains a read-only collection
|
/// A task that represents the asynchronous get operation. The task result contains a read-only collection
|
||||||
/// of the requested audit log entries.
|
/// of the requested audit log entries.
|
||||||
/// </returns>
|
/// </returns>
|
||||||
Task<IReadOnlyCollection<IAuditLogEntry>> GetAuditLogsAsync(int limit = DiscordConfig.MaxAuditLogEntriesPerBatch,
|
Task<IReadOnlyCollection<IAuditLogEntry>> GetAuditLogsAsync(int limit = DiscordConfig.MaxAuditLogEntriesPerBatch,
|
||||||
CacheMode mode = CacheMode.AllowDownload, RequestOptions options = null, ulong? beforeId = null, ulong? userId = null,
|
CacheMode mode = CacheMode.AllowDownload, RequestOptions options = null, ulong? beforeId = null, ulong? userId = null,
|
||||||
ActionType? actionType = null);
|
ActionType? actionType = null, ulong? afterId = null);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets a webhook found within this guild.
|
/// Gets a webhook found within this guild.
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ namespace Discord.API.Rest
|
|||||||
{
|
{
|
||||||
public Optional<int> Limit { get; set; }
|
public Optional<int> Limit { get; set; }
|
||||||
public Optional<ulong> BeforeEntryId { get; set; }
|
public Optional<ulong> BeforeEntryId { get; set; }
|
||||||
|
public Optional<ulong> AfterEntryId { get; set; }
|
||||||
public Optional<ulong> UserId { get; set; }
|
public Optional<ulong> UserId { get; set; }
|
||||||
public Optional<int> ActionType { get; set; }
|
public Optional<int> ActionType { get; set; }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2331,6 +2331,11 @@ namespace Discord.API
|
|||||||
queryArgs.Append("&action_type=")
|
queryArgs.Append("&action_type=")
|
||||||
.Append(args.ActionType.Value);
|
.Append(args.ActionType.Value);
|
||||||
}
|
}
|
||||||
|
if (args.AfterEntryId.IsSpecified)
|
||||||
|
{
|
||||||
|
queryArgs.Append("&after=")
|
||||||
|
.Append(args.AfterEntryId);
|
||||||
|
}
|
||||||
|
|
||||||
// Still use string interpolation for the query w/o params, as this is necessary for CreateBucketId
|
// Still use string interpolation for the query w/o params, as this is necessary for CreateBucketId
|
||||||
endpoint = () => $"guilds/{guildId}/audit-logs?limit={limit}{queryArgs.ToString()}";
|
endpoint = () => $"guilds/{guildId}/audit-logs?limit={limit}{queryArgs.ToString()}";
|
||||||
|
|||||||
@@ -611,7 +611,7 @@ namespace Discord.Rest
|
|||||||
|
|
||||||
#region Audit logs
|
#region Audit logs
|
||||||
public static IAsyncEnumerable<IReadOnlyCollection<RestAuditLogEntry>> GetAuditLogsAsync(IGuild guild, BaseDiscordClient client,
|
public static IAsyncEnumerable<IReadOnlyCollection<RestAuditLogEntry>> GetAuditLogsAsync(IGuild guild, BaseDiscordClient client,
|
||||||
ulong? from, int? limit, RequestOptions options, ulong? userId = null, ActionType? actionType = null)
|
ulong? from, int? limit, RequestOptions options, ulong? userId = null, ActionType? actionType = null, ulong? afterId = null)
|
||||||
{
|
{
|
||||||
return new PagedAsyncEnumerable<RestAuditLogEntry>(
|
return new PagedAsyncEnumerable<RestAuditLogEntry>(
|
||||||
DiscordConfig.MaxAuditLogEntriesPerBatch,
|
DiscordConfig.MaxAuditLogEntriesPerBatch,
|
||||||
@@ -627,6 +627,8 @@ namespace Discord.Rest
|
|||||||
args.UserId = userId.Value;
|
args.UserId = userId.Value;
|
||||||
if (actionType.HasValue)
|
if (actionType.HasValue)
|
||||||
args.ActionType = (int)actionType.Value;
|
args.ActionType = (int)actionType.Value;
|
||||||
|
if (afterId.HasValue)
|
||||||
|
args.AfterEntryId = afterId.Value;
|
||||||
var model = await client.ApiClient.GetAuditLogsAsync(guild.Id, args, options);
|
var model = await client.ApiClient.GetAuditLogsAsync(guild.Id, args, options);
|
||||||
return model.Entries.Select((x) => RestAuditLogEntry.Create(client, model, x)).ToImmutableArray();
|
return model.Entries.Select((x) => RestAuditLogEntry.Create(client, model, x)).ToImmutableArray();
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -914,12 +914,13 @@ namespace Discord.Rest
|
|||||||
/// <param name="beforeId">The audit log entry ID to get entries before.</param>
|
/// <param name="beforeId">The audit log entry ID to get entries before.</param>
|
||||||
/// <param name="actionType">The type of actions to filter.</param>
|
/// <param name="actionType">The type of actions to filter.</param>
|
||||||
/// <param name="userId">The user ID to filter entries for.</param>
|
/// <param name="userId">The user ID to filter entries for.</param>
|
||||||
|
/// <param name="afterId">The audit log entry ID to get entries after.</param>
|
||||||
/// <returns>
|
/// <returns>
|
||||||
/// A task that represents the asynchronous get operation. The task result contains a read-only collection
|
/// A task that represents the asynchronous get operation. The task result contains a read-only collection
|
||||||
/// of the requested audit log entries.
|
/// of the requested audit log entries.
|
||||||
/// </returns>
|
/// </returns>
|
||||||
public IAsyncEnumerable<IReadOnlyCollection<RestAuditLogEntry>> GetAuditLogsAsync(int limit, RequestOptions options = null, ulong? beforeId = null, ulong? userId = null, ActionType? actionType = null)
|
public IAsyncEnumerable<IReadOnlyCollection<RestAuditLogEntry>> GetAuditLogsAsync(int limit, RequestOptions options = null, ulong? beforeId = null, ulong? userId = null, ActionType? actionType = null, ulong? afterId = null)
|
||||||
=> GuildHelper.GetAuditLogsAsync(this, Discord, beforeId, limit, options, userId: userId, actionType: actionType);
|
=> GuildHelper.GetAuditLogsAsync(this, Discord, beforeId, limit, options, userId: userId, actionType: actionType, afterId: afterId);
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Webhooks
|
#region Webhooks
|
||||||
@@ -1508,10 +1509,10 @@ namespace Discord.Rest
|
|||||||
}
|
}
|
||||||
|
|
||||||
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, ulong? afterId)
|
||||||
{
|
{
|
||||||
if (cacheMode == CacheMode.AllowDownload)
|
if (cacheMode == CacheMode.AllowDownload)
|
||||||
return (await GetAuditLogsAsync(limit, options, beforeId: beforeId, userId: userId, actionType: actionType).FlattenAsync().ConfigureAwait(false)).ToImmutableArray();
|
return (await GetAuditLogsAsync(limit, options, beforeId: beforeId, userId: userId, actionType: actionType, afterId: afterId).FlattenAsync().ConfigureAwait(false)).ToImmutableArray();
|
||||||
else
|
else
|
||||||
return ImmutableArray.Create<IAuditLogEntry>();
|
return ImmutableArray.Create<IAuditLogEntry>();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1397,12 +1397,13 @@ namespace Discord.WebSocket
|
|||||||
/// <param name="beforeId">The audit log entry ID to filter entries before.</param>
|
/// <param name="beforeId">The audit log entry ID to filter entries before.</param>
|
||||||
/// <param name="actionType">The type of actions to filter.</param>
|
/// <param name="actionType">The type of actions to filter.</param>
|
||||||
/// <param name="userId">The user ID to filter entries for.</param>
|
/// <param name="userId">The user ID to filter entries for.</param>
|
||||||
|
/// <param name="afterId">The audit log entry ID to filter entries after.</param>
|
||||||
/// <returns>
|
/// <returns>
|
||||||
/// A task that represents the asynchronous get operation. The task result contains a read-only collection
|
/// A task that represents the asynchronous get operation. The task result contains a read-only collection
|
||||||
/// of the requested audit log entries.
|
/// of the requested audit log entries.
|
||||||
/// </returns>
|
/// </returns>
|
||||||
public IAsyncEnumerable<IReadOnlyCollection<RestAuditLogEntry>> GetAuditLogsAsync(int limit, RequestOptions options = null, ulong? beforeId = null, ulong? userId = null, ActionType? actionType = null)
|
public IAsyncEnumerable<IReadOnlyCollection<RestAuditLogEntry>> GetAuditLogsAsync(int limit, RequestOptions options = null, ulong? beforeId = null, ulong? userId = null, ActionType? actionType = null, ulong? afterId = null)
|
||||||
=> GuildHelper.GetAuditLogsAsync(this, Discord, beforeId, limit, options, userId: userId, actionType: actionType);
|
=> GuildHelper.GetAuditLogsAsync(this, Discord, beforeId, limit, options, userId: userId, actionType: actionType, afterId: afterId);
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Webhooks
|
#region Webhooks
|
||||||
@@ -2093,10 +2094,10 @@ namespace Discord.WebSocket
|
|||||||
|
|
||||||
/// <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,
|
||||||
ulong? beforeId, ulong? userId, ActionType? actionType)
|
ulong? beforeId, ulong? userId, ActionType? actionType, ulong? afterId)
|
||||||
{
|
{
|
||||||
if (cacheMode == CacheMode.AllowDownload)
|
if (cacheMode == CacheMode.AllowDownload)
|
||||||
return (await GetAuditLogsAsync(limit, options, beforeId: beforeId, userId: userId, actionType: actionType).FlattenAsync().ConfigureAwait(false)).ToImmutableArray();
|
return (await GetAuditLogsAsync(limit, options, beforeId: beforeId, userId: userId, actionType: actionType, afterId: afterId).FlattenAsync().ConfigureAwait(false)).ToImmutableArray();
|
||||||
else
|
else
|
||||||
return ImmutableArray.Create<IAuditLogEntry>();
|
return ImmutableArray.Create<IAuditLogEntry>();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user