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

discord.py模块如何添加embed消息 现有代码报错该怎么修复

报错修复方案
  • 直接触发报错的原因是变量名不匹配:你在代码中定义的Embed实例变量名为embedVar,但调用发送方法时使用了未定义的embed变量
  • 同时存在参数写法错误:discord.py v1.x及以上版本中,message.channel.send()不需要将频道对象作为第一个入参,且embed参数名是小写的embed,不是大写开头的Embed

修正后的完整可运行代码如下:

import os
import discord

TOKEN = os.environ['TOKEN']
client = discord.Client()

@client.event
async def on_ready():
    print("bot online") 

@client.event
async def on_message(message):
    # 避免机器人响应自己发送的消息,建议保留该判断
    if message.author == client.user:
        return
    if message.content == "!Help":
        embedVar = discord.Embed(title="功能说明", description="机器人指令说明", color=0x00ff00)
        embedVar.add_field(name="!Help", value="调出帮助菜单", inline=False)
        embedVar.add_field(name="字段2", value="对应功能说明", inline=False)
        await message.channel.send(embed=embedVar)

client.run(TOKEN)
Embed正确用法说明
  • 实例化Embed对象:通过discord.Embed()创建实例,常用参数如下:
    • title:Embed顶部展示的大标题,字符串类型
    • description:标题下方的描述内容,字符串类型
    • color:Embed侧边条的颜色,传入十六进制颜色值,例如0xff0000对应红色
  • 新增内容字段调用add_field()方法,参数说明:
    • name:字段的小标题,必填
    • value:字段的具体内容,必填
    • inline:是否和其他字段同行展示,设为False会独占一行,默认值为True
  • 发送Embed只需要在send()方法中传入embed=你的Embed实例变量即可

如果你确实使用的是低于1.0的废弃版本,发送写法改为await client.send_message(message.channel, embed=embedVar)即可,目前Replit默认安装的都是v2.x版本,使用上述修正后的代码即可正常运行。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.29 11:24:03