/// <summary>The internal method that gets scheduled for later when scheduling a delayed send task.</summary> public static void WaitingTask(object _self) { MessageBulker self = _self as MessageBulker; Task.Delay(300).Wait(); lock (self.Internal.Locker) { self.Internal.HasSendTaskActive = false; try { self.Internal.DoSend(); } catch (Exception ex) { Console.Error.WriteLine($"MessageBulker encountered exception while sending message: {ex}"); } } }
/// <summary>The internal direct send-to-channel message. Do not call directly, use <see cref="Send(string)"/>.</summary> public void DoSend() { StringBuilder output = new(); lock (Locker) { while (ToSend.Any()) { string next = ToSend.Peek(); if (output.Length + next.Length > 1800 && output.Length > 0) { break; } if (output.Length > 0) { output.Append('\n'); } output.Append(next); ToSend.Dequeue(); } if (ToSend.Any()) { ScheduleTask(); } if (output.Length == 0) { return; } LastSentTicks = Environment.TickCount64; } MessageBulker self = Self; Task.Factory.StartNew(() => // TODO: Does this need to be swapped to Thread as well? { try { self.Channel.SendMessageAsync(output.ToString(), allowedMentions: self.Mentions).Wait(); } catch (Exception ex) { Console.Error.WriteLine($"MessageBulker encountered error while sending message to channel {self.Channel.Id}: {ex}"); } }); }