简体   繁体   中英

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?

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`);
  }
});
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`); }) } });

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`);
        }
      };

you need to use.then() to have m:

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

you need to do

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`);
  }
});

I used a code where its date now and a message created timestamp and a api ping.

module.exports = {
  name: "ping",
  category: "info",
  description: "Get bot ping :/",
  usage: "ping",
  run: (client, message) => {
    message.channel.send(`**:ping_pong:Latency is ${Date.now() - message.createdTimestamp}ms. API Latency is ${Math.round(client.ws.ping)}ms.:ping_pong:**`);
  }
  
}

For me I used this it seemed to work for me!

module.exports = {
    name: 'ping',
    discription: 'description',
    execute(message, args) {
        message.channel.send(`🏓 | Latency is: **${Date.now() - message.createdTimestamp}ms.**`);
    },
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM