Files
chomens-bot-java/src/main/java/me/chayapak1/chomens_bot/commands/NetMessageCommand.java
ChomeNS 56aae42a94 feat: support URL in discord, netmsg, and console
feat: support copy to clipboard on NetMessageCommand messages
2025-03-30 17:18:35 +07:00

87 lines
3.5 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package me.chayapak1.chomens_bot.commands;
import me.chayapak1.chomens_bot.Bot;
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;
public class NetMessageCommand extends Command {
public NetMessageCommand () {
super(
"netmsg",
"Broadcasts a message to all of the servers that the bot is connected",
new String[] { "<message>" },
new String[] { "networkmessage", "irc" },
TrustLevel.PUBLIC,
false
);
}
@Override
public Component execute(CommandContext context) throws CommandException {
final Bot bot = context.bot;
final List<Bot> bots = bot.bots;
final String originServerName = bot.getServerString();
final String originServerAddress = bot.getServerString(false);
Component serverNameComponent = Component
.text(originServerName)
.color(NamedTextColor.GRAY);
if (!bot.options.hidden) serverNameComponent = serverNameComponent
.clickEvent(ClickEvent.copyToClipboard(originServerAddress))
.hoverEvent(
HoverEvent.showText(
Component.empty()
.append(Component.text(originServerAddress).color(NamedTextColor.GRAY))
.append(Component.newline())
.append(Component.text("Click here to copy the server host and port to your clipboard").color(NamedTextColor.GREEN))
)
);
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,
Component.space(),
context.sender.displayName == null ?
Component.text(context.sender.profile.getName()).color(NamedTextColor.GRAY) :
context.sender.displayName.color(NamedTextColor.GRAY),
Component.space(),
Component.empty()
.append(stylizedMessage)
.color(NamedTextColor.GRAY)
).color(NamedTextColor.DARK_GRAY);
for (Bot eachBot : bots) {
if (!eachBot.loggedIn) continue;
eachBot.chat.tellraw(component);
}
return null;
}
}