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

Discord Bot问题:删除消息日志可存文本但无法处理文件

Fixing the RangeError When Deleting Media Messages in Your Discord Bot

Let's break down why you're hitting this error and how to fix it:

The Root Cause

When a user deletes a message that only contains images/videos (no text content), messageDelete.content becomes an empty string. Discord.js strictly enforces that MessageEmbed field values can't be empty, which triggers the RangeError [EMBED_FIELD_VALUE] you're seeing.

Also, that first conditional block in your code does nothing useful—comparing messageDelete.content to Discord.MessageEmbed (a class reference) will never be true, and the line messageDelete.content === 'embed' is just a comparison, not an assignment. You can safely remove that part.

The Fixed Code

Here's an updated version of your event handler that handles both text and media messages properly:

client.on("messageDelete", (messageDelete) => {
  // Handle empty message content (media-only messages)
  const messageContent = messageDelete.content || "*This message contained only media (images/videos/files)*";
  
  // Get attachment info if there are any
  let attachmentInfo = "";
  if (messageDelete.attachments.size > 0) {
    attachmentInfo = messageDelete.attachments.map(attch => 
      `[${attch.name}](${attch.url})`
    ).join("\n");
  }

  const embed = new Discord.MessageEmbed()
    .setColor("PURPLE")
    .setDescription("A message has been deleted")
    .setAuthor(`${messageDelete.author.tag}`, messageDelete.author.displayAvatarURL({ dynamic: true }))
    .setThumbnail(messageDelete.author.displayAvatarURL({ dynamic: true }))
    .addFields(
      { name: "Message Content:", value: messageContent, inline: false },
      { name: "Channel:", value: `${messageDelete.channel}`, inline: true },
      { name: "Author:", value: `${messageDelete.author.tag}`, inline: true }
    )
    .setFooter(`The message may be deleted by the user or a bot.`)
    .setTimestamp();

  // Add attachments field if there are any
  if (attachmentInfo) {
    embed.addField("Attachments:", attachmentInfo, inline: false);
  }

  client.channels.cache.get('723005913841008662').send(embed);
});

Key Improvements

  • Empty Content Handling: We use messageDelete.content || "*This message contained only media..." to ensure the content field always has a value, even for media-only messages.
  • Attachment Support: We check if the deleted message had attachments, then format them as clickable links so you can view the deleted media directly from the log embed.
  • Cleaned Up Unused Code: Removed the useless conditional block that wasn't doing anything.
  • Better Field Layout: Changed the content field to inline: false so it gets its own line, making longer text or attachment links easier to read.

Additional Notes

  • Make sure your bot has the necessary permissions to send embeds and view message history in both the source channels and the log channel.
  • If you want to display previews of images, you can use the attachment URL as an image in the embed with .setImage(attch.url) (just note that only one image can be set per embed, so you might pick the first attachment if there are multiple).

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.09 12:42:42