Skip to content

Instantly share code, notes, and snippets.

@TheNullicorn
Created August 10, 2020 16:43
Show Gist options
  • Select an option

  • Save TheNullicorn/559fb45910f26bfd80d9e54279dfb230 to your computer and use it in GitHub Desktop.

Select an option

Save TheNullicorn/559fb45910f26bfd80d9e54279dfb230 to your computer and use it in GitHub Desktop.
A simple example of a Discord bot which returns the current Hypixel player count in an embed
{
"hypixel_api_key": "Your Hypixel API key (run '/api new' on Hypixel to get this)",
"bot": {
"token": "Your Discord bot's token",
"name": "Hypixel PlayerCount Bot",
"command_indicator": ".",
"embed_color": "0xf4c75d"
}
}
"use strict";
import Discord from "discord.js";
import fetch from "node-fetch";
import config from "./config.json";
// Login to Discord using the bot token in config.json
const bot = new Discord.Client();
bot.login(config.bot.token);
/**
* Run when the bot receives a message in any channel (public or private)
*/
bot.on("message", async message => {
if (isMessageCommand(message, "playercount")) {
// Fetch the current player count
const currentPlayerCount = await getCurrentPlayerCount();
// Create an embed to store the player count
const embedMessage = new Discord.MessageEmbed()
.setTitle(config.bot.name)
.setColor(config.bot.embed_color)
.addField("Current PlayerCount:", currentPlayerCount, true);
// Send the embed in the same channel that the command was run in
message.channel.send(null, embedMessage);
}
});
/**
* Fetch the current player count from the Hypixel PublicAPI
* @returns {number} The current number of players playing on Hypixel, or zero if it could not be determined
*/
async function getCurrentPlayerCount() {
// Make a request to the Hypixel API and convert the response to a JSON object
const apiResponse = await fetch(`https://api.hypixel.net/gameCounts?key=${config.hypixel_api_key}`)
.then(response => response.json());
if (apiResponse.success == false) {
// The API returned an error
console.error(`The Hypixel API returned an error: ${apiResponse}`);
return 0;
} else if (!apiResponse.playerCount) {
// The "playerCount" value was missing from the API response
console.error(`Unable to find playerCount in API response: ${apiResponse}`);
return 0;
} else {
// All is well, return the player count
return apiResponse.playerCount;
}
}
/**
* @param {Discord.Message} message The Discord message to check
* @param {string} command The name of the command to check for
* @returns {boolean} Whether or not the provided message is the command
*/
function isMessageCommand(message, command) {
return !message.author.bot // Only if the author is not a bot
&& message.channel.guild // Only if the channel is in a server (e.g. not a DM)
&& message.content.toLowerCase().startsWith(config.bot.command_indicator + command.toLowerCase()); // Only if the message starts with the specified command
}
{
"name": "playercount-bot",
"version": "0.0.1",
"description": "A simple example of a Discord bot which returns the current Hypixel player count in an embed",
"main": "index.js",
"type": "module",
"scripts": {
"start": "node --experimental-json-modules index.js"
},
"keywords": [
"hypixel",
"discord",
"bot"
],
"author": "Nullicorn (Brynn)",
"license": "MIT",
"dependencies": {
"node-fetch": "~2.6.0",
"discord.js": "~12.2.0"
}
}
@TheNullicorn
Copy link
Author

Example:

Screen Shot 2020-08-10 at 12 37 52 PM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment