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

Discord.JS v11.2.1创建MessageEmbed报错TypeError求助

Fixing "Cannot read property 'client' of undefined" with Discord.js v11.2.1 MessageEmbed

Hey there, let's break down why you're hitting this error and how to fix it quickly.

The Root Cause

The error happens because Discord.js v11.x requires a message object as the first parameter when creating a MessageEmbed—but most modern tutorials you're seeing are for v12+ of the library, which removed this requirement.

Looking at the line from MessageEmbed.js you mentioned:

Object.defineProperty(this, 'client', { value: message.client });

The constructor is trying to access message.client, but if you didn't pass a message object to new MessageEmbed(), it's undefined—hence the error.

How to Fix It

You have two options depending on whether you want to stick with v11 or upgrade:

Option 1: Stick with Discord.js v11.2.1

When creating your embed, pass the message object (from your client.on('message') callback) as the first argument:

// Inside your message event handler
client.on('message', message => {
  // Option A: Pass message + embed data directly
  const embed = new Discord.MessageEmbed(message, {
    title: "Hello!",
    description: "This is a working embed for v11",
    color: 0x00ff00
  });

  // Option B: Initialize with message, then set properties
  const embed = new Discord.MessageEmbed(message);
  embed.setTitle("Hello!")
       .setDescription("This also works")
       .setColor(0x00ff00);

  // Send the embed
  message.channel.send(embed);
});

Option 2: Upgrade to a Newer Discord.js Version

If you prefer the cleaner syntax from newer tutorials (no message parameter), you can upgrade Discord.js—but note that v12+ requires Node.js 12.0.0 or higher (you're on 8.9.4 right now).

First upgrade Node.js to a supported version, then update Discord.js:

npm install discord.js@latest

Then your embed code can follow the modern pattern:

const embed = new Discord.MessageEmbed()
  .setTitle("Hello!")
  .setDescription("No message parameter needed here");

Quick Check

Double-check that you're calling new MessageEmbed() inside your message event handler, where the message object is available. If you're trying to create an embed outside that scope, you'll need to pass a valid message object or consider upgrading.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 07:37:08