繁体   English   中英

Discord.py 嵌入消息用户反应

[英]Discord.py embeded message user reactions

我一直试图解决这个问题几天没有解决方案。 我希望我的 Discord 机器人能够购买数字物品,并且购买它们我已经制作了一条确认消息和一个系统,您可以使用我在网上找到的代码在 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("✅") #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

提前致谢,诺埃尔。

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

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

参考:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM