Skip to content

Commit

Permalink
[Feature] SentRequest event (#2870)
Browse files Browse the repository at this point in the history
  • Loading branch information
Misha-133 authored Mar 4, 2024
1 parent fa51f0a commit fc2fc87
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/Discord.Net.Rest/BaseDiscordClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ public abstract class BaseDiscordClient : IDiscordClient
public event Func<Task> LoggedOut { add { _loggedOutEvent.Add(value); } remove { _loggedOutEvent.Remove(value); } }
private readonly AsyncEvent<Func<Task>> _loggedOutEvent = new AsyncEvent<Func<Task>>();

internal readonly AsyncEvent<Func<string, string, double, Task>> _sentRequest = new();
/// <summary>
/// Fired when a REST request is sent to the API. First parameter is the HTTP method,
/// second is the endpoint, and third is the time taken to complete the request.
/// </summary>
public event Func<string, string, double, Task> SentRequest { add { _sentRequest.Add(value); } remove { _sentRequest.Remove(value); } }

internal readonly Logger _restLogger;
private readonly SemaphoreSlim _stateLock;
private bool _isFirstLogin, _isDisposed;
Expand Down Expand Up @@ -61,6 +68,7 @@ internal BaseDiscordClient(DiscordRestConfig config, API.DiscordRestApiClient cl
await _restLogger.WarningAsync($"Rate limit triggered: {endpoint} Remaining: {info.Value.RetryAfter}s {(id.IsHashBucket ? $"(Bucket: {id.BucketHash})" : "")}").ConfigureAwait(false);
};
ApiClient.SentRequest += async (method, endpoint, millis) => await _restLogger.VerboseAsync($"{method} {endpoint}: {millis} ms").ConfigureAwait(false);
ApiClient.SentRequest += (method, endpoint, millis) => _sentRequest.InvokeAsync(method, endpoint, millis);
}

public async Task LoginAsync(TokenType tokenType, string token, bool validateToken = true)
Expand Down
2 changes: 2 additions & 0 deletions src/Discord.Net.WebSocket/DiscordShardedClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,8 @@ private void RegisterEvents(DiscordSocketClient client, bool isPrimary)
return Task.Delay(0);
};

client.SentRequest += (method, endpoint, millis) => _sentRequest.InvokeAsync(method, endpoint, millis);

client.Connected += () => _shardConnectedEvent.InvokeAsync(client);
client.Disconnected += (exception) => _shardDisconnectedEvent.InvokeAsync(exception, client);
client.Ready += () => _shardReadyEvent.InvokeAsync(client);
Expand Down

0 comments on commit fc2fc87

Please sign in to comment.