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

如何通过React更新Discord.Embed字段并实现多同模板命令独立运行?

解决Discord.py中React更新Embed字段的问题

你的代码目前有几个关键问题,导致无法实现预期效果,而且没法让多个命令独立工作。我来帮你逐个解决:

核心问题分析

  1. embed变量作用域问题:你在expedition命令里定义的embed是局部变量,在on_raw_reaction_add事件里根本访问不到,所以没法修改它。
  2. on_raw_reaction_add参数错误:这个事件接收的是RawReactionActionEvent(也就是payload)对象,不是reaction,user,你得从payload里提取消息ID、用户、表情等信息。
  3. 缺少消息编辑操作:就算你修改了embed,也得调用msg.edit(embed=updated_embed)才能把更新后的embed发送回Discord。
  4. 未区分不同消息的embed:多个命令生成的消息需要各自独立的embed实例,得用一个全局容器(比如字典)来关联消息ID和对应的embed。
  5. 未排除机器人自身反应:机器人添加表情的时候会触发自己的事件,得过滤掉,避免不必要的操作。
  6. 医疗表情格式问题:你用的六‍⚕️可能是组合表情,Discord识别有问题,换成标准的⚕️更可靠。

修正后的完整代码

import discord
from discord.ext import commands

# 初始化bot,记得替换你的前缀和token
intents = discord.Intents.default()
intents.message_content = True
intents.reactions = True
bot = commands.Bot(command_prefix="!", intents=intents)
client = bot  # 保持和你原代码的变量名一致

# 全局字典:存储消息ID -> 对应的embed对象,实现多个消息独立管理
message_embed_map = {}

@bot.command()
async def expedition(ctx, expe, date, heure):
    embed=discord.Embed(title="Expedition", description= f"{expe}\n{date} : {heure}\nFor {ctx.message.author.mention}", color=0xFF5733)
    embed.add_field(name="tank", value="1 slots", inline=True)
    embed.add_field(name="heal", value="1 slots", inline=True)
    embed.add_field(name="dps", value="3 slots", inline=True)
    msg = await ctx.send(embed=embed)
    # 将消息ID和对应的embed存入字典
    message_embed_map[msg.id] = embed
    # 添加反应,换成标准emoji
    await msg.add_reaction('\N{SHIELD}')
    await msg.add_reaction('⚕️')
    await msg.add_reaction('\N{CROSSED SWORDS}')
    await msg.add_reaction('❌')

# 你另外3个类似的命令,只需要在最后把msg.id和对应的embed存入message_embed_map即可,比如:
# @bot.command()
# async def another_expedition(ctx, ...):
#     embed = ...
#     msg = await ctx.send(embed=embed)
#     message_embed_map[msg.id] = embed
#     await msg.add_reaction(...)

@bot.event
async def on_raw_reaction_add(payload):
    # 排除机器人自己的反应
    if payload.user_id == bot.user.id:
        return
    
    # 检查当前消息是否在我们的管理字典里
    msg_id = payload.message_id
    if msg_id not in message_embed_map:
        return
    
    # 获取对应的embed和消息对象
    embed = message_embed_map[msg_id]
    channel = bot.get_channel(payload.channel_id)
    msg = await channel.fetch_message(msg_id)
    # 获取用户对象
    user = bot.get_user(payload.user_id)
    
    # 根据不同表情更新字段
    emoji = str(payload.emoji)
    if emoji == '\N{SHIELD}':
        embed.set_field_at(0, name="tank", value=user.mention)
        print("Tank slot updated")
    elif emoji == '⚕️':
        embed.set_field_at(1, name="heal", value=user.mention)
        print("Heal slot updated")
    elif emoji == '\N{CROSSED SWORDS}':
        embed.set_field_at(2, name="dps", value=user.mention)
        print("DPS slot updated")
    elif emoji == '❌':
        # 这里可以添加取消报名的逻辑,比如恢复成"1 slots"之类的
        print("Cancel reaction detected")
    
    # 编辑消息,更新embed
    await msg.edit(embed=embed)

# 运行bot,替换成你的token
bot.run("YOUR_BOT_TOKEN")

关键改进点说明

  • message_embed_map字典:每个命令生成的消息ID都会和它的embed绑定,这样四个命令的消息完全独立,各自的React操作只会修改自己对应的embed。
  • 事件参数修正:用payload正确获取消息ID、用户ID、表情等信息,避免参数错误导致的崩溃。
  • 过滤机器人反应:通过payload.user_id == bot.user.id排除机器人自身的反应,防止触发不必要的更新。
  • 消息编辑:每次修改embed后都调用msg.edit(embed=embed),把更新后的内容同步到Discord。
  • 字符串格式化优化:用f-string替换原来的字符串拼接,代码更简洁易读。

这样修改后,你的四个命令生成的消息都会独立响应React操作,正确更新对应的字段啦。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.30 21:28:12