mirror of
https://gitlab.com/Kwoth/nadekobot.git
synced 2025-09-10 17:28:27 -04:00
Cancel some delay tasks if they're not being used
This commit is contained in:
@@ -97,14 +97,23 @@ public class Blackjack
|
||||
|
||||
private async Task PromptUserMove(User usr)
|
||||
{
|
||||
var pause = Task.Delay(20000); //10 seconds to decide
|
||||
using var cts = new CancellationTokenSource();
|
||||
var pause = Task.Delay(20000, cts.Token); //10 seconds to decide
|
||||
CurrentUser = usr;
|
||||
currentUserMove = new();
|
||||
await PrintState();
|
||||
// either wait for the user to make an action and
|
||||
// if he doesn't - stand
|
||||
var finished = await Task.WhenAny(pause, currentUserMove.Task);
|
||||
if (finished == pause) await Stand(usr);
|
||||
if (finished == pause)
|
||||
{
|
||||
await Stand(usr);
|
||||
}
|
||||
else
|
||||
{
|
||||
cts.Cancel();
|
||||
}
|
||||
|
||||
CurrentUser = null;
|
||||
currentUserMove = null;
|
||||
}
|
||||
|
@@ -146,7 +146,8 @@ public sealed class AyuVoiceStateService : INService
|
||||
await SendJoinVoiceChannelInternalAsync(guildId, channelId);
|
||||
|
||||
// create a delay task, how much to wait for gateway response
|
||||
var delayTask = Task.Delay(2500);
|
||||
using var cts = new CancellationTokenSource();
|
||||
var delayTask = Task.Delay(2500, cts.Token);
|
||||
|
||||
// either delay or successful voiceStateUpdate
|
||||
var maybeUpdateTask = Task.WhenAny(delayTask, voiceStateUpdatedSource.Task);
|
||||
@@ -156,8 +157,14 @@ public sealed class AyuVoiceStateService : INService
|
||||
// wait for both to end (max 1s) and check if either of them is a delay task
|
||||
var results = await Task.WhenAll(maybeUpdateTask, maybeServerTask);
|
||||
if (results[0] == delayTask || results[1] == delayTask)
|
||||
{
|
||||
// if either is delay, return null - connection unsuccessful
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
cts.Cancel();
|
||||
}
|
||||
|
||||
// if both are succesful, that means we can safely get
|
||||
// the values from completion sources
|
||||
|
Reference in New Issue
Block a user