I make ping command in discord.js
, but I got an error: TypeError: Cannot read properties of undefined (reading 'ws')
. If you can help me, I will be very grateful, this is my code:
const { MessageEmbed } = require("discord.js");
const commandsHandler = require("../../Handlers/commands-handler");
module.exports = {
name: "ping",
aliases: ['latency', 'lag'],
description: "Ping"
execute(message, args, commandName, client, Discord) {
const Response = new MessageEmbed()
.setColor("BLUE")
.setDescription(`🏓${client.ws.ping}ms`);
message.channel.send({embeds: [Response]});
}
}
TypeError: Cannot read properties of undefined (reading 'ws')
This means that client
in client.ws.ping
is undefined
.
To prevent your program from crashing add a guard clause for client being undefined
like this:
.setDescription(`🏓${client?.ws?.ping || 'undefined'}ms`);
or at right before const Response = new ...
, write this:
if (!client) return; // breaks out of function if `client` is `undefined`.
But you'll need to dig deeper to find out why client
is coming in undefined
.