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

如何使用已有的discord.py代码发送Embed消息

适配后完整可运行代码

以下代码完全兼容你原有基础逻辑,新增了!测试embed关键词触发Embed卡片的功能,你可以直接替换原有代码使用:

import discord

# 替换为你自己的机器人TOKEN
TOKEN = 'xxxxxxxxxxxxxxxxxxxx'

# 配置机器人意图,Discord要求必须开启对应权限才能读取消息内容
intents = discord.Intents.default()
intents.message_content = True
client = discord.Client(intents=intents)

@client.event
async def on_ready():
    print('We have logged in as {0.user}'.format(client))


@client.event
async def on_message(message):
    username = str(message.author).split('#')[0]
    user_message = str(message.content)
    channel = str(message.channel.name)
    print(f'{username}: {user_message} ({channel})')

    if message.author == client.user:
        return

    # 触发Embed的关键词规则,可自行修改
    if user_message == '!测试embed':
        # 初始化Embed实例
        embed = discord.Embed(
            title="Embed测试标题",
            description="这是Embed卡片的正文描述,支持自定义所有内容",
            color=discord.Color.green() # 卡片左侧边栏颜色,也可直接写十六进制值,比如0xFF5733
        )
        # 添加分段信息字段
        embed.add_field(name="第一个字段", value="第一个字段的内容", inline=False)
        embed.add_field(name="并排字段1", value="内容1", inline=True)
        embed.add_field(name="并排字段2", value="内容2", inline=True)
        # 底部页脚
        embed.set_footer(text=f"触发用户:{username}")
        # 发送Embed消息
        await message.channel.send(embed=embed)

client.run(TOKEN)

自定义说明

你可以根据需求调整Embed的内容,常用可修改参数如下:

  • title:Embed卡片的顶部大标题
  • description:卡片的正文核心内容
  • color:卡片左侧的高亮边栏颜色,除了discord预置的blurple/green/red/yellow等,也可以直接使用十六进制颜色值
  • add_field:用于添加分段展示的内容,inline参数设为True时多个字段可以横向并排展示
  • 还可以扩展使用set_image()添加卡片大图、set_thumbnail()添加右上角小图标、set_author()添加作者栏等属性

运行注意事项

  1. 将代码中的TOKEN替换为你自己的Discord机器人令牌
  2. 前往Discord开发者平台的机器人设置页,开启Message Content Intent权限,否则机器人无法读取消息内容
  3. 确保安装了最新版discord.py库,安装/更新命令:pip install -U discord.py
  4. 代码运行成功后,在机器人加入的服务器中发送!测试embed即可看到效果

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.03 16:45:01