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

discord.js开发按钮命令触发Cannot read property 'reply' of null报错如何解决

报错原因

你遇到的TypeError: Cannot read property 'reply' of null错误,本质是你在回调中重命名为msgInt的interaction参数值为null,没有成功取到交互实例。
触发该问题的常见原因有两个:

  • 你当前使用的WOKCommands版本中,默认前缀命令不会传入interaction参数,只有斜杠命令/上下文菜单命令触发时才会生成有效的interaction实例。你的命令没有指定命令类型,若你是用前缀触发该命令,就会出现interaction为null的情况。
  • 你邀请Bot进服务器时没有勾选applications.commands权限,斜杠命令没有成功注册,触发时无法拿到有效交互实例。

修复方案

方案1:指定命令为斜杠命令(推荐)

直接在命令配置中新增type: "SLASH"字段,让WOKCommands将该命令仅注册为斜杠命令,保证触发时一定能拿到有效interaction实例。
修改后的代码示例:

import { MessageActionRow, MessageButton, MessageEmbed } from "discord.js";
import { ICommand } from "wokcommands";

export default {
    category: "Testing",
    description: "Interazione pulsante",
    // 新增这一行,指定为斜杠命令
    type: "SLASH",

    callback: async ({ interaction: msgInt, channel }) => {
        const row = new MessageActionRow()
            .addComponents(
                new MessageButton()
                .setCustomId("yes")
                .setStyle("SUCCESS")
                .setLabel("Conferma")
            )
            .addComponents(
                new MessageButton()
                    .setCustomId("no")
                    .setStyle("DANGER")
                    .setLabel("Nega")
            )
        const embed = new MessageEmbed()
                .setDescription("**SICURO DI PROCEDERE?**")
                // 补充:discord.js v13+版本setAuthor需传对象,否则会触发警告,可按需修改
                .setAuthor({ name: "clicca su un pulsante per rispondere." })
                .setColor("ORANGE")
        
        await msgInt.reply({
            content: "sicuro di procedere?",
            embeds: [embed],
            components: [row],
        })
    }
} as ICommand

方案2:兼容前缀+斜杠命令

如果你需要同时支持两种触发方式,可以先判断interaction是否存在,再选择对应的消息发送方式:

callback: async ({ interaction: msgInt, channel, message }) => {
    // 省略组件、Embed构造逻辑
    const sendContent = {
        content: "sicuro di procedere?",
        embeds: [embed],
        components: [row],
    }
    if (msgInt) {
        await msgInt.reply(sendContent)
    } else if (message) {
        await channel.send(sendContent)
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.25 22:06:03