Skip to content

Instantly share code, notes, and snippets.

@RascalTwo
Last active October 2, 2021 16:34
Show Gist options
  • Save RascalTwo/5af068c248aa9e4936a8219f8ffcf020 to your computer and use it in GitHub Desktop.
Save RascalTwo/5af068c248aa9e4936a8219f8ffcf020 to your computer and use it in GitHub Desktop.

Discord Minecraft Server Count Updator

bot status with 11 people online

Update the current playing text of a Discord bot with the number of players on a Minecraft Server using the Minecraft Server Status API and discord.js.

Variable Usage
DISCORD_BOT_TOKEN Token for the Discord bot to run
MCSERVER_ADDRESS Address of the Minecraft server to poll
const fetch = require('node-fetch');
const Discord = require('discord.js');
const client = new Discord.Client();
client.on('ready', () =>
console.log(`Logged in as ${client.user.tag}!`)
);
const URL = `https://api.mcsrvstat.us/2/${process.env.MCSERVER_ADDRESS}`;
const updateStatus = () => {
if (!client.readyTimestamp) return setTimeout(updateStatus, 1000);
console.log(URL)
return fetch(URL)
.then(r => r.json())
.then(data => {
if ('list' in data.players) return data.players.list;
return new Array(data.players.online || 0).fill(undefined);
})
.then(onlinePlayers => {
const name = `Online: ${onlinePlayers.length}`;
const currentGame = client.user.presence.game;
if (currentGame && currentGame.name !== name) return;
return client.user.setActivity(name, { type: 'LISTENING' })
});
}
setInterval(updateStatus, 60 * 5 * 1000);
updateStatus();
client.login(process.env.DISCORD_BOT_TOKEN);
{
"name": "discord_mcbot",
"version": "1.0.0",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
"async-limiter": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/async-limiter/-/async-limiter-1.0.1.tgz",
"integrity": "sha512-csOlWGAcRFJaI6m+F2WKdnMKr4HhdhFVBk0H/QbJFMCr+uO2kwohwXQPxw/9OCxp05r5ghVBFSyioixx3gfkNQ=="
},
"discord.js": {
"version": "11.5.1",
"resolved": "https://registry.npmjs.org/discord.js/-/discord.js-11.5.1.tgz",
"integrity": "sha512-tGhV5xaZXE3Z+4uXJb3hYM6gQ1NmnSxp9PClcsSAYFVRzH6AJH74040mO3afPDMWEAlj8XsoPXXTJHTxesqcGw==",
"requires": {
"long": "^4.0.0",
"prism-media": "^0.0.3",
"snekfetch": "^3.6.4",
"tweetnacl": "^1.0.0",
"ws": "^6.0.0"
}
},
"long": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/long/-/long-4.0.0.tgz",
"integrity": "sha512-XsP+KhQif4bjX1kbuSiySJFNAehNxgLb6hPRGJ9QsUr8ajHkuXGdrHmFUTUUXhDwVX2R5bY4JNZEwbUiMhV+MA=="
},
"node-fetch": {
"version": "2.6.0",
"resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.0.tgz",
"integrity": "sha512-8dG4H5ujfvFiqDmVu9fQ5bOHUC15JMjMY/Zumv26oOvvVJjM67KF8koCWIabKQ1GJIa9r2mMZscBq/TbdOcmNA=="
},
"prism-media": {
"version": "0.0.3",
"resolved": "https://registry.npmjs.org/prism-media/-/prism-media-0.0.3.tgz",
"integrity": "sha512-c9KkNifSMU/iXT8FFTaBwBMr+rdVcN+H/uNv1o+CuFeTThNZNTOrQ+RgXA1yL/DeLk098duAeRPP3QNPNbhxYQ=="
},
"snekfetch": {
"version": "3.6.4",
"resolved": "https://registry.npmjs.org/snekfetch/-/snekfetch-3.6.4.tgz",
"integrity": "sha512-NjxjITIj04Ffqid5lqr7XdgwM7X61c/Dns073Ly170bPQHLm6jkmelye/eglS++1nfTWktpP6Y2bFXjdPlQqdw=="
},
"tweetnacl": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/tweetnacl/-/tweetnacl-1.0.1.tgz",
"integrity": "sha512-kcoMoKTPYnoeS50tzoqjPY3Uv9axeuuFAZY9M/9zFnhoVvRfxz9K29IMPD7jGmt2c8SW7i3gT9WqDl2+nV7p4A=="
},
"ws": {
"version": "6.2.1",
"resolved": "https://registry.npmjs.org/ws/-/ws-6.2.1.tgz",
"integrity": "sha512-GIyAXC2cB7LjvpgMt9EKS2ldqr0MTrORaleiOno6TweZ6r3TKtoFQWay/2PceJ3RuBasOHzXNn5Lrw1X0bEjqA==",
"requires": {
"async-limiter": "~1.0.0"
}
}
}
}
{
"name": "discord_mcbot",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node index.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"discord.js": "^11.5.1",
"node-fetch": "^2.6.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment