Skip to content

Commit

Permalink
Update /geyser connectiontest command usage (#299)
Browse files Browse the repository at this point in the history
* 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 <chew@chew.pw>

---------

Co-authored-by: Olivia <chew@chew.pw>
  • Loading branch information
onebeastchris and Chew authored Aug 1, 2024
1 parent 4166286 commit d1027dd
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 30 deletions.
50 changes: 23 additions & 27 deletions src/main/java/org/geysermc/discordbot/commands/PingCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -60,12 +58,15 @@ public class PingCommand extends SlashCommand {
public PingCommand() {
this.name = "ping";
this.aliases = new String[] { "status" };
this.arguments = "<server>";
this.arguments = "<ip> [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)
);
}

Expand All @@ -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
Expand All @@ -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.");
Expand All @@ -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) {
Expand All @@ -138,7 +135,7 @@ private MessageEmbed handle(String ip) {

try {
MCPingOptions options = MCPingOptions.builder()
.hostname(hostname)
.hostname(ip)
.port(jePort)
.timeout(TIMEOUT)
.build();
Expand All @@ -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" +
Expand All @@ -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();
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/tags/errors/unabletoconnect.tag
Original file line number Diff line number Diff line change
Expand Up @@ -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 <ip>:<port>` command with your server IP and Geyser port.
To verify Bedrock clients can connect, you can try running the `geyser connectiontest <ip> <port>` 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/).
4 changes: 2 additions & 2 deletions src/main/resources/tags/util/connectiontest.tag
Original file line number Diff line number Diff line change
Expand Up @@ -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 <ip>:<port>`
`geyser connectiontest <ip> <port>`

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`.

0 comments on commit d1027dd

Please sign in to comment.