Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 9 additions & 11 deletions src/commander/java/com/mcmoddev/mmdbot/commander/TheCommander.java
Original file line number Diff line number Diff line change
Expand Up @@ -546,17 +546,15 @@ public Jdbi getJdbi() {
}

@Nullable
public RestAction<Message> getMessageByLink(final String link) throws MessageUtilities.MessageLinkException {
final AtomicReference<RestAction<Message>> returnAtomic = new AtomicReference<>();
MessageUtilities.decodeMessageLink(link, (guildId, channelId, messageId) -> {
final var guild = getJda().getGuildById(guildId);
if (guild == null) return;
final var channel = guild.getChannelById(MessageChannel.class, channelId);
if (channel != null) {
returnAtomic.set(channel.retrieveMessageById(messageId));
}
});
return returnAtomic.get();
public RestAction<Message> getMessageByLink(final String link) {
return MessageUtilities.decodeMessageLink(link)
.map(info -> {
final var guild = getJda().getGuildById(info.guildId());
if (guild == null) return null;
final var channel = guild.getChannelById(MessageChannel.class, info.channelId());
return channel == null ? null : channel.retrieveMessageById(info.messageId());
})
.orElse(null);
}

public String getGithubToken() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,14 @@ public void onMessageReceived(@NonNull MessageReceivedEvent event) {
return;
}

final var matcher = MessageUtilities.MESSAGE_LINK_PATTERN.matcher(msg[0]);
if (matcher.find()) {
try {
final var m = TheCommander.getInstance().getMessageByLink(msg[0]);
if (m != null) {
m.queue(message -> {
event.getChannel().sendMessageEmbeds(reference(message, event.getMember())).queue();
if (msg.length == 1) {
originalMsg.delete().reason("Quote successful").queue();
}
});
final var m = TheCommander.getInstance().getMessageByLink(msg[0]);
if (m != null) {
m.queue(message -> {
event.getChannel().sendMessageEmbeds(reference(message, event.getMember())).queue();
if (msg.length == 1) {
originalMsg.delete().reason("Quote successful").queue();
}
} catch (MessageUtilities.MessageLinkException e) {
// Do nothing
}
});
}
}

Expand Down
61 changes: 14 additions & 47 deletions src/core/java/com/mcmoddev/mmdbot/core/util/MessageUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,22 @@
*/
package com.mcmoddev.mmdbot.core.util;

import com.jagrosh.jdautilities.commons.utils.SafeIdUtil;
import lombok.NonNull;
import lombok.experimental.UtilityClass;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.interactions.components.ActionRow;
import net.dv8tion.jda.api.interactions.components.buttons.Button;

import java.io.Serial;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.regex.Pattern;

@UtilityClass
public class MessageUtilities {

// TODO: expand this, see JDA's Message.JUMP_URL_PATTERN
public static final Pattern MESSAGE_LINK_PATTERN = Pattern.compile("https://discord.com/channels/");

/**
Expand All @@ -59,56 +61,21 @@ public static void disableButtons(@NonNull Message message) {
.queue();
}

public static void decodeMessageLink(final String link, MessageInfo consumer)
throws MessageLinkException {
final var matcher = MESSAGE_LINK_PATTERN.matcher(link);
if (matcher.find()) {
try {
var originalWithoutLink = matcher.replaceAll("");
if (originalWithoutLink.indexOf('/') > -1) {
final long guildId = Long
.parseLong(originalWithoutLink.substring(0, originalWithoutLink.indexOf('/')));
originalWithoutLink = originalWithoutLink.substring(originalWithoutLink.indexOf('/') + 1);
if (originalWithoutLink.indexOf('/') > -1) {
final long channelId = Long
.parseLong(originalWithoutLink.substring(0, originalWithoutLink.indexOf('/')));
originalWithoutLink = originalWithoutLink.substring(originalWithoutLink.indexOf('/') + 1);
final long messageId = Long.parseLong(originalWithoutLink);
consumer.accept(guildId, channelId, messageId);
} else {
throw new MessageLinkException("Invalid Link");
}
} else {
throw new MessageLinkException("Invalid Link");
}
} catch (NumberFormatException e) {
throw new MessageLinkException(e);
}
} else {
throw new MessageLinkException("Invalid Link");
}
}

public static class MessageLinkException extends Exception {
public static Optional<MessageLinkInformation> decodeMessageLink(final String link) {
final var matcher = Message.JUMP_URL_PATTERN.matcher(link);
if (!matcher.find()) return Optional.empty();

@Serial
private static final long serialVersionUID = -2805786147679905681L;

public MessageLinkException(Throwable e) {
super(e);
}
try {
final long guildId = SafeIdUtil.safeConvert(matcher.group("guild"));
final long channelId = SafeIdUtil.safeConvert(matcher.group("channel"));
final long messageId = SafeIdUtil.safeConvert(matcher.group("message"));

public MessageLinkException(String message) {
super(message);
return Optional.of(new MessageLinkInformation(guildId, channelId, messageId));
} catch (NumberFormatException e) {
return Optional.empty();
}

}

@FunctionalInterface
public interface MessageInfo {

void accept(final long guildId, final long channelId, final long messageId);

public record MessageLinkInformation(long guildId, long channelId, long messageId) {
}

}