我希望我的机器人在用户对上述消息做出反应时发送嵌入。不知道我将如何做这件事

时间:2021-04-04 00:51:28

标签: python discord.py

@client.command() 异步 def ra(ctx):

embed = discord.Embed(
    colour=discord.Colour.blue(),
    title="RIDEALONG REQUEST",
    description=str(ctx.author.mention) + " IS REQUESTING A RIDEALONG IN SERVER."
)
embed.add_field(name="For FTOs", value="Please react to this message to accept the Ride-Along.", inline=False)
embed.timestamp = datetime.utcnow()
embed.set_footer(text= "This message will automatically delete in two hours")
msg = await ctx.send(embed=embed, delete_after=7200)
await msg.add_reaction('✔️')
await msg.add_reaction('❌')
await ctx.message.delete()

enter image description here

1 个答案:

答案 0 :(得分:1)

您可以使用不同的事件。大多数情况下我们wait_forreaction_add

我个人使用以下内容:

try:
    reaction, user = await client.wait_for('reaction_add')
    while user == client.user:
        reaction, user = await client.wait_for('reaction_add')
    if str(reaction.emoji) == "YourEmoji":
        # Do what you want to do
except Exception: # Timeout can be added for example
    return

在这里,我们检查机器人反应是否不算作有效反应,而是算作用户反应。

完整代码如下:

@client.command()
async def ra(ctx):
    embed = discord.Embed(
        colour=discord.Colour.blue(),
        title="RIDEALONG REQUEST",
        description=str(ctx.author.mention) + " IS REQUESTING A RIDEALONG IN SERVER.")
    embed.add_field(name="For FTOs", value="Please react to this message to accept the Ride-Along.", inline=False)
    embed.timestamp = datetime.datetime.utcnow()
    embed.set_footer(text="This message will automatically delete in two hours")
    msg = await ctx.send(embed=embed, delete_after=7200)
    await msg.add_reaction('✔️')
    await msg.add_reaction('❌')
    await ctx.message.delete()
    try:
        reaction, user = await client.wait_for('reaction_add')
        while user == client.user:
            reaction, user = await client.wait_for('reaction_add')
        if str(reaction.emoji) == "✔️":
            # Do what you want to do
    except Exception:
        return