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

Discord.js:通过ID获取消息返回错误结果求助

Fixing the [object Promise] Issue with Message Fetching

Alright, let's break down what's going on here and fix that frustrating [object Promise] problem you're hitting. The core issue is that message fetching is an asynchronous operation—functions like fetchMessage() (or messages.fetch() in newer Discord.js versions) don't return the actual message right away. Instead, they hand you a Promise, which is JavaScript's way of saying "I'll get back to you with the data once it's ready." When you try to use this Promise directly in your embed, it just shows up as that generic [object Promise] text instead of the message content you want.

Here are two straightforward ways to resolve this:

You'll need to mark your command's callback function as async, then use the await keyword to pause execution until the Promise resolves and gives you the actual message object. We'll also add error handling to cover cases where the message ID is invalid, the message was deleted, or your bot lacks permissions to access it.

if (cmd === `${prefix}report`) {
  // Make sure your command's parent function is marked as async!
  let messageID = args[0];
  if (isNaN(messageID)) {
    await message.author.send("Not an ID");
    return;
  }

  try {
    // Wait for the fetch to complete and grab the message object
    let messagea = await message.channel.messages.fetch(messageID);
    
    // Now you can use messagea normally in your embed
    let reportEmbed = new Discord.MessageEmbed()
      .setTitle("Reported Message")
      .setDescription(messagea.content)
      .setAuthor(messagea.author.tag, messagea.author.displayAvatarURL())
      .setTimestamp();

    await message.channel.send({ embeds: [reportEmbed] });
  } catch (error) {
    // Handle fetch failures gracefully
    await message.author.send("Failed to fetch the message. Double-check the ID is valid and the message exists in this channel.");
    console.error("Error fetching reported message:", error);
  }
}

2. Use .then() Chaining (Alternative for Async/Await Newcomers)

If you're more comfortable with traditional Promise chaining, you can use .then() to access the resolved message object once the fetch completes, and .catch() to handle any errors that pop up:

if (cmd === `${prefix}report`) {
  let messageID = args[0];
  if (isNaN(messageID)) {
    message.author.send("Not an ID")
      .catch(err => console.error("Error sending DM:", err));
    return;
  }

  // Chain the fetch operation to access the message
  message.channel.messages.fetch(messageID)
    .then(messagea => {
      let reportEmbed = new Discord.MessageEmbed()
        .setTitle("Reported Message")
        .setDescription(messagea.content)
        .setAuthor(messagea.author.tag, messagea.author.displayAvatarURL())
        .setTimestamp();

      message.channel.send({ embeds: [reportEmbed] })
        .catch(err => console.error("Error sending embed:", err));
    })
    .catch(error => {
      message.author.send("Failed to fetch the message. Double-check the ID is valid and the message exists in this channel.")
        .catch(err => console.error("Error sending error DM:", err));
      console.error("Error fetching reported message:", error);
    });
}

Quick Important Notes:

  • Discord.js Version Check: If you're using v12 or newer, the old fetchMessage() method is deprecated—use message.channel.messages.fetch(messageID) instead (which is what the examples above use).
  • Never Skip Error Handling: Skipping try/catch or .catch() blocks can lead to uncaught exceptions that crash your bot, so always include them to handle edge cases.
  • Async 101: Any function that returns a Promise needs to be awaited or handled with .then() to get the actual data. Using the Promise directly will always result in [object Promise].

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 03:39:39