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

discord.js斜杠命令运行报错ReferenceError: message未定义

报错原因

ReferenceError: message is not defined 是因为你错误给INTERACTION_CREATE事件的回调函数声明了第二个message形参——这个WebSocket事件触发时只会传入交互对象interaction这一个参数,根本不会传message对象,你声明的第二个形参实际值为undefined,自然访问message.createdTimestamp时会抛出未定义错误。

斜杠命令和传统前缀消息命令是两套完全独立的交互机制,斜杠命令触发时不存在普通消息场景下的message对象,不能直接照搬前缀命令里的延迟计算逻辑。

修复方案
  • 删除INTERACTION_CREATE回调里多余的message形参
  • 延迟计算改用交互对象自带的时间戳字段,不要引用不存在的message变量

修复后的对应代码段如下:

client.ws.on('INTERACTION_CREATE', async (interaction) => {
    const slashCommmand = interaction.data.name.toLowerCase()

    if(slashCommmand === 'ping') {
        client.api.interactions(interaction.id, interaction.token).callback.post({
            data: {
                type: 4,
                data: {
                    // 用交互自带的timestamp计算延迟,替代不存在的message.createdTimestamp
                    content: `🏓Latency is ${Date.now() - Date.parse(interaction.timestamp)}ms.`,
                },
            },
        })
    }
})
补充说明
  • 如果需要展示Websocket延迟,可以直接读取client.ws.ping属性拼接到返回内容里
  • 两套命令体系的事件回调参数不通用,写逻辑时不要直接复用另一套体系里的变量名,先确认对应事件实际返回的参数结构

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.29 00:45:48