我已经尝试解决这个问题好几天了,但没有解决。我希望我的 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
提前致谢, 诺埃尔。
答案 0 :(得分:2)
要使此消息具体化,只需在 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
参考: