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: falseso 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

