2020 */
2121package com .mcmoddev .mmdbot .core .util ;
2222
23+ import com .jagrosh .jdautilities .commons .utils .SafeIdUtil ;
2324import lombok .NonNull ;
2425import lombok .experimental .UtilityClass ;
2526import net .dv8tion .jda .api .entities .Message ;
2627import net .dv8tion .jda .api .interactions .components .ActionRow ;
2728import net .dv8tion .jda .api .interactions .components .buttons .Button ;
2829
29- import java .io .Serial ;
3030import java .util .ArrayList ;
3131import java .util .List ;
32+ import java .util .Optional ;
3233import java .util .regex .Pattern ;
3334
3435@ UtilityClass
3536public class MessageUtilities {
3637
38+ // TODO: expand this, see JDA's Message.JUMP_URL_PATTERN
3739 public static final Pattern MESSAGE_LINK_PATTERN = Pattern .compile ("https://discord.com/channels/" );
3840
3941 /**
@@ -59,56 +61,21 @@ public static void disableButtons(@NonNull Message message) {
5961 .queue ();
6062 }
6163
62- public static void decodeMessageLink (final String link , MessageInfo consumer )
63- throws MessageLinkException {
64- final var matcher = MESSAGE_LINK_PATTERN .matcher (link );
65- if (matcher .find ()) {
66- try {
67- var originalWithoutLink = matcher .replaceAll ("" );
68- if (originalWithoutLink .indexOf ('/' ) > -1 ) {
69- final long guildId = Long
70- .parseLong (originalWithoutLink .substring (0 , originalWithoutLink .indexOf ('/' )));
71- originalWithoutLink = originalWithoutLink .substring (originalWithoutLink .indexOf ('/' ) + 1 );
72- if (originalWithoutLink .indexOf ('/' ) > -1 ) {
73- final long channelId = Long
74- .parseLong (originalWithoutLink .substring (0 , originalWithoutLink .indexOf ('/' )));
75- originalWithoutLink = originalWithoutLink .substring (originalWithoutLink .indexOf ('/' ) + 1 );
76- final long messageId = Long .parseLong (originalWithoutLink );
77- consumer .accept (guildId , channelId , messageId );
78- } else {
79- throw new MessageLinkException ("Invalid Link" );
80- }
81- } else {
82- throw new MessageLinkException ("Invalid Link" );
83- }
84- } catch (NumberFormatException e ) {
85- throw new MessageLinkException (e );
86- }
87- } else {
88- throw new MessageLinkException ("Invalid Link" );
89- }
90- }
91-
92- public static class MessageLinkException extends Exception {
64+ public static Optional <MessageLinkInformation > decodeMessageLink (final String link ) {
65+ final var matcher = Message .JUMP_URL_PATTERN .matcher (link );
66+ if (!matcher .find ()) return Optional .empty ();
9367
94- @ Serial
95- private static final long serialVersionUID = -2805786147679905681L ;
96-
97- public MessageLinkException (Throwable e ) {
98- super (e );
99- }
68+ try {
69+ final long guildId = SafeIdUtil .safeConvert (matcher .group ("guild" ));
70+ final long channelId = SafeIdUtil .safeConvert (matcher .group ("channel" ));
71+ final long messageId = SafeIdUtil .safeConvert (matcher .group ("message" ));
10072
101- public MessageLinkException (String message ) {
102- super (message );
73+ return Optional .of (new MessageLinkInformation (guildId , channelId , messageId ));
74+ } catch (NumberFormatException e ) {
75+ return Optional .empty ();
10376 }
104-
10577 }
10678
107- @ FunctionalInterface
108- public interface MessageInfo {
109-
110- void accept (final long guildId , final long channelId , final long messageId );
111-
79+ public record MessageLinkInformation (long guildId , long channelId , long messageId ) {
11280 }
113-
11481}
0 commit comments