11package net .discordjug .javabot .systems .user_commands .format_code ;
22
33import net .dv8tion .jda .api .entities .Message ;
4- import net .dv8tion .jda .api .entities .channel .middleman .MessageChannel ;
54
65import java .util .ArrayList ;
76import java .util .List ;
@@ -18,43 +17,53 @@ private LinkedMessages(){}
1817
1918 /**
2019 * Resolves the block ending at {@code triggerMessage} (walking back {@code total} messages) and
21- * passes the bot's own messages among them to {@code onResolved}.
20+ * passes the bot's own messages to {@code onResolved}, or runs {@code onError} if it can't be
21+ * safely resolved.
2222 *
23- * @param channel the channel the block is in
2423 * @param triggerMessage the last message of the block (carries the component)
2524 * @param total the number of messages in the block
26- * @param onResolved receives the bot's messages that make up the block
25+ * @param onResolved receives the bot's messages that make up the block
26+ * @param onError runs if the block can't be safely resolved
2727 */
28- static void resolve ( MessageChannel channel , Message triggerMessage , int total , Consumer <List <Message >> onResolved ) {
28+ static void resolveBefore ( Message triggerMessage , int total , Consumer <List <Message >> onResolved , Runnable onError ) {
2929 if (total <= 1 ) {
30- onResolved . accept (List .of (triggerMessage ));
30+ verify (List .of (triggerMessage ), total , onResolved , onError );
3131 return ;
3232 }
33- channel .getHistoryBefore (triggerMessage .getIdLong (), total - 1 ).queue (history -> {
33+ triggerMessage . getChannel () .getHistoryBefore (triggerMessage .getIdLong (), total - 1 ).queue (history -> {
3434 List <Message > block = new ArrayList <>(history .getRetrievedHistory ());
3535 block .add (triggerMessage );
36- onResolved . accept ( onlyOwn ( channel , block ) );
36+ verify ( block , total , onResolved , onError );
3737 });
3838 }
3939
4040 /**
4141 * Resolves the block of {@code total} messages sent after {@code anchorMessage} and passes the
42- * bot's own messages among them to {@code onResolved}.
42+ * bot's own messages to {@code onResolved}, or runs {@code onError} if it can't be safely resolved .
4343 *
44- * @param channel the channel the block is in
4544 * @param anchorMessage the message just before the block (carries the component)
4645 * @param total the number of messages in the block
47- * @param onResolved receives the bot's messages that make up the block
46+ * @param onResolved receives the bot's messages that make up the block
47+ * @param onError runs if the block can't be safely resolved
4848 */
49- static void resolveForward (MessageChannel channel , Message anchorMessage , int total , Consumer <List <Message >> onResolved ) {
50- channel .getHistoryAfter (anchorMessage .getIdLong (), total ).queue (history ->
51- onResolved .accept (onlyOwn (channel , history .getRetrievedHistory ())));
49+ static void resolveAfter (Message anchorMessage , int total , Consumer <List <Message >> onResolved , Runnable onError ) {
50+ anchorMessage .getChannel ().getHistoryAfter (anchorMessage .getIdLong (), total ).queue (history ->
51+ verify (history .getRetrievedHistory (), total , onResolved , onError ));
52+ }
53+
54+ private static void verify (List <Message > messages , int total , Consumer <List <Message >> onResolved , Runnable onError ) {
55+ List <Message > own = onlyOwn (messages );
56+ boolean allCodeBlocks = own .stream ().allMatch (message -> message .getContentRaw ().startsWith ("```" ));
57+ if (own .size () != total || !allCodeBlocks ) {
58+ onError .run ();
59+ return ;
60+ }
61+ onResolved .accept (own );
5262 }
5363
54- private static List <Message > onlyOwn (MessageChannel channel , List <Message > messages ) {
55- long selfId = channel .getJDA ().getSelfUser ().getIdLong ();
64+ private static List <Message > onlyOwn (List <Message > messages ) {
5665 return messages .stream ()
57- .filter (message -> message .getAuthor ().getIdLong () == selfId )
66+ .filter (message -> message .getAuthor ().getIdLong () == message . getJDA (). getSelfUser (). getIdLong () )
5867 .toList ();
5968 }
6069}
0 commit comments