Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How to make a Discord bot that pings your Minecraft server🤔

Posted on Jun 28 Have you ever had your very own Minecraft Server and caught yourself thinking, "Wouldn't it be awesome if I could showcase useful information about my server, or track its latency right inside Discord?" Well, you're in luck! This tutorial will walk you through the entire process, starting from scratch, so you can achieve all of that and more.We'll be using discord.js, and the MinecraftStatusPinger library, which is a modern, small, performant, zero dependency typescript library written by me that doesn't mess with the server's response.This is pretty straightforward. Run npm install discord.js, import discord.js, declare the client, and it's Intents, log in your bot and log a message once the client is ready.In this step, we'll make a "ping" slash command. We'll start by declaring REST, which is used for updating slash commands. We set it's token to our token environment variable. We add two options to the command - hostname, and port. We send and register the slash command to Discord, providing your application IDSubscribe to the InteractionCreate event. When one is received, we check whether it is a slash command interaction. Is it? Bingo. Once that's done, we check it's name, and if it's correct, we call the getMcStatus function, which we create in the next stepAs mentioned before, we'll be using the MinecraftStatusPinger library.Run this command to install it from NPM: npm install minecraftstatuspinger. This ensures that it is on your computer, and you can import and use it.Then, import it like so:The setup is quite easier than discord.js! Just install it, import it, and get on your way. No additional setup, no creating a pinger object, it just works out of the box.We finally create the getMcStatus function. We need the two options - IP and port. If the port is null, it will automatically switch to 25565 by default. Once we get those, we can call .lookup() with both the options. This makes the library send a ping request directly to the Minecraft server without contacting any intermediary APIs.now inside status, there is an object with these three children - status, statusRaw, latency.status is the object which contains the playerlist, version, favicon, and MOTD! We will use all of these in the next step. statusRaw is just like status, but it is an unparsed string instead of an object.latency is a number, which represents the latency to the server. It is achieved by sending an arbitrary packet to the server, and waiting for a response, so it's way more accurate than a normal ping.A minecraft server returns it's favicon as a Data URL blob. We need to remove the prefix, done with the replace function and regex, which replaces all filetypes (like data:image/png;base64, or data:image/jpeg;base64,). Then, it turns it into a buffer, which is that discord.js can use for an image attachment. We set it's name to icon.png.We make a new embed, which we give a title of the IP and the MOTD. The MOTDs vary across versions, for examply hypixel uses result.status.description, modern servers should use result.status.description.text. We add a player count, we add a version field, which shows what version you can join the server from. Lastly, we add a thumbnail, which is the previously defined attachment. Then, we reply to the interaction, and we're done!And, we're done! Wasn't so hard, was it? This is how the code should look:Thank you for following this tutorial, I hope everything worked well!Templates let you quickly answer FAQs or store snippets for re-use. Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink. Hide child comments as well Confirm For further actions, you may consider blocking this person and/or reporting abuse pankrok - May 30 JS - Jun 21 Rubén Alapont - Jun 16 [email protected] - Jun 16 Once suspended, woodendoors7 will not be able to comment or publish posts until their suspension is removed. Once unsuspended, woodendoors7 will be able to comment and publish posts again. Once unpublished, all posts by woodendoors7 will become hidden and only accessible to themselves. If woodendoors7 is not suspended, they can still re-publish their posts from their dashboard. Note: Once unpublished, this post will become invisible to the public and only accessible to woodendoors7. They can still re-publish the post if they are not suspended. Thanks for keeping DEV Community safe. Here is what you can do to flag woodendoors7: woodendoors7 consistently posts content that violates DEV Community's code of conduct because it is harassing, offensive or spammy. Unflagging woodendoors7 will restore default visibility to their posts. DEV Community — A constructive and inclusive social network for software developers. With you every step of your journey. Built on Forem — the open source software that powers DEV and other inclusive communities.Made with love and Ruby on Rails. DEV Community © 2016 - 2023. We're a place where coders share, stay up-to-date and grow their careers.



This post first appeared on VedVyas Articles, please read the originial post: here

Share the post

How to make a Discord bot that pings your Minecraft server🤔

×

Subscribe to Vedvyas Articles

Get updates delivered right to your inbox!

Thank you for your subscription

×