From 05d8af4d0eeef346e46e58988626386b02266e55 Mon Sep 17 00:00:00 2001 From: EkiciLP Date: Thu, 24 Mar 2022 09:17:31 +0100 Subject: [PATCH] added prompt timeout --- .../Prompt/Model/DiscordPromptBase.cs | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/TomatenMusicCore/Prompt/Model/DiscordPromptBase.cs b/TomatenMusicCore/Prompt/Model/DiscordPromptBase.cs index 8a214d4..1b210b8 100644 --- a/TomatenMusicCore/Prompt/Model/DiscordPromptBase.cs +++ b/TomatenMusicCore/Prompt/Model/DiscordPromptBase.cs @@ -23,6 +23,7 @@ namespace TomatenMusic.Prompt.Model public List Options { get; protected set; } = new List(); public DiscordClient _client { get; set; } public DiscordPromptBase LastPrompt { get; protected set; } + public System.Timers.Timer TimeoutTimer { get; set; } protected ILogger _logger { get; set; } @@ -132,6 +133,16 @@ namespace TomatenMusic.Prompt.Model Interaction = interaction; Message = await interaction.CreateFollowupMessageAsync(builder); State = PromptState.OPEN; + + long timeoutTime = (Interaction.CreationTimestamp.ToUnixTimeMilliseconds() + 900000) - DateTimeOffset.Now.ToUnixTimeMilliseconds(); + + if (TimeoutTimer != null) + TimeoutTimer.Close(); + + TimeoutTimer = new System.Timers.Timer(timeoutTime); + TimeoutTimer.Elapsed += OnTimeout; + TimeoutTimer.AutoReset = false; + TimeoutTimer.Start(); } public async Task UseAsync(DiscordMessage message) @@ -171,6 +182,22 @@ namespace TomatenMusic.Prompt.Model Message = message; await EditMessageAsync(builder); State = PromptState.OPEN; + + long timeoutTime = (Interaction.CreationTimestamp.ToUnixTimeMilliseconds() + 900000) - DateTimeOffset.Now.ToUnixTimeMilliseconds(); + + if (TimeoutTimer != null) + TimeoutTimer.Close(); + + TimeoutTimer = new System.Timers.Timer(timeoutTime); + TimeoutTimer.Elapsed += OnTimeout; + TimeoutTimer.AutoReset = false; + TimeoutTimer.Start(); + + } + + private void OnTimeout(object? sender, System.Timers.ElapsedEventArgs e) + { + _ = InvalidateAsync(); } private void AddGuids()