private async Task <ConfigurationStep> AskForConfirmation(IPaginatable element) { var deletionConfirmationStep = new ConfigurationStep("Do you really want to do that?", interactive, context, ConfigurationStep.StepType.Reaction, null); var message = await context.Channel.SendMessageAsync($"delete: { element.display() }"); deletionConfirmationStep.MessagesToRemoveOnNextProgression.Add(message); var deniedStep = new ReactionAction(new Emoji("✅")); deniedStep.Action = async(ConfigurationStep a) => { a.Result = actionOnIndex(element); await Task.CompletedTask; }; var confirmStep = new ReactionAction(new Emoji("❌")); confirmStep.Action = async(ConfigurationStep a) => { a.Result = step; await Task.CompletedTask; }; deletionConfirmationStep.Actions.Add(confirmStep); deletionConfirmationStep.Actions.Add(deniedStep); return(deletionConfirmationStep); }
public PaginationWithAction(ConfigurationStep step, ConfigurationStep parent, System.Collections.Generic.List <IPaginatable> elements, bool confirmation, SocketCommandContext context, InteractiveService interactive) { this.step = step; this.parent = parent; this.elements = elements; this.needsConfirmation = confirmation; this.context = context; this.interactive = interactive; }
public ConfigurationStep(string description, InteractiveService interactive, SocketCommandContext context, StepType type, ConfigurationStep parent) { this.Interactive = interactive; this.description = description; Context = context; _criterion = new ReactionSameUserCriterion(); this.type = type; this.parent = parent; if (type == StepType.Reaction) { var abortAction = new ReactionAction(new Emoji("🆘")); abortAction.Action = (ConfigurationStep a) => { a.Result = null; return(Task.CompletedTask); }; this.Actions.Add(abortAction); } }
public void setup() { var prevAction = new ReactionAction(new Emoji("◀")); prevAction.Action = (ConfigurationStep a ) => { if (currentPage > 0) { currentPage--; } a.Result = step; return(Task.CompletedTask); }; var forwardAction = new ReactionAction(new Emoji("▶")); forwardAction.Action = (ConfigurationStep a ) => { if (currentPage < (Math.Ceiling((double)elements.Count) / elementOnPage)) { currentPage++; } a.Result = step; return(Task.CompletedTask); }; Func <int, Task <ConfigurationStep> > processAtIndex = async(int index) => { index = currentPage * elementOnPage + index; if (index < elements.Count) { var element = elements[index]; if (needsConfirmation) { return(await AskForConfirmation(element)); } else { actionOnIndex(element); } } return(null); }; var firstAction = new ReactionAction(new Emoji("\u0031\u20e3")); firstAction.Action = async(ConfigurationStep a) => { var otherStep = await processAtIndex(0); a.Result = otherStep ?? step; await Task.CompletedTask; }; var secondAction = new ReactionAction(new Emoji("\u0032\u20e3")); secondAction.Action = async(ConfigurationStep a) => { var otherStep = await processAtIndex(1); a.Result = otherStep ?? step; await Task.CompletedTask; }; var thirdAction = new ReactionAction(new Emoji("\u0033\u20e3")); thirdAction.Action = async(ConfigurationStep a) => { var otherStep = await processAtIndex(2); a.Result = otherStep ?? step; await Task.CompletedTask; }; var fourthAction = new ReactionAction(new Emoji("\u0034\u20e3")); fourthAction.Action = async(ConfigurationStep a) => { var otherStep = await processAtIndex(3); a.Result = otherStep ?? step; await Task.CompletedTask; }; var fifthAction = new ReactionAction(new Emoji("\u0035\u20e3")); fifthAction.Action = async(ConfigurationStep a) => { var otherStep = await processAtIndex(4); a.Result = otherStep ?? step; await Task.CompletedTask; }; var abortDeletionAction = new ReactionAction(new Emoji("❌")); abortDeletionAction.Action = async(ConfigurationStep a) => { a.Result = parent; await Task.CompletedTask; }; var skipPageBoundary = 3; var pageCount = Math.Ceiling((double)elements.Count / elementOnPage); var skipPages = pageCount >= skipPageBoundary; if (!skipPages) { step.Actions.Add(prevAction); } step.Actions.Add(firstAction); step.Actions.Add(secondAction); step.Actions.Add(thirdAction); step.Actions.Add(fourthAction); step.Actions.Add(fifthAction); if (!skipPages) { step.Actions.Add(forwardAction); } step.Actions.Add(abortDeletionAction); if (skipPages) { var skipPageStep = new ConfigurationStep("", this.interactive, this.context, ConfigurationStep.StepType.Text, step); skipPageStep.beforeTextPosted = async(ConfigurationStep step) => { // we need to re-render the text, because the page count might change step.description = $"Enter the page you want to skip to. Current page {currentPage + 1}/{pageCount}"; await Task.CompletedTask; }; skipPageStep.TextCallback = (string text, ConfigurationStep a) => { var result = 0; if (Int32.TryParse(text, out result)) { currentPage = result - 1; a.Result = step; } else { a.Result = skipPageStep; } return(Task.CompletedTask); }; var skipPagesAction = new ReactionAction(new Emoji("📇")); skipPagesAction.Action = async(ConfigurationStep a) => { a.Result = skipPageStep; await Task.CompletedTask; }; step.Actions.Add(skipPagesAction); } step.beforeTextPosted = async(ConfigurationStep a) => { a.additionalPosts.Clear(); for (int i = currentPage * elementOnPage; i < currentPage * elementOnPage + elementOnPage && i < elements.Count; i++) { var cmd = elements[i]; a.additionalPosts.Add(cmd.display()); } a.additionalPosts.Add($"Current page {currentPage + 1}/{pageCount}"); await Task.CompletedTask; }; }