public async Task <IResponsiveMessageResponse> GetResponseAsync(ICommandContext context, Messaging.IMessage message, bool allowCancellation = true) { ResponsiveMessageInfo info = await SendMessageAndReturnInfoAsync(context, message, allowCancellation); IResponsiveMessageResponse response = new ResponseMessageResponse(null, true); if (info != null) { // Block until we get a response. info.Waiter = new ManualResetEvent(false); info.Waiter.WaitOne(); response = info.Response; if (response is null) { response = new ResponseMessageResponse(null, true); } if (response.Canceled) { await context.Channel.SendMessageAsync(embed : EmbedUtilities.BuildInfoEmbed("The command has been canceled.").ToDiscordEmbed()); } } return(response); }
private async Task <ResponsiveMessageInfo> SendMessageAndReturnInfoAsync(ICommandContext context, Messaging.IMessage message, bool allowCancellation) { ResponsiveMessageInfo info = null; if (message != null) { if (allowCancellation) { message.Text += $"\nTo cancel, reply with \"{CancellationString}\"."; } IUserMessage sentMessage = await context.Channel.SendMessageAsync(message?.Text, false, message?.Embed?.ToDiscordEmbed()); if (messages.Count() >= MaxResponsiveMessages) { // Remove the oldest message. ulong oldestMessageUserId = messages .OrderBy(pair => pair.Value.Context.Message.Timestamp) .Select(pair => pair.Key) .First(); RemoveMessageInfo(oldestMessageUserId); } info = new ResponsiveMessageInfo { AllowCancellation = allowCancellation, Context = context }; if (messages.TryAdd(context.User.Id, info)) { return(info); } } return(info); }