首页 > 解决方案 > discord.embed 更新与角色反应

问题描述

我正在尝试用反应更新 discord.embed:

@bot.command()
async def expedition(ctx, expe, date, heure):
    embed=discord.Embed(title="Expedition", description= 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)
    await msg.add_reaction('\N{SHIELD}')
    await msg.add_reaction('‍⚕️')
    await msg.add_reaction('\N{CROSSED SWORDS}')
    await msg.add_reaction('❌')

    @client.event
    async def on_raw_reaction_add(reaction,user):
        if reaction.emoji == '\N{SHIELD}':
            embed.set_field_at(0,name="tank",value=user.mention)
            print("test 1")
        if reaction.emoji == '‍⚕️':
            embed.set_field_at(1,name="heal",value=user)
            print("2 test")
        if reaction.emoji == '\N{CROSSED SWORDS}':
            embed.set_field_at(2,name="dps",value=user)
            print("D la réponse D")

我创建了一个嵌入了一些默认信息的不和谐,并添加了我想要使用的反应,其想法是当有人对选择反应之一做出反应时,用户的名称被添加到该字段中。

(我实际上有 4 个命令使用相同的“模板”并具有相同的反应,我希望它们都单独工作)

标签: pythondiscorddiscord.py

解决方案


您需要为此使用wait_for

on_raw_reaction_add只需要一个参数payload,我相信你正在尝试使用on_reaction_add

如果您只想考虑添加的第一个反应,请删除 while 循环

async def expedition(ctx, expe, date, heure):
    embed=discord.Embed(title="Expedition", description= 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)
    await msg.add_reaction('\N{SHIELD}')
    await msg.add_reaction('‍⚕️')
    await msg.add_reaction('\N{CROSSED SWORDS}')
    await msg.add_reaction('❌')

    while True:
        reaction, user = await client.wait_for("reaction_add", check=lambda reaction, user: reaction.message.id == msg.id)
        if reaction.emoji == '\N{SHIELD}':
            embed.set_field_at(0,name="tank",value=user.mention)
            print("test 1")
        if reaction.emoji == '‍⚕️':
            embed.set_field_at(1,name="heal",value=user)
            print("2 test")
        if reaction.emoji == '\N{CROSSED SWORDS}':
            embed.set_field_at(2,name="dps",value=user)
            print("D la réponse D")

推荐阅读