Set a max bot execution time

If you want the bot to end the overall execution after a given amount of time, you can use this snippet. The code is commented so you can see what it’s doing.

// These lines will set the bot to automatically error out after N seconds
// You can change the value to your needs.
int timeoutSeconds = 5;
CancellationTokenSource cts = new CancellationTokenSource(TimeSpan.FromSeconds(timeoutSeconds));
CancellationTokenSource linkedCts = CancellationTokenSource.CreateLinkedTokenSource(data.CancellationToken, cts.Token);
data.CancellationToken = linkedCts.Token;

// The try/catch block is only needed if you don't want to end up with the ERROR
// status and an OperationCanceledException.
try
{

// Put your config blocks from here
// --------------------------------
BLOCK:Delay
  milliseconds = 10000
ENDBLOCK
// --------------------------------
// Up to here

}
catch (OperationCanceledException)
{
  // These two lines are for preventing memory leaks
  cts.Dispose();
  linkedCts.Dispose();

  // The final status you want to set (e.g. RETRY, BAN, etc.)
  data.STATUS = "RETRY";
}

Ruri

5 Likes