feat: support URL in discord, netmsg, and console

feat: support copy to clipboard on NetMessageCommand messages
This commit is contained in:
ChomeNS
2025-03-30 17:18:35 +07:00
parent c70e8b5779
commit 56aae42a94
5 changed files with 72 additions and 5 deletions

View File

@@ -5,10 +5,12 @@ import me.chayapak1.chomens_bot.command.Command;
import me.chayapak1.chomens_bot.command.CommandContext;
import me.chayapak1.chomens_bot.command.CommandException;
import me.chayapak1.chomens_bot.command.TrustLevel;
import me.chayapak1.chomens_bot.util.ComponentUtilities;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.event.HoverEvent;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import java.util.List;
@@ -47,6 +49,20 @@ public class NetMessageCommand extends Command {
)
);
final String rawMessage = context.getString(true, true);
final Component stylizedMessage = LegacyComponentSerializer.legacyAmpersand()
.deserialize(rawMessage)
.replaceText(ComponentUtilities.URL_REPLACEMENT_CONFIG)
.clickEvent(ClickEvent.copyToClipboard(rawMessage))
.hoverEvent(
HoverEvent.showText(
Component
.text("Click here to copy the message to your clipboard")
.color(NamedTextColor.GREEN)
)
);
final Component component = Component.translatable(
"[%s]%s%s%s %s",
serverNameComponent,
@@ -55,7 +71,9 @@ public class NetMessageCommand extends Command {
Component.text(context.sender.profile.getName()).color(NamedTextColor.GRAY) :
context.sender.displayName.color(NamedTextColor.GRAY),
Component.space(),
Component.text(context.getString(true, true)).color(NamedTextColor.GRAY)
Component.empty()
.append(stylizedMessage)
.color(NamedTextColor.GRAY)
).color(NamedTextColor.DARK_GRAY);
for (Bot eachBot : bots) {

View File

@@ -5,6 +5,7 @@ import me.chayapak1.chomens_bot.Configuration;
import me.chayapak1.chomens_bot.Main;
import me.chayapak1.chomens_bot.command.Command;
import me.chayapak1.chomens_bot.command.ConsoleCommandContext;
import me.chayapak1.chomens_bot.util.ComponentUtilities;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.ComponentBuilder;
import net.kyori.adventure.text.SelectorComponent;
@@ -107,7 +108,9 @@ public class ConsolePlugin implements Completer {
if (!bot.loggedIn) continue;
final Component stylizedMessage = LEGACY.deserialize(line);
final Component stylizedMessage = LEGACY
.deserialize(line)
.replaceText(ComponentUtilities.URL_REPLACEMENT_CONFIG);
final Component rendered = RENDERER.render(
format,

View File

@@ -352,7 +352,8 @@ public class DiscordPlugin extends ListenerAdapter {
Component actualMessage = LegacyComponentSerializer
.legacyAmpersand()
.deserialize(replacedMessageContent);
.deserialize(replacedMessageContent)
.replaceText(ComponentUtilities.URL_REPLACEMENT_CONFIG);
if (!extraComponents.isEmpty()) {
if (!replacedMessageContent.isBlank()) actualMessage = actualMessage.append(Component.space());

View File

@@ -4,6 +4,8 @@ import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import net.kyori.adventure.text.*;
import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.event.HoverEvent;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.Style;
import net.kyori.adventure.text.format.TextColor;
@@ -17,9 +19,52 @@ import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
// totallynotskidded™ from chipmunkbot and added colors (ignore the ohio code please,..,.)
public class ComponentUtilities {
// https://github.com/kaboomserver/extras/blob/master/src/main/java/pw/kaboom/extras/modules/player/PlayerChat.java#L49C9-L81C26
// with the hover event added
public static final TextReplacementConfig URL_REPLACEMENT_CONFIG =
TextReplacementConfig
.builder()
.match(Pattern
.compile("((https?://(ww(w|\\d)\\.)?|ww(w|\\d))[-a-zA-Z0-9@:%._+~#=]{1,256}"
+ "\\.[a-zA-Z0-9]{2,6}\\b([-a-zA-Z0-9@:%_+.~#?&/=]*))"))
.replacement((b, c) -> {
if (c == null) {
return null;
}
if (b.groupCount() < 1) {
return null;
}
final String content = b.group(1);
final String url;
/*
Minecraft doesn't accept "www.google.com" as a URL
in click events
*/
if (content.contains("://")) {
url = content;
} else {
url = "https://" + content;
}
return Component.text(content, NamedTextColor.BLUE)
.decorate(TextDecoration.UNDERLINED)
.clickEvent(ClickEvent.openUrl(url))
.hoverEvent(
HoverEvent.showText(
Component
.text("Click here to open the URL")
.color(NamedTextColor.BLUE)
)
);
})
.build();
// component parsing
// rewritten from chipmunkbot, a lot of stuff has changed, and also ANSI and section signs support, etc...
public static final Map<String, String> LANGUAGE = loadJsonStringMap("language.json");
public static final Map<String, String> VOICE_CHAT_LANGUAGE = loadJsonStringMap("voiceChatLanguage.json");
public static final Map<String, String> KEYBINDINGS = loadJsonStringMap("keybinds.json");