Tutorial: Discord Bot Ping Command

Oskar Xion
3 min readApr 9, 2020

--

If you have no experience on how to code discord bots read my last article:

Requirements

  • You must have Node JS installed on your desktop or hosting.
  • You must have a text editor installed. (I recomment Atom)
  • You must have a discord account

Lets Get Started

In the last tutorial we created the following files server.js, config.json and package.json.
In this tutorial we will just need to edit the server.js

const express = require("express");
const app = express();
const config = require("./config.json");
var server = require("http").createServer(app);app.get("/", (request, response) => {
console.log(Date.now() + " Ping Received");
response.sendStatus(200);
});const listener = server.listen(process.env.PORT, function() {
console.log("Your app is listening on port " + listener.address().port);
});setInterval(() => {
}, 280000);const Discord = require("discord.js");
const {Client, Attachment, Collection, RichEmbed } = require("discord.js");const client = new Client({
disableEveryone: true
});client.on("ready", () => {
console.log(
`Ready to serve on ${client.guilds.size} servers, for ${client.users.size} users.`
);
let activities = [
`${client.guilds.size} Guilds!`,
`${client.channels.size} Channels!`,
`${client.users.size} Members!`
],
i = 0;
setInterval(
() =>
client.user.setActivity(
`btc-help | ${activities[i++ % activities.length]}`,
{ type: "WATCHING" }
),
15000
);
});
client.on("message", message => {
if (message.author.bot) return;
if (message.content.indexOf(config.prefix) !== 0) return;const args = message.content.slice(config.prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
if(command === 'hello') {
message.reply("Hey, How are you?");
}
});
client.login(config.bot);

Lets get a closer look on the last part.

client.on("message", message => {
if (message.author.bot) return;
if (message.content.indexOf(config.prefix) !== 0) return;
const args = message.content.slice(config.prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
if(command === 'hello') {
message.reply("Hey, How are you?");
}
});

In the last part we can see this:

if(command === 'hello') {
//code
}

Now we will change the command from hello to ping.

if(command === 'ping') {
//code
}

Now we after the bot hast checked if the command equals ping we will execute the code to get the ping and we will send it back to the channel or message author:

if(command === 'ping') {
//send the message to the channel
message.channel.send(`Ping: ${Math.round(client.ping)}ms`);
//send the message to the author
message.author.send(`Ping: ${Math.round(client.ping)}ms`);)
}

If you want, you can also add a loading message, that will be edited to the Ping message

if(command === 'ping') {
//send the message to the channel
message.channel.send("Pinging....").then((msg)=>{
msg.edit(`Ping: ${Math.round(client.ping)}ms`);
//send the message to the author
message.author.send("Pinging....").then((msg)=>{
msg.edit(`Ping: ${Math.round(client.ping)}ms`);
}

Run your Bot

Now its time to run our bot! Just type in:

node server.js

Moment of Truth

Now go to your chat server and make sure your bot is connected and online, then type in to the chat box “-ping” it should reply with the ping to you.

If it worked then Congratulations you have created your first custom command!

Its just a start but i guess you wouldnt have thought it is so easy.

I hope you learned something and i could help you. Keep going! If you have any questions you can contact me on my social media.

--

--

Oskar Xion
0 Followers

I’m an always optimistic, open minded and knowledge seeking fullstack developer passionate about UI/UX and changing things for the better :)