You need to enable JavaScript to run this app. Discord.js抛出ReferenceError: message is not defined错误修复求助
优惠活动
大模型
产品
解决方案
定价
更多

Discord.js抛出ReferenceError: message is not defined错误修复求助

问题解决方案

核心错误原因

你在事件回调中定义的参数名为msg,但后续逻辑全部调用了未声明的变量message,这是触发ReferenceError: message is not defined的直接原因。

其他潜在问题

  1. 模板字符串需要用反引号包裹才能正确解析${}插值表达式,你当前使用双引号包裹,会导致插值语句直接作为普通文本输出,无法正常获取用户信息。
  2. 若你使用discord.js v13及以上版本,message事件已被官方废弃,需要替换为messageCreate事件才能正常触发消息监听。

修复后代码

方案1:统一参数名和内部调用变量名(推荐)

// discord.js v13+ 使用messageCreate事件,v12及以下保留message即可
client.on("messageCreate", (message) => {
    if (message.content === "sky claim") {
        message.channel.send(`${message.author} claimed 250 SkyBucks. Spend SkyBucks on colored roles.`);
        // 日志输出用户标签更易读,不需要可改回message.author
        console.log(`${message.author.tag} executed command successfully`);
    }
});

方案2:修改内部调用变量和参数名一致

client.on("message", (msg) => {
    if (msg.content === "sky claim") {
        msg.channel.send(`${msg.author} claimed 250 SkyBucks. Spend SkyBucks on colored roles.`);
        console.log(`${msg.author.tag} executed command successfully`);
    }
});

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.14 15:36:10