You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

discord.js编写!ping命令获取ms延迟时报TypeError错误如何解决

问题原因
  • 你错误调用了discord.js导出的Client类本身的ws属性,但是ws是实例化后的客户端对象才有的属性,不是构造类本身的静态属性
  • 你的run函数已经传入了实例化完成的client参数,直接调用这个参数的对应属性即可
修复后的代码
const { Client } = require("discord.js");
const Command = require("../Structures/Command.js");

module.exports = new Command({
    name: "ping",
    description: "Shows the ping of the bot!",

    async run(message, args, client) {
        // 将大写的Client类改为小写的client实例即可
        message.reply(`Ping: ${client.ws.ping} ms.`);
    },
});
可选优化方案

如果需要同时展示API延迟和消息往返延迟,可以使用如下写法:

async run(message, args, client) {
    const loadMsg = await message.reply('延迟计算中...');
    const roundTripLatency = loadMsg.createdTimestamp - message.createdTimestamp;
    loadMsg.edit(`API延迟: ${client.ws.ping} ms\n消息往返延迟: ${roundTripLatency} ms`);
}

内容的提问来源于stack exchange,提问作者Spademin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.07 08:06:03