Use /serverstatus [host] to lookup data about a server! For example, /serverstatus mc.hypixel.net.
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
// This snippet lets you use /serverstatus [host] to look up a Minecraft: Java Edition's
// player count, ping (latency) and version.
// ▶️ This snippet uses one request every time someone uses /namehistory
// ⚠️ TO USE THIS SNIPPET, YOU MUST FOLLOW THE FOLLOWING INSTRUCTIONS FIRST ⚠️
// 1. In a new tab, go to https://omg.ac/command
// 2. Link the Discord bot you want to install the command to
// 3. Create a new command with the following options
// Command name: serverstatus
// Description: Shows a server's player count, version and latency
// One option, with the following settings:
// Type: String
// Option name: server
// Description: The host/IP address of the server. For example, mc.hypixel.net, or us.mineplex.com:25565
// 4. Click the save button!
// Your command should look like this screenshot once setup: https://i.imgur.com/7xZgPzI.png
// We'll take our commands input, then do some magic to split it into a seperate host and port
// In short, this code just checks to see if the value the user provided contains a :
// If it does, it separates out the host and the port into seperate variables
// If it doesn't, it defaults to port 25565
let hostData = context.params.event.data.options[0].value;
let hostname = hostData.includes(':') ? hostData.split(':')[0] : hostData;
let port = hostData.includes(':') ? hostData.split(':')[1] : 25565;
// Send a response straight away, so Discord doesn't timeout if the API is slow
await lib.discord.interactions['@1.0.1'].responses.ephemeral.create({
token: context.params.event.token,
response_type: 'CHANNEL_MESSAGE_WITH_SOURCE',
content: '',
tts: false,
embeds: [
{
type: 'rich',
title: `⏳ Looking up server status, please wait...`,
description: ``,
color: 0x4b6584,
},
],
});
let serverStatus; // we initialise this here so we can access it outside the try/catch block
// Lookup the server's status
try {
serverStatus = await lib.minecraft.servers['@0.0.1'].java.status({
host: hostname,
port: port,
});
} catch (e) {
// If we can't connect to a server, return an error
return await lib.discord.interactions['@1.0.1'].responses.update({
token: context.params.event.token,
response_type: 'CHANNEL_MESSAGE_WITH_SOURCE',
content: '',
tts: false,
embeds: [
{
type: 'rich',
title: `❌ Failed to lookup server status`,
description: `${hostData} doesn't seem to exist!`,
color: 0xeb3b5a,
},
],
});
}
// Update our initial response to show the server's status
await lib.discord.interactions['@1.0.1'].responses.update({
token: context.params.event.token,
response_type: 'CHANNEL_MESSAGE_WITH_SOURCE',
content: '',
tts: false,
embeds: [
{
type: 'rich',
title: `Server status for ${hostData}`,
description: '',
color: 0x3867d6,
fields: [
{
name: `Players`,
value: `${serverStatus.data.players.online}/${serverStatus.data.players.max}`,
inline: true,
},
{
name: `Version`,
value: `${serverStatus.data.version.name}`,
inline: true,
},
{
name: `Latency`,
value: `${serverStatus.data.roundTripLatency}ms`,
inline: true,
},
],
thumbnail: {
url: `${serverStatus.data.favicon.url}`,
height: 64,
width: 64,
},
},
],
});