diff --git a/src/NadekoBot/Modules/Gambling/BlackJack/Blackjack.cs b/src/NadekoBot/Modules/Gambling/BlackJack/Blackjack.cs index 84e0a0dcd..9a45e6da7 100644 --- a/src/NadekoBot/Modules/Gambling/BlackJack/Blackjack.cs +++ b/src/NadekoBot/Modules/Gambling/BlackJack/Blackjack.cs @@ -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; } diff --git a/src/NadekoBot/Modules/Music/Services/AyuVoiceStateService.cs b/src/NadekoBot/Modules/Music/Services/AyuVoiceStateService.cs index 88d87877b..94d1071c7 100644 --- a/src/NadekoBot/Modules/Music/Services/AyuVoiceStateService.cs +++ b/src/NadekoBot/Modules/Music/Services/AyuVoiceStateService.cs @@ -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