aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarko Zajc <marko@zajc.eu.org>2022-12-13 01:37:21 +0100
committerMarko Zajc <marko@zajc.eu.org>2022-12-13 01:37:21 +0100
commit1cb56e16ad6ac46298522b9ba20bd77d5a67ef64 (patch)
tree3d9f03ca9419cefaa0aa542df017234e0761092f
parent00615aef23830c190aa9a059d2369313dc7c8db7 (diff)
Allow override of prompt deletion for confirmation prompts
-rw-r--r--src/main/java/libot/core/entities/CommandContext.java71
-rw-r--r--src/main/java/libot/utils/EventUtils.java5
2 files changed, 62 insertions, 14 deletions
diff --git a/src/main/java/libot/core/entities/CommandContext.java b/src/main/java/libot/core/entities/CommandContext.java
index 5357eb0..a729068 100644
--- a/src/main/java/libot/core/entities/CommandContext.java
+++ b/src/main/java/libot/core/entities/CommandContext.java
@@ -793,8 +793,12 @@ public class CommandContext {
793 793
794 // ===============* confirm *=============== 794 // ===============* confirm *===============
795 795
796 @SuppressWarnings("null")
797 public boolean confirm(@Nonnull String message) { 796 public boolean confirm(@Nonnull String message) {
797 return confirm(message, false);
798 }
799
800 @SuppressWarnings("null")
801 public boolean confirm(@Nonnull String message, boolean keepPrompt) {
798 String useMessage = message; 802 String useMessage = message;
799 if (!hasChannelPermission(MESSAGE_ADD_REACTION)) 803 if (!hasChannelPermission(MESSAGE_ADD_REACTION))
800 useMessage += "\n" + FORMAT_FALLBACK_ASK; 804 useMessage += "\n" + FORMAT_FALLBACK_ASK;
@@ -802,14 +806,18 @@ public class CommandContext {
802 // because the stupid user denied/forgot to grant it 806 // because the stupid user denied/forgot to grant it
803 807
804 try { 808 try {
805 return getConfirmation(reply(useMessage).get()); 809 return getConfirmation(reply(useMessage).get(), false);
806 } catch (ExecutionException | InterruptedException e) { // NOSONAR 810 } catch (ExecutionException | InterruptedException e) { // NOSONAR
807 throw asUnchecked(e); 811 throw asUnchecked(e);
808 } 812 }
809 } 813 }
810 814
811 @SuppressWarnings("null")
812 public boolean confirm(@Nonnull MessageEmbed embed) { 815 public boolean confirm(@Nonnull MessageEmbed embed) {
816 return confirm(embed, false);
817 }
818
819 @SuppressWarnings("null")
820 public boolean confirm(@Nonnull MessageEmbed embed, boolean keepPrompt) {
813 MessageEmbed useEmbed; 821 MessageEmbed useEmbed;
814 if (!canReact()) { // fallback ditto 822 if (!canReact()) { // fallback ditto
815 var eb = new EmbedBuilder(embed); 823 var eb = new EmbedBuilder(embed);
@@ -825,30 +833,47 @@ public class CommandContext {
825 } 833 }
826 834
827 try { 835 try {
828 return getConfirmation(reply(useEmbed).get()); 836 return getConfirmation(reply(useEmbed).get(), keepPrompt);
829 } catch (ExecutionException | InterruptedException e) { // NOSONAR 837 } catch (ExecutionException | InterruptedException e) { // NOSONAR
830 throw asUnchecked(e); 838 throw asUnchecked(e);
831 } 839 }
832 } 840 }
833 841
834 public boolean confirm(@Nonnull EmbedBuilder builder) { 842 public boolean confirm(@Nonnull EmbedBuilder builder) {
835 return confirm(builder.build()); 843 return confirm(builder.build(), false);
844 }
845
846 public boolean confirm(@Nonnull EmbedBuilder builder, boolean keepPrompt) {
847 return confirm(builder.build(), keepPrompt);
836 } 848 }
837 849
838 public boolean confirm(@Nullable String title, @Nonnull String message, @Nullable String footer, 850 public boolean confirm(@Nullable String title, @Nonnull String message, @Nullable String footer,
839 @Nullable Color color) { 851 @Nullable Color color) {
852 return confirm(title, message, footer, color, false);
853 }
854
855 public boolean confirm(@Nullable String title, @Nonnull String message, @Nullable String footer,
856 @Nullable Color color, boolean keepPrompt) {
840 EmbedBuilder builder = 857 EmbedBuilder builder =
841 new EmbedBuilder().setTitle(title).appendDescription(message).setFooter(footer, null).setColor(color); 858 new EmbedBuilder().setTitle(title).appendDescription(message).setFooter(footer, null).setColor(color);
842 859
843 return confirm(builder.build()); 860 return confirm(builder.build(), keepPrompt);
844 } 861 }
845 862
846 public boolean confirm(@Nullable String title, @Nonnull String message, @Nullable Color color) { 863 public boolean confirm(@Nullable String title, @Nonnull String message, @Nullable Color color) {
847 return confirm(title, message, null, color); 864 return confirm(title, message, null, color, false);
865 }
866
867 public boolean confirm(@Nullable String title, @Nonnull String message, @Nullable Color color, boolean keepPrompt) {
868 return confirm(title, message, null, color, keepPrompt);
848 } 869 }
849 870
850 public boolean confirm(@Nonnull String message, @Nullable Color color) { 871 public boolean confirm(@Nonnull String message, @Nullable Color color) {
851 return confirm(null, message, null, color); 872 return confirm(null, message, null, color, false);
873 }
874
875 public boolean confirm(@Nonnull String message, @Nullable Color color, boolean keepPrompt) {
876 return confirm(null, message, null, color, keepPrompt);
852 } 877 }
853 878
854 // ===============* confirmf *=============== 879 // ===============* confirmf *===============
@@ -859,18 +884,40 @@ public class CommandContext {
859 } 884 }
860 885
861 @SuppressWarnings("null") 886 @SuppressWarnings("null")
887 public boolean confirmf(@Nonnull String messageFormat, boolean keepPrompt, @Nonnull Object... args) {
888 return confirm(format(messageFormat, args), keepPrompt);
889 }
890
891 @SuppressWarnings("null")
862 public boolean confirmf(@Nullable String title, @Nonnull String messageFormat, @Nullable String footer, 892 public boolean confirmf(@Nullable String title, @Nonnull String messageFormat, @Nullable String footer,
863 @Nullable Color color, @Nonnull Object... args) { 893 @Nullable Color color, @Nonnull Object... args) {
864 return confirm(title, format(messageFormat, args), footer, color); 894 return confirm(title, format(messageFormat, args), footer, color);
895
896 }
897
898 @SuppressWarnings("null")
899 public boolean confirmf(@Nullable String title, @Nonnull String messageFormat, @Nullable String footer,
900 boolean keepPrompt, @Nullable Color color, @Nonnull Object... args) {
901 return confirm(title, format(messageFormat, args), footer, color, keepPrompt);
865 } 902 }
866 903
867 public boolean confirmf(@Nullable String title, @Nonnull String messageFormat, @Nullable Color color, 904 public boolean confirmf(@Nullable String title, @Nonnull String messageFormat, @Nullable Color color,
868 @Nonnull Object... args) { 905 @Nonnull Object... args) {
869 return confirmf(title, messageFormat, null, color, args); 906 return confirmf(title, messageFormat, null, false, color, args);
907 }
908
909 public boolean confirmf(@Nullable String title, @Nonnull String messageFormat, boolean keepPrompt,
910 @Nullable Color color, @Nonnull Object... args) {
911 return confirmf(title, messageFormat, null, keepPrompt, color, args);
870 } 912 }
871 913
872 public boolean confirmf(@Nonnull String messageFormat, @Nullable Color color, @Nonnull Object... args) { 914 public boolean confirmf(@Nonnull String messageFormat, @Nullable Color color, @Nonnull Object... args) {
873 return confirmf(null, messageFormat, null, color, args); 915 return confirmf(null, messageFormat, null, false, color, args);
916 }
917
918 public boolean confirmf(@Nonnull String messageFormat, boolean keepPrompt, @Nullable Color color,
919 @Nonnull Object... args) {
920 return confirmf(null, messageFormat, null, keepPrompt, color, args);
874 } 921 }
875 922
876 // ===============* askraw *=============== 923 // ===============* askraw *===============
@@ -1027,13 +1074,13 @@ public class CommandContext {
1027 1074
1028 // ===============* Internal *=============== 1075 // ===============* Internal *===============
1029 1076
1030 private boolean getConfirmation(@Nonnull Message m) { 1077 private boolean getConfirmation(@Nonnull Message m, boolean keepPrompt) {
1031 if (canReact()) { 1078 if (canReact()) {
1032 m.addReaction(ACCEPT_EMOJI).queue(); 1079 m.addReaction(ACCEPT_EMOJI).queue();
1033 m.addReaction(DENY_EMOJI).queue(); 1080 m.addReaction(DENY_EMOJI).queue();
1034 } 1081 }
1035 try { 1082 try {
1036 return getWaiter().awaitBoolean(m); 1083 return getWaiter().awaitBoolean(m, keepPrompt);
1037 } catch (InterruptedException e) { // NOSONAR 1084 } catch (InterruptedException e) { // NOSONAR
1038 throw asUnchecked(e); 1085 throw asUnchecked(e);
1039 } 1086 }
diff --git a/src/main/java/libot/utils/EventUtils.java b/src/main/java/libot/utils/EventUtils.java
index 85687cb..53428ad 100644
--- a/src/main/java/libot/utils/EventUtils.java
+++ b/src/main/java/libot/utils/EventUtils.java
@@ -71,7 +71,7 @@ public class EventUtils {
71 }, null, this.timeout, SECONDS, MessageReceivedEvent.class).getMessage(); 71 }, null, this.timeout, SECONDS, MessageReceivedEvent.class).getMessage();
72 } 72 }
73 73
74 public boolean awaitBoolean(@Nonnull Message question) throws InterruptedException { 74 public boolean awaitBoolean(@Nonnull Message question, boolean keepPrompt) throws InterruptedException {
75 boolean result = ACCEPT_EMOJI.equals(this.ewl.awaitEvent(p -> { 75 boolean result = ACCEPT_EMOJI.equals(this.ewl.awaitEvent(p -> {
76 MessageReactionAddEvent e = (MessageReactionAddEvent) p; 76 MessageReactionAddEvent e = (MessageReactionAddEvent) p;
77 String emote = e.getReactionEmote().getName(); 77 String emote = e.getReactionEmote().getName();
@@ -89,7 +89,8 @@ public class EventUtils {
89 89
90 }, this.timeout, SECONDS, MessageReactionAddEvent.class).getReactionEmote().getName()); 90 }, this.timeout, SECONDS, MessageReactionAddEvent.class).getReactionEmote().getName());
91 91
92 question.delete().queue(); 92 if(!keepPrompt)
93 question.delete().queue();
93 return result; 94 return result;
94 } 95 }
95 96