Skip to content

Commit 5390173

Browse files
authored
Cleanup MessageUtilities (#190)
Removes the exception and functional interface, since their use is entirely avoidable and replaceable with normal Java conventions of return values and Optional. Uses JDA's message jump URL pattern for robustness, instead of matching only normal (non-canary/PTB) Discord links and using manual string manipulation to obtain the IDs.
1 parent eeafd06 commit 5390173

File tree

3 files changed

+30
-72
lines changed

3 files changed

+30
-72
lines changed

src/commander/java/com/mcmoddev/mmdbot/commander/TheCommander.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -546,17 +546,15 @@ public Jdbi getJdbi() {
546546
}
547547

548548
@Nullable
549-
public RestAction<Message> getMessageByLink(final String link) throws MessageUtilities.MessageLinkException {
550-
final AtomicReference<RestAction<Message>> returnAtomic = new AtomicReference<>();
551-
MessageUtilities.decodeMessageLink(link, (guildId, channelId, messageId) -> {
552-
final var guild = getJda().getGuildById(guildId);
553-
if (guild == null) return;
554-
final var channel = guild.getChannelById(MessageChannel.class, channelId);
555-
if (channel != null) {
556-
returnAtomic.set(channel.retrieveMessageById(messageId));
557-
}
558-
});
559-
return returnAtomic.get();
549+
public RestAction<Message> getMessageByLink(final String link) {
550+
return MessageUtilities.decodeMessageLink(link)
551+
.map(info -> {
552+
final var guild = getJda().getGuildById(info.guildId());
553+
if (guild == null) return null;
554+
final var channel = guild.getChannelById(MessageChannel.class, info.channelId());
555+
return channel == null ? null : channel.retrieveMessageById(info.messageId());
556+
})
557+
.orElse(null);
560558
}
561559

562560
public String getGithubToken() {

src/commander/java/com/mcmoddev/mmdbot/commander/eventlistener/ReferencingListener.java

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -56,21 +56,14 @@ public void onMessageReceived(@NonNull MessageReceivedEvent event) {
5656
return;
5757
}
5858

59-
final var matcher = MessageUtilities.MESSAGE_LINK_PATTERN.matcher(msg[0]);
60-
if (matcher.find()) {
61-
try {
62-
final var m = TheCommander.getInstance().getMessageByLink(msg[0]);
63-
if (m != null) {
64-
m.queue(message -> {
65-
event.getChannel().sendMessageEmbeds(reference(message, event.getMember())).queue();
66-
if (msg.length == 1) {
67-
originalMsg.delete().reason("Quote successful").queue();
68-
}
69-
});
59+
final var m = TheCommander.getInstance().getMessageByLink(msg[0]);
60+
if (m != null) {
61+
m.queue(message -> {
62+
event.getChannel().sendMessageEmbeds(reference(message, event.getMember())).queue();
63+
if (msg.length == 1) {
64+
originalMsg.delete().reason("Quote successful").queue();
7065
}
71-
} catch (MessageUtilities.MessageLinkException e) {
72-
// Do nothing
73-
}
66+
});
7467
}
7568
}
7669

src/core/java/com/mcmoddev/mmdbot/core/util/MessageUtilities.java

Lines changed: 14 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,22 @@
2020
*/
2121
package com.mcmoddev.mmdbot.core.util;
2222

23+
import com.jagrosh.jdautilities.commons.utils.SafeIdUtil;
2324
import lombok.NonNull;
2425
import lombok.experimental.UtilityClass;
2526
import net.dv8tion.jda.api.entities.Message;
2627
import net.dv8tion.jda.api.interactions.components.ActionRow;
2728
import net.dv8tion.jda.api.interactions.components.buttons.Button;
2829

29-
import java.io.Serial;
3030
import java.util.ArrayList;
3131
import java.util.List;
32+
import java.util.Optional;
3233
import java.util.regex.Pattern;
3334

3435
@UtilityClass
3536
public 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

Comments
 (0)