mirror of
https://github.com/SlothDpal/Relaunch-Process.git
synced 2026-02-22 17:27:38 +03:00
discord queue upgrade
This commit is contained in:
@@ -13,12 +13,17 @@ namespace Discord.Webhook
|
||||
{
|
||||
public string Url { get; set; }
|
||||
|
||||
private UInt64 queueNum = 0;
|
||||
private UInt64 totalMessages = 0;
|
||||
private readonly ConcurrentQueue<(UInt64 num, DiscordMessage message, FileInfo[] files)> _queue = new ConcurrentQueue<(UInt64 num, DiscordMessage, FileInfo[])>();
|
||||
private readonly SemaphoreSlim _semaphore = new SemaphoreSlim(1, 1);
|
||||
private bool _isProcessing;
|
||||
private CancellationTokenSource _cts = new CancellationTokenSource();
|
||||
|
||||
public async Task SendAsync(DiscordMessage message, params FileInfo[] files)
|
||||
public UInt64 GetTotalMessages() => totalMessages;
|
||||
public int GetQueueSize() => _queue.Count;
|
||||
public void CancelProcessing() => _cts.Cancel();
|
||||
|
||||
public async Task<bool> SendAsync(DiscordMessage message, params FileInfo[] files)
|
||||
{
|
||||
if (string.IsNullOrEmpty(Url))
|
||||
throw new ArgumentNullException("Invalid Webhook URL.");
|
||||
@@ -54,46 +59,70 @@ namespace Discord.Webhook
|
||||
|
||||
try
|
||||
{
|
||||
var response = await client.PostAsync(Url, content);
|
||||
var response = await client.PostAsync(Url, content, _cts.Token);
|
||||
response.EnsureSuccessStatusCode();
|
||||
}
|
||||
catch (HttpRequestException ex)
|
||||
{
|
||||
Debug.WriteLine($"Discord webhook request failed: {ex.Message}");
|
||||
return false;
|
||||
}
|
||||
catch (TaskCanceledException ex)
|
||||
{
|
||||
Debug.WriteLine($"Discord webhook request cancelled: {ex.Message}");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private async Task ProcessQueueAsync()
|
||||
{
|
||||
while (_queue.TryDequeue(out var item))
|
||||
while (_queue.TryPeek(out var item))
|
||||
{
|
||||
if (_cts.Token.IsCancellationRequested)
|
||||
{
|
||||
Debug.WriteLine("Discord queue processing cancelled.");
|
||||
break;
|
||||
}
|
||||
await _semaphore.WaitAsync();
|
||||
try
|
||||
{
|
||||
Debug.WriteLine($"Processing message {item.num}. Queue size: {_queue.Count}");
|
||||
await SendAsync(item.message, item.files);
|
||||
Debug.WriteLine($"Message {item.num} sent.");
|
||||
if (await SendAsync(item.message, item.files))
|
||||
{
|
||||
_queue.TryDequeue(out var deqItem);
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
_semaphore.Release();
|
||||
}
|
||||
Task.Delay(500).Wait();
|
||||
try
|
||||
{
|
||||
await Task.Delay(1000, _cts.Token); // Discord rate limit: 1 message per second
|
||||
}
|
||||
catch (TaskCanceledException)
|
||||
{
|
||||
Debug.WriteLine($"Discord queue processing cancelled during delay. Was {_queue.Count} messages in queue. {totalMessages} messages in session. ");
|
||||
break;
|
||||
}
|
||||
}
|
||||
_isProcessing = false;
|
||||
}
|
||||
|
||||
public void Send(DiscordMessage message, params FileInfo[] files)
|
||||
{
|
||||
_queue.Enqueue((queueNum++, message, files));
|
||||
Debug.WriteLine($"Message {queueNum-1} added. Queue size: {_queue.Count}");
|
||||
_queue.Enqueue((totalMessages++, message, files));
|
||||
Debug.WriteLine($"Message {totalMessages-1} added. Queue size: {_queue.Count}");
|
||||
if (_isProcessing)
|
||||
{
|
||||
Debug.WriteLine("Already processing queue.");
|
||||
return;
|
||||
}
|
||||
Debug.WriteLine("Run ProcessQueueAsync");
|
||||
_cts.Dispose();
|
||||
_cts = new CancellationTokenSource();
|
||||
_isProcessing = true;
|
||||
Task.Run(ProcessQueueAsync);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user