首页 > 解决方案 > Discord.py 嵌入消息用户反应

问题描述

这几天我一直在尝试解决这个问题,但没有解决方案。我希望我的不和谐机器人能够购买数字商品,并且为了购买它们,我已经发送了一条确认消息和一个系统,您可以在该系统中使用我在网上找到的代码以 60 秒超时进行确认。

该代码的问题在于它没有考虑用户反应的消息,因此例如,如果用户发送两条消息并确认一条消息,它们都会被执行,而且我不能添加另一个图标,例如取消图标。这非常重要,因为这是机器人工作的重要功能。

此外,制作 on_reaction_add 函数是不可行的,因为我希望我的机器人同时在不同的服务器和通道中运行,不同的用户发送命令。

@client.event
async def on_message(message):
        channel = message.channel
        embedVar2 = discord.Embed(title="Item buying", description="<@"+ str(message.author.id) +">  Are you sure you want to buy " + str(conditions[1])  + " quantity of " + str(conditions[0]), color = 0x9A9B9D)
        embedVar2.add_field(name="\u200b", value= "It will cost $" + str(price) + " you will have $" + str(endbal) + " remaining \nReact with ✅ to confirm", inline=False)
        await confirm_message.add_reaction("✅&quot;) #add a reaction for the user to understand he has to react
        def check(reaction, user):  #checks if user has reacted
            return user == message.author and str(reaction.emoji) == '✅'
        try: #waits if it doest get timeout error
            reaction, user = await client.wait_for('reaction_add', timeout=60.0, check=check) #calls for function check defined prev
        except asyncio.TimeoutError: #if timeout throws error:
            embedVar5 = discord.Embed(title="Money transfer", description="<@"+ str(message.author.id) +">  Are you sure you want to buy " + str(conditions[1])  + " quantity of " + str(conditions[0]), color = 0xFF0A26)
            embedVar5.add_field(name="\u200b", value= "It would have cost $" + str(price) + " you would have had $" + str(endbal) + " remaining \nTransaction canceled", inline=False)
            await confirm_message.edit(embed = embedVar5) #message edit to canceled
            return
        else: #if user reacted before timeout
            embedVar4 = discord.Embed(title="Money transfer", description="<@"+ str(message.author.id) +">  Are you sure you want to buy " + str(conditions[1])  + " quantity of " + str(conditions[0]), color = 0x77FF87)
            embedVar4.add_field(name="\u200b", value= "It will cost $" + str(price) + " you now have $" + str(endbal) + " remaining \nTransaction confirmed", inline=False)
            await confirm_message.edit(embed = embedVar4)   #message edit to confirmed

在此先感谢,诺埃尔。

标签: pythonpython-3.xdiscorddiscord.py

解决方案


要使此消息具体化,只需and reaction.message == message添加check

def check(reaction, user):  #checks if user has reacted
    return user == message.author and str(reaction.emoji) == '✅' and reaction.message == message

如果你还想有一个取消反应,你应该修改check为也允许取消反应:

def check(reaction, user):  #checks if user has reacted
    return user == message.author and str(reaction.emoji) in ['✅','Your cancel reaction here'] and reaction.message == confirm_message

然后检查用户反应的反应,并根据使用的反应做一些事情:

        try:
            reaction, user = await client.wait_for('reaction_add', timeout=60.0, check=check) #calls for function check defined prev
        except asyncio.TimeoutError:
            embedVar5 = discord.Embed(title="Money transfer", description="<@"+ str(message.author.id) +">  Are you sure you want to buy " + str(conditions[1])  + " quantity of " + str(conditions[0]), color = 0xFF0A26)
            embedVar5.add_field(name="\u200b", value= "It would have cost $" + str(price) + " you would have had $" + str(endbal) + " remaining \nTransaction canceled", inline=False)
            await confirm_message.edit(embed = embedVar5) 
            return
        else:
        if str(reaction.emoji) == '✅':
            embedVar4 = discord.Embed(title="Money transfer", description="<@"+ str(message.author.id) +">  Are you sure you want to buy " + str(conditions[1])  + " quantity of " + str(conditions[0]), color = 0x77FF87)
            embedVar4.add_field(name="\u200b", value= "It will cost $" + str(price) + " you now have $" + str(endbal) + " remaining \nTransaction confirmed", inline=False)
            await confirm_message.edit(embed = embedVar4) 
        elif str(reaction.emoji) == 'Your cancel emoji here':
            await confirm_message.edit(embed = embedVar5) #execute the same code you executed on TimeoutError
            return

参考:


推荐阅读