Skip to content

Instantly share code, notes, and snippets.

@5w14
Created January 21, 2026 06:26
Show Gist options
  • Select an option

  • Save 5w14/606d96823f17b0d449e38c3e3996ce7a to your computer and use it in GitHub Desktop.

Select an option

Save 5w14/606d96823f17b0d449e38c3e3996ce7a to your computer and use it in GitHub Desktop.
Sends updates from a minecraft server to a name of a discord channel. View discord docs here: https://discord.com/developers/docs/resources/channel#modify-channel
import {
ping,
type NewPingResult,
type OldPingResult,
} from "minecraft-protocol";
const MC_HOST = process.env.MINECRAFT_HOST ?? "mc.hypixel.net";
const MC_PORT = parseInt(process.env.MINECRAFT_PORT ?? "25565", 10);
const DS_CHANNEL = process.env.DISCORD_CHANNEL!;
const DS_TOKEN = process.env.DISCORD_TOKEN!;
const INTERVAL = parseInt(process.env.UPDATE_INTERVAL ?? "1", 10) * 1000;
function doPing(): Promise<{
success: boolean;
data: OldPingResult | NewPingResult | null;
}> {
return new Promise((cb, err) => {
ping({ host: MC_HOST, port: MC_PORT }, (error, result) => {
if (error) {
return cb({ success: false, data: null });
}
return cb({ success: true, data: result });
});
});
}
async function pingServer() {
const data = await doPing();
const name = data.success
? `🟢 online: ${(data?.data as any)?.players?.online}`
: "🔴 offline";
await updateDiscordChannel({ name });
}
async function updateDiscordChannel(updateObj: any) {
const resp = await fetch(
`https://discord.com/api/v10/channels/${DS_CHANNEL}`,
{
headers: {
Authorization: `Bot ${DS_TOKEN}`,
"Content-Type": "application/json",
},
method: "PATCH",
body: JSON.stringify(updateObj),
},
);
}
setInterval(pingServer, INTERVAL);
pingServer();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment