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

Discord.py中如何实现点击按钮/表情后按序列发送嵌入式消息

实现方案

方案1:按钮交互(更推荐)

该方案基于discord.py 2.0+版本的原生UI组件实现,交互体验更好,不会被无关操作触发。

前置依赖

导入所需组件:

import discord
from discord.ui import Button, View

初始化机器人时需要开启对应意图:

intents = discord.Intents.default()
intents.message_content = True
client = discord.Client(intents=intents)

完整代码示例

# 定义按钮点击的回调逻辑
async def send_second_embed(interaction: discord.Interaction):
    # 构造第二条嵌入式消息
    second_embed = discord.Embed(
        title="消息通知", 
        description="#公告频道是服务器最重要的信息渠道,你可以通过该频道实时了解服务器内的所有动态,不会错过任何重要通知", 
        color=0x00ff00
    )
    second_embed.add_field(name="全程仅需几分钟", value="点击下方按钮继续")
    second_embed.set_footer(text="Christer - 使用说明", icon_url="https://cdn.icon-icons.com/icons2/2619/PNG/512/among_us_discord_icon_156922.png")        
    # 发送第二条消息,ephemeral参数设为False则所有人可见,设为True则仅点击按钮的用户可见
    await interaction.response.send_message(embed=second_embed)

@client.event
async def on_message(message):
    if message.content == 'test':
        how_channel = client.get_channel(CHANNELNAME)
        # 构造第一条嵌入式消息
        first_embed = discord.Embed(
            title="功能使用快速引导", 
            description="你可以通过百分比进度条查看当前的引导完成进度,点击下方按钮继续流程", 
            color=0x00ff00
        )
        first_embed.add_field(name="全程仅需几分钟", value="点击下方按钮继续")
        first_embed.set_footer(text="Christer - 使用说明", icon_url="https://cdn.icon-icons.com/icons2/2619/PNG/512/among_us_discord_icon_156922.png")
        # 创建按钮并绑定回调
        continue_btn = Button(label="继续", style=discord.ButtonStyle.green)
        continue_btn.callback = send_second_embed
        view = View()
        view.add_item(continue_btn)
        # 发送第一条消息,携带按钮组件
        await how_channel.send(embed=first_embed, view=view)

方案2:表情反应交互

如果使用低版本discord.py,也可以通过监听表情添加事件实现:

@client.event
async def on_message(message):
    if message.content == 'test':
        how_channel = client.get_channel(CHANNELNAME)
        first_embed = discord.Embed(
            title="功能使用快速引导", 
            description="你可以通过百分比进度条查看当前的引导完成进度,添加下方指定表情继续流程", 
            color=0x00ff00
        )
        first_embed.add_field(name="全程仅需几分钟", value="添加✅表情继续")
        first_embed.set_footer(text="Christer - 使用说明", icon_url="https://cdn.icon-icons.com/icons2/2619/PNG/512/among_us_discord_icon_156922.png")
        # 发送消息后主动添加指定表情作为交互入口
        first_msg = await how_channel.send(embed=first_embed)
        await first_msg.add_reaction("✅")

# 监听表情添加事件
@client.event
async def on_reaction_add(reaction, user):
    # 过滤机器人自身的操作
    if user.bot:
        return
    # 校验触发条件:表情为✅、消息是我们发送的第一条引导embed
    if reaction.emoji == "✅" and reaction.message.embeds and reaction.message.embeds[0].title == "功能使用快速引导":
        second_embed = discord.Embed(
            title="消息通知", 
            description="#公告频道是服务器最重要的信息渠道,你可以通过该频道实时了解服务器内的所有动态,不会错过任何重要通知", 
            color=0x00ff00
        )
        second_embed.add_field(name="全程仅需几分钟", value="添加✅表情继续")
        second_embed.set_footer(text="Christer - 使用说明", icon_url="https://cdn.icon-icons.com/icons2/2619/PNG/512/among_us_discord_icon_156922.png")        
        await reaction.message.channel.send(embed=second_embed)

注意事项

  • 按钮方案仅支持discord.py 2.0及以上版本
  • 两种方案都需要在Discord开发者后台开启机器人对应的message_content、guilds权限
  • 如果需要限制仅触发指令的用户可以点击按钮/添加表情触发后续流程,可以在回调逻辑中增加用户ID校验

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.28 03:57:03