So I recently decided to make a Discord bot, and had been going through some tutorials with no problem. However, when I tried to make a ping command (you know, the kind that says "Your ping is 137 ms
"), I got numbers that didn't make sense.
Numbers such as -627 ms
, 32 ms
, 1001 ms
, -10 ms
, 238 ms
. Basically anything and everything between -1000
and 1000
.
Now correct me if I'm wrong, but I'm pretty sure that getting negative numbers means I'm sending the response before I even ask for it, and I'm almost certain I didn't write a precognitive bot :P
I spent some time online looking for a reason why I was getting strange ping numbers, but I came up with zilch. I thought to myself, "Hmmmm..... maybe I'm generating them wrong." So I looked at how other people generated their numbers with discord.js...
...aaaand I hit a wall. As far as I can tell, no one has ever tried generating ping numbers with discord.js. If they have, they haven't shared their precious findings online. At least, that's what it looks like to me. (if you have a link to such a tutorial or post, please send it to me)
I'm generating the numbers with ${Date.now() - message.createdTimestamp}
This is the only way I have found to generate them, so if anyone has a better method, I would love to see it.
That snippet fits right in with the rest of my code (and yes, it is a very boring bot):
const Discord = require('discord.js');
const client = new Discord.Client();
const token = require('./token.json').token;
client.on('ready', () => {
console.log('Bot is up and running!');
});
var prefix = "?"
client.on('message', message => {
if (message.author.bot) return;
if (!message.content.startsWith(prefix)) return;
if (message.content.startsWith(prefix + 'ping')) {
message.channel.sendMessage('Pong! Your ping is `' + `${Date.now() - message.createdTimestamp}` + ' ms`');
}
});
client.login(token);
If anybody could give me a reason why I'm getting negative numbers, or give me a better way to generate them, I would be extremely grateful. Thanks in advance.