Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Discord.js Ping Command

I was trying to make a ping command for my bot here is my Code

client.on('message', message => {
  if (message.content === '+ping') {  
    message.channel.send(`🏓Latency is ${m.createdTimestamp - message.createdTimestamp}ms. API Latency is ${Math.round(client.ws.ping)}ms`);
  }
});

However I end up getting the following error

C:\Users\ujjwa\Desktop\All Disc\Test all\index.js:236
    message.channel.send(`🏓Latency is ${m.createdTimestamp - message.createdTimestamp}ms. API Latency is ${Math.round(client.ws.ping)}ms`);
                                     ^

ReferenceError: m is not defined
    at Client.<anonymous> (C:\Users\lol\Desktop\All Disc\Test all\index.js:236:42)
    at Client.emit (events.js:327:22)
    at MessageCreateAction.handle (C:\Users\lol\Desktop\All Disc\Test all\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
    at Object.module.exports [as MESSAGE_CREATE] (C:\Users\lol\Desktop\All Disc\Test all\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (C:\Users\lol\Desktop\All Disc\Test all\node_modules\discord.js\src\client\websocket\WebSocketManager.js:386:31)
    at WebSocketShard.onPacket (C:\Users\lol\Desktop\All Disc\Test all\node_modules\discord.js\src\client\websocket\WebSocketShard.js:436:22)
    at WebSocketShard.onMessage (C:\Users\lol\Desktop\All Disc\Test all\node_modules\discord.js\src\client\websocket\WebSocketShard.js:293:10)
    at WebSocket.onMessage (C:\Users\lol\Desktop\All Disc\Test all\node_modules\ws\lib\event-target.js:125:16)
    at WebSocket.emit (events.js:315:20)
    at Receiver.receiverOnMessage (C:\Users\lol\Desktop\All Disc\Test all\node_modules\ws\lib\websocket.js:797:20)

Can you help me out?

like image 698
Lallu Avatar asked Aug 14 '20 10:08

Lallu


People also ask

How do you show Ping in discord?

Press the “@” icon at the bottom of the screen. The “Recent Mentions” screen will display. To narrow down the pings you want to see, tap the “Filter Options” icon at the top-right corner. Under “Server Options,” select the appropriate option to see pings from the server you're on right now or “All Servers.”

Can you use Javascript in discord?

There are also powerful APIs for creating Discord bots for those who prefer to take matters into their own hands. For instance, Discord. js allows us to create a simple Discord bot using Javascript.


3 Answers

You need to use Date.now() - message.createdTimestamp to get the latency.

client.on('message', message => {
  if (message.content === '+ping') {  
    message.channel.send(`🏓Latency is ${Date.now() - message.createdTimestamp}ms. API Latency is ${Math.round(client.ws.ping)}ms`);
  }
});
like image 60
Static Avatar answered Nov 11 '22 22:11

Static


client.on('message', message => {
  if (message.content === prefix + 'ping') {
  message.channel.send('Loading data').then (async (msg) =>{
    msg.delete()
    message.channel.send(`🏓Latency is ${msg.createdTimestamp - message.createdTimestamp}ms. API Latency is ${Math.round(client.ws.ping)}ms`);
  })
  }
});
like image 20
PeterGamez Avatar answered Nov 11 '22 23:11

PeterGamez


I use this As My Ping Command (Sorry if I make Any Kind of Mistake, I just learned JavaScript in the last 2 Days)

module.exports={
    name:'ping',
    description: "Command ini digunakan untuk Ping",
    execute(message, args){
        message.channel.send(`🏓Latency is ${Date.now() - message.createdTimestamp}ms`);
        }
      };
like image 42
Fachri Avatar answered Nov 11 '22 23:11

Fachri