Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
238 views
in Technique[技术] by (71.8m points)

javascript - 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:UsersujjwaDesktopAll DiscTest allindex.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:UserslolDesktopAll DiscTest allindex.js:236:42)
    at Client.emit (events.js:327:22)
    at MessageCreateAction.handle (C:UserslolDesktopAll DiscTest all
ode_modulesdiscord.jssrcclientactionsMessageCreate.js:31:14)
    at Object.module.exports [as MESSAGE_CREATE] (C:UserslolDesktopAll DiscTest all
ode_modulesdiscord.jssrcclientwebsockethandlersMESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (C:UserslolDesktopAll DiscTest all
ode_modulesdiscord.jssrcclientwebsocketWebSocketManager.js:386:31)
    at WebSocketShard.onPacket (C:UserslolDesktopAll DiscTest all
ode_modulesdiscord.jssrcclientwebsocketWebSocketShard.js:436:22)
    at WebSocketShard.onMessage (C:UserslolDesktopAll DiscTest all
ode_modulesdiscord.jssrcclientwebsocketWebSocketShard.js:293:10)
    at WebSocket.onMessage (C:UserslolDesktopAll DiscTest all
ode_moduleswslibevent-target.js:125:16)
    at WebSocket.emit (events.js:315:20)
    at Receiver.receiverOnMessage (C:UserslolDesktopAll DiscTest all
ode_moduleswslibwebsocket.js:797:20)

Can you help me out?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...