private static API.DiscordRestApiClient CreateApiClient(DiscordRestConfig config) => new API.DiscordRestApiClient(config.RestClientProvider, DiscordRestConfig.UserAgent);
/// <summary> Creates a new Webhook Discord client. </summary> public DiscordWebhookClient(IWebhook webhook, DiscordRestConfig config) : this(config) { Webhook = webhook; _webhookId = Webhook.Id; }
private static API.DiscordRestApiClient CreateApiClient(DiscordRestConfig config) => new API.DiscordRestApiClient(config.RestClientProvider, DiscordRestConfig.UserAgent, useSystemClock: config.UseSystemClock, defaultRatelimitCallback: config.DefaultRatelimitCallback);
/// <summary> /// Creates a new Webhook Discord client. /// </summary> /// <param name="webhookUrl">The url of the webhook.</param> /// <param name="config">The configuration options to use for this client.</param> /// <exception cref="ArgumentException">Thrown if the <paramref name="webhookUrl"/> is an invalid format.</exception> /// <exception cref="ArgumentNullException">Thrown if the <paramref name="webhookUrl"/> is null or whitespace.</exception> public DiscordWebhookClient(string webhookUrl, DiscordRestConfig config) : this(config) { ParseWebhookUrl(webhookUrl, out _webhookId, out string token); ApiClient.LoginAsync(TokenType.Webhook, token).GetAwaiter().GetResult(); Webhook = WebhookClientHelper.GetWebhookAsync(this, _webhookId).GetAwaiter().GetResult(); }
// Long-running worker thread. private async Task WorkerAsync(Func <DiscordRestConfig, DiscordWebhookClient> clientFactory, CancellationToken ct) { var config = new DiscordRestConfig { RestClientProvider = DefaultRestClientProvider.Create(true) }; #if DEBUG config.LogLevel = LogSeverity.Info; #endif using var client = clientFactory(config); #if DEBUG client.Log += log => { Debug.WriteLine(log.ToString()); return(Task.CompletedTask); }; #endif var messageBuffer = new List <Embed>(); do { try { await Task.Delay(_RequestThrottleTime, ct); } catch (OperationCanceledException) { // cancelled from Delay } try { // Take 1 // Consider the case where ct is cancelled and we are draining the queue. if (!impendingMessagesSemaphore.Wait(0)) { await impendingMessagesSemaphore.WaitAsync(ct); } } catch (OperationCanceledException) { // cancelled from WaitAsync } for (int i = 0; i < _MaxMessagesPerPack; i++) { // Consume 1 var result = impendingMessages.TryDequeue(out var embed); Debug.Assert(result); if (result) { messageBuffer.Add(embed); } // Take another if (!impendingMessagesSemaphore.Wait(0)) { break; } } await client.SendMessageAsync(embeds : messageBuffer); messageBuffer.Clear(); } while (!ct.IsCancellationRequested || impendingMessagesSemaphore.CurrentCount > 0); }