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

如何定义message变量?遇ReferenceError: message is not defined报错求助

Fixing ReferenceError: message is not defined in Discord.js

Hey there! Let's fix that annoying ReferenceError: message is not defined issue you're hitting. The problem here is that the message variable you're trying to use hasn't been created or passed into the scope where you're running that line of code.

In Discord.js, message is an object representing a message sent in a server or DM, and it's only available inside the callback function for the messageCreate event (or when explicitly passed as a parameter to other functions that need it). Here's how to properly define and use it:

1. Set Up the messageCreate Event Correctly

Starting with Discord.js v13+, the event for detecting new messages is messageCreate (replacing the old message event). You need to listen to this event, and the callback will receive the message object as a parameter. Here's a complete working example:

const { Client, IntentsBitField } = require('discord.js');
const client = new Client({
  intents: [
    IntentsBitField.Flags.Guilds,
    IntentsBitField.Flags.GuildMessages,
    IntentsBitField.Flags.MessageContent // Critical! Needed to read message content
  ]
});

// Listen for new messages
client.on('messageCreate', (message) => {
  // Now `message` is defined right here!
  const prefix = '!'; // Replace with your bot's command prefix
  const args = message.content.slice(prefix.length).trim().split(/ +/g);
  
  // Test it out
  console.log(`Command arguments: ${args.join(', ')}`);
});

// Log in with your bot token
client.login('YOUR_BOT_TOKEN'); // Replace with your actual bot token from Discord Developer Portal

Important Note: Don't forget to enable the Message Content Intent in your Discord Developer Portal (under your bot's "Privileged Gateway Intents" section) — without this, your bot won't be able to access message.content.

2. Pass message to Other Functions If Needed

If you want to use message in a separate helper function, you need to explicitly pass it as a parameter. For example:

// Helper function to handle commands
function handlePingCommand(message) {
  message.reply('Pong!'); // Uses the passed-in message object
}

client.on('messageCreate', (message) => {
  const prefix = '!';
  const args = message.content.slice(prefix.length).trim().split(/ +/g);
  
  // Call the helper function and pass `message`
  if (args[0] === 'ping') {
    handlePingCommand(message);
  }
});

Common Mistakes to Avoid

  • Using the old message event: Discord.js v13+ uses messageCreate — if you're still using client.on('message', ...), the event won't trigger, so message will never be defined.
  • Trying to use message outside the event callback: The message variable only exists within the scope of the messageCreate callback. If you try to run your args line outside of that, you'll get the error.
  • Missing required intents: Without the GuildMessages and MessageContent intents, your bot can't receive or read messages, which will also lead to issues with the message object.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.07 16:02:43