How i can do a ping command

How i can do a ping command like this:Screenshot_20200114-093225

this might be worth checking out. DiscordJs Bot Tutorial

alternatively, I run a code server that you can join just tag me if thats easier for you and i will help you as soon as i can. I assume you want someone to give u the code. Not help u write it yourself correct?

Check this out: https://github.com/promise/countr/blob/master/commands/ping.js#L11-L42

1 Like

Hey @ControlCit1,

The ping command is based on the difference in the time at which the message was posted and the timestamp of the message. This is generally how it works.

var ping = Date.now() - message.createdTimestamp + " ms";

To make this into a command, just wrap it up like this:

  if (message.content.startsWith(prefix + "ping")) {
    
    var ping = Date.now() - message.createdTimestamp + " ms";
    message.channel.sendMessage("Your ping is `" + `${Date.now() - message.createdTimestamp}` + " ms`");

  }

Replace prefix with the prefix of your bot.

Hope this helps!

2 Likes

people actually do it like that still? I would be lost without a command handler

3 Likes

Itu membuat bot tidak meresponds

It would be smarter to use the ping variable :grinning:

if (message.content.startsWith(prefix + "ping")) {
    var ping = Date.now() - message.createdTimestamp + " ms";
    message.channel.sendMessage("Your ping is `" + `${ping}` + " ms`");
}
1 Like

It would be smart to do msg.channel.send(“Your ping is " + ${ping} + "”); to prevent two “ms”

Technically it’s not their ping, it’s the bot’s ping to discord, not the opposite. And you should also use client.ws.ping to get the ping.

1 Like

It’d be easier to use template literals
same thing just neater i guess

if (message.content.startsWith(`${prefix} ping`)) {
    var ping = `${Date.now() - message.createdTimestamp} ms`;
    message.channel.sendMessage(`Your ping is ${ping}`);
}

cleaner version is

if (message.content.startsWith(prefix + "ping")) {
    message.channel.sendMessage(`Your ping is \`${Date.now() - message.createdTimestamp} ms\``);
  }

Even cleaner

if (message.content.startsWith(`${prefix}ping`))
    message.channel.sendMessage(`Your ping is \`${Date.now() - message.createdTimestamp}ms\``);

even cleaner

if (message.content.startsWith(`${prefix}ping`)) 
    msg.reply(`Your ping is \`${Date.now() - msg.createdTimestamp} ms\``);

SyntaxError, msg is undefined.

Can’t we just do this?

msg.reply(`Your ping is ${Date.now() - msg.createdTimestamp} ms`);

Also why is everyone trying to clean it up? :joy:

1 Like

You can do
message.channel.send(`${message.author}`, new Discord.MessageEmbed() .setTitle(`🏓**Pong** `) .setDescription(`**Latency:**${Date.now() - message.createdTimestamp}ms`) .setColor(‘BLUE’));
It worked for my and it’s like the screenshot you send
image

How do you do this for a slash command?