From d1027dd5295955c0b6fa5001da028e89baf36d8e Mon Sep 17 00:00:00 2001 From: chris Date: Thu, 1 Aug 2024 09:45:27 +0200 Subject: [PATCH] Update `/geyser connectiontest` command usage (#299) * Update connectiontest command usage, adapt /ping command to work the same way * Use nullable Integer instead of re-parsing the ip:port stuff * add min/max value & max length check to port remove timestamp, changed title * Address review * Fix: max length Co-authored-by: Olivia --------- Co-authored-by: Olivia --- .../discordbot/commands/PingCommand.java | 50 +++++++++---------- .../resources/tags/errors/unabletoconnect.tag | 2 +- .../resources/tags/util/connectiontest.tag | 4 +- 3 files changed, 26 insertions(+), 30 deletions(-) diff --git a/src/main/java/org/geysermc/discordbot/commands/PingCommand.java b/src/main/java/org/geysermc/discordbot/commands/PingCommand.java index 2bff5b56..781bbe8b 100644 --- a/src/main/java/org/geysermc/discordbot/commands/PingCommand.java +++ b/src/main/java/org/geysermc/discordbot/commands/PingCommand.java @@ -46,10 +46,8 @@ import java.io.IOException; import java.net.InetSocketAddress; -import java.time.Instant; import java.util.ArrayList; import java.util.Arrays; -import java.util.Collections; import java.util.List; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; @@ -60,12 +58,15 @@ public class PingCommand extends SlashCommand { public PingCommand() { this.name = "ping"; this.aliases = new String[] { "status" }; - this.arguments = ""; + this.arguments = " [port]"; this.help = "Ping a server to check if its accessible"; this.guildOnly = false; - this.options = Collections.singletonList( - new OptionData(OptionType.STRING, "server", "The IP Address of the server you want to ping", true) + this.options = List.of( + new OptionData(OptionType.STRING, "ip", "The IP Address of the server you want to ping", true), + new OptionData(OptionType.INTEGER, "port", "The port of the server you want to ping", false) + .setMinValue(1) + .setMaxValue(65535) ); } @@ -74,9 +75,10 @@ protected void execute(SlashCommandEvent event) { // Defer to wait for us to load a response and allows for files to be uploaded InteractionHook interactionHook = event.deferReply().complete(); - String ip = event.getOption("server").getAsString(); + String ip = event.getOption("ip").getAsString(); + Integer port = event.getOption("port") != null ? event.getOption("port").getAsInt() : null; - interactionHook.editOriginalEmbeds(handle(ip)).queue(); + interactionHook.editOriginalEmbeds(handle(ip, port)).queue(); } @Override @@ -89,10 +91,13 @@ protected void execute(CommandEvent event) { return; } - event.getMessage().replyEmbeds(handle(args.get(0))).queue(); + String ip = args.get(0); + Integer port = args.size() > 1 ? Integer.parseInt(args.get(1)) : null; + + event.getMessage().replyEmbeds(handle(ip, port)).queue(); } - private MessageEmbed handle(String ip) { + private MessageEmbed handle(String ip, Integer port) { // Check we were given a valid IP/domain if (!ip.matches("[\\w.\\-:]+")) { return MessageHelper.errorResponse(null, "IP invalid", "The given IP appears to be invalid and won't be queried. If you believe this is incorrect please contact an admin."); @@ -108,24 +113,16 @@ private MessageEmbed handle(String ip) { ip = ip.replaceAll("https?://", "").split("/")[0]; } - String[] ipParts = ip.split(":"); - - String hostname = ipParts[0]; - - if (NetworkUtils.isInternalIP(hostname)) { + if (NetworkUtils.isInternalIP(ip)) { return MessageHelper.errorResponse(null, "IP invalid", "The given IP appears to be an internal address and won't be queried."); } int jePort = 25565; int bePort = 19132; - if (ipParts.length > 1) { - try { - jePort = Integer.parseInt(ipParts[1]); - bePort = jePort; - } catch (NumberFormatException ignored) { - return MessageHelper.errorResponse(null, "Invalid port", "The port you specified is not a valid number."); - } + if (port != null) { + jePort = port; + bePort = jePort; } if (jePort < 1 || jePort > 65535) { @@ -138,7 +135,7 @@ private MessageEmbed handle(String ip) { try { MCPingOptions options = MCPingOptions.builder() - .hostname(hostname) + .hostname(ip) .port(jePort) .timeout(TIMEOUT) .build(); @@ -159,7 +156,7 @@ private MessageEmbed handle(String ip) { client.bind().join(); - InetSocketAddress addressToPing = new InetSocketAddress(hostname, bePort); + InetSocketAddress addressToPing = new InetSocketAddress(ip, bePort); BedrockPong pong = client.ping(addressToPing, TIMEOUT, TimeUnit.MILLISECONDS).get(); bedrockInfo = "**MOTD:** \n```\n" + BotHelpers.trim(MCPingUtil.stripColors(pong.getMotd()), 100) + (pong.getSubMotd() != null ? "\n" + BotHelpers.trim(MCPingUtil.stripColors(pong.getSubMotd()), 100) : "") + "\n```\n" + @@ -174,10 +171,9 @@ private MessageEmbed handle(String ip) { } return new EmbedBuilder() - .setTitle("Pinging server: " + ip) - .addField("Java", javaInfo, false) - .addField("Bedrock", bedrockInfo, false) - .setTimestamp(Instant.now()) + .setTitle("Pinging server " + ip) + .addField("Java (" + jePort + ")", javaInfo, false) + .addField("Bedrock (" + bePort + ")", bedrockInfo, false) .setColor(success ? BotColors.SUCCESS.getColor() : BotColors.FAILURE.getColor()) .build(); } diff --git a/src/main/resources/tags/errors/unabletoconnect.tag b/src/main/resources/tags/errors/unabletoconnect.tag index a1fbc809..d7c8d665 100644 --- a/src/main/resources/tags/errors/unabletoconnect.tag +++ b/src/main/resources/tags/errors/unabletoconnect.tag @@ -5,6 +5,6 @@ issues: Unable to connect to world This means that the Bedrock client cannot find the server specified. If you have not already, follow our [setup instructions](https://wiki.geysermc.org/geyser/setup/). Then, check the server console for any errors. -To verify Bedrock clients can connect, you can try running the `geyser connectiontest :` command with your server IP and Geyser port. +To verify Bedrock clients can connect, you can try running the `geyser connectiontest ` command with your server IP and Geyser port. Additionally, there are various fixes for this on our wiki, please see [here](https://wiki.geysermc.org/geyser/fixing-unable-to-connect-to-world/). diff --git a/src/main/resources/tags/util/connectiontest.tag b/src/main/resources/tags/util/connectiontest.tag index 3a177e62..1f804bb6 100644 --- a/src/main/resources/tags/util/connectiontest.tag +++ b/src/main/resources/tags/util/connectiontest.tag @@ -5,6 +5,6 @@ aliases: test, serverstatus To check whether Bedrock players can connect or if port forwarding for Geyser is working, run the following command in your Minecraft server console: -`geyser connectiontest :` +`geyser connectiontest ` -For example, if your server is running on the IP 12.34.56.78 and port 19132, you would run `geyser connectiontest 12.34.56.78:19132`. +For example, if your server is running on the IP 12.34.56.78 and port 19132, you would run `geyser connectiontest 12.34.56.78 19132`.