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

运行Discord.js建议命令报TypeError: Cannot read property 'channel' of undefined如何解决

报错原因
  • 核心拼写错误:message.guild下的频道集合属性为复数channels,你的代码写为单数channel,导致message.guild.channel取值为undefined,读取cache属性时触发类型错误。
  • 变量名不匹配:函数入参传入的discord实例变量名为小写discord,你初始化Embed时写的是首字母大写的Discord,后续运行会额外抛出引用错误。
  • 逻辑偏差:代码查找了专属建议频道,但最终Embed发送到了命令执行的当前频道,不符合功能设计预期。
修复后完整代码
module.exports = {
    name: 'suggestions',
    aliases: ['suggest'],
    execute(message, args, cmd, client, discord) {
        // 修正频道属性为复数channels
        let channel = message.guild.channels.cache.find(ch => ch.id === '890313569260937247')
        if(!channel) return message.channel.send({ content: '当前服务器没有配置建议收集频道!' });

        let messageArgs = args.join(' ')
        // 修正discord变量名大小写
        const embed = new discord.MessageEmbed()
        .setColor('#008000')
        .setAuthor(message.author.tag)
        .setDescription(messageArgs)
        .setTitle(`${message.author.tag} 提交了一条建议!`)

        // 修正为发送到预设的建议频道,可选保留当前频道提示
        channel.send({ embeds: [embed] })
        message.channel.send({ content: '建议已提交成功,感谢反馈!' })
    }
}
补充说明

如果你使用的是discord.js v14及以上版本,MessageEmbed已被弃用,需要替换为EmbedBuilder,构造方法和参数也会有对应调整,上述代码适配v12/v13版本可直接运行。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.02 21:00:05