This is a Node.js script for a Discord bot. Based on MineStat, the bot checks the status of user-specified Minecraft servers and returns the number of players online.
Installation
This requires Node.js to be installed on your system, along with primary dependencies discord.js and minestat.js.
Setting up for use with Node.js
Install Node.js and npm on your system. (You can use nvm or the Node.js binary.)
Clone this repository and place it in a folder. (Use git clone https://github.com/amalbansode/Minecraft-Discord-Bot)
In terminal, navigate to this folder.
$ cd location/of/your/Minecraft-Discord-Bot
Install the latest discord.js using npm.
$ npm i -S discord.js # You can remove the -S because npm now automatically saves it to package.json
Navigate to the ‘Bot’ section. Reveal your bot’s token, and copy it.
Open auth.json, which is contained in the same folder as bot.js.
Paste your token within the double quotes corresponding to the "token" key, and save auth.json.
Now, with Terminal open in the same directory as before, run
$ node bot.js
The output should say the following. If so, you have setup the bot correctly.
Logged in as <your bot’s discord tag>!
Ready...
Exit the application using a keyboard escape (typically control/command + C).
Adding the bot to a server
On the Dicord Chat Client, click the '+' icon on the left side and create a new server for testing the bot. Skip this step if you manage a pre-existing server you want the bot to be added to.
On the Discord Develper Portal for your bot, navigate to the section titled ‘OAuth2’. Scroll down to view the OAuth2 URL Generator.
Check the following boxes:
In Scopes, bot
In Bot Permissions, Send Messages and Read Message History
A URL must have been generated above. Follow this URL in a new browser tab.
Specify the server you would like to add the bot to (this could be the one you created in step 1, or a pre-existing server).
Verify the permissions stated, and click ‘Authorize’.
The bot should now be a member of your specified server.
Start the bot in Terminal again
$ node bot.js
In the Discord chat window for this server, enter ‘!ip’ and check for output in your Discord chat.
Features
Player count display
The number of players on the server is displayed as the bot’s activity in the ‘Users‘ list on the right side.
Printing the list of server IPs in chat
The IP details of servers can be displayed in Discord chat by typing !ip into Discord chat. The bot will reply to the user invoking the command.
Configuration
Changing the servers to be checked
Stop the bot if it is running. Open bot.js in a text-editing program.
Navigate to line 33, which contains the dictionary type variable servIP.
By default, the dictionary contains the IP Address for the MInecraft Hypixel server.
Modify this dictionary to follow the template, keeping the title(s) and IP Address(es) within single quotation (') marks, separated by a colon (:) mark. The order of servers in this dictionary list will be preserved in the bot’s output.
Changing the frequency of checking servers' status
By default, the bot checks the status of servers every four minutes.
To modify this, change the variable refreshEvery on line 29 to the number of minutes between every refresh. Use a floating point numeral if required.
Note: The bot being asynchronous, however, will require at least the number of servers times 2.5 seconds to fetch the statuses of all servers at once. This time interval can be reduced, but it is suggested that this remain as it is to prevent any malfunctioning.
FAQs
There's no port specified for my Minecraft server
The standard port for Minecraft servers is typically 25565.
Does the bot run forever?
No, the bot goes offline when you close Terminal or end the process on your system. You could host the bot on AWS or Heroku, following the same steps as above, instead. The bot can be kept online using the forever module. Check out this guide on the usage of AWS.
Contributors
Contributions and improvements to the project are welcome! The following users helped by reviewing code and fixing my silly errors:
请发表评论