public async Task RemoveSessionAsync(CommandSession session) { while (!Sessions.TryRemove(session, out var x)) { await Task.Delay(100); } }
public async Task <IDiscordMessage> WaitForNextMessage(CommandSession session, int timeOutInMilliseconds = 10000) { IDiscordMessage nextMessage = null; if (sessionCache.TryAdd(session, (msg) => { nextMessage = msg; })) { while (nextMessage == null) { await Task.Delay(100); timeOutInMilliseconds -= 100; if (timeOutInMilliseconds <= 0) { throw new TimeoutException(); } } sessionCache.TryRemove(session, out var x); } return(nextMessage); }
public async Task AddSessionAsync(CommandSession session, CommandHandler handler, TimeSpan?expiration = null) { if (Sessions.TryGetValue(session, out var handlerTuple)) { if (handlerTuple.Item2 >= DateTime.Now) { throw new SessionInUseException(); } await RemoveSessionAsync(session); } while (!Sessions.TryAdd(session, new Tuple <CommandHandler, DateTime>(handler, DateTime.Now + (expiration ?? TimeSpan.FromSeconds(30))))) { await Task.Delay(100); } }