[英]Discord.py: how do i make the bot get a list of users who reacted to a message in a given time
i am making a discord.py bot on python 3.8.6.我正在 python 3.8.6 上制作一个 discord.py 机器人。
i want this particular function to work in the specific way...我希望这个特定的 function 以特定的方式工作......
message = await ctx.send(embed=e)
message = await ctx.send(embed=e)
await message.add_reaction('✅')
await message.add_reaction('✅')
添加一个反应code-代码-
@bot.command()
async def command_name(ctx, function):
#code for function 1
#code for function 2
# making the embed
message = await ctx.send(embed=e)
await message.add_reaction('✅')
def check(reaction, user):
if user not in players and not user.bot:
players.append(user.mention)
return reaction.message == message and str(reaction.emoji) == '✅'
try:
await bot.wait_for('reaction_add', timeout=10, check=check)
except asyncio.TimeoutError:
await ctx.send('time is up, and not enough players')
else:
await ctx.send(players)
# further code
issue: the bot instantly sends the list players问题:机器人立即发送玩家列表
question: what can i add for it to wait 30 seconds, append the list with users who react to the embed and then send the list and if after 30 seconds len(players) isn't 3 or more send -问题:我可以添加什么让它等待 30 秒,append 列表与对嵌入做出反应的用户然后发送列表,如果 30 秒后 len(players) 不是 3 或更多发送 -
await ctx.send('time is up, and not enough players')
The problem is that await bot.wait_for('reaction_add', timeout=10, check=check)
proceeds at the first reaction made.问题是
await bot.wait_for('reaction_add', timeout=10, check=check)
在做出第一个反应时继续进行。 In this case, the bot reaction instantly triggers this event, so an empty list will be returned.在这种情况下,机器人反应会立即触发此事件,因此将返回一个空列表。
I thought of two options:我想到了两个选择:
First option:第一个选项:
@client.command()
async def command_name(ctx):
players = []
message = await ctx.send("Message")
await message.add_reaction('✅')
def check(reaction, user):
if user not in players and not user.bot:
players.append(user.mention)
return False
try:
await client.wait_for('reaction_add', timeout=30, check=check)
except asyncio.TimeoutError:
pass
if len(players) < 3:
await ctx.send('Time is up, and not enough players')
else:
await ctx.send(players)
Second (preferred) option:第二个(首选)选项:
@client.command()
async def command_name(ctx):
players = []
message = await ctx.send("Message")
await message.add_reaction('✅')
await asyncio.sleep(30)
message = await ctx.fetch_message(message.id)
for reaction in message.reactions:
if reaction.emoji == '✅':
async for user in reaction.users():
if user != client.user:
players.append(user.mention)
if len(players) < 3:
await ctx.send('Time is up, and not enough players')
else:
await ctx.send(players)
References to the API :参考API :
fetch_message 获取消息
reaction.users() 反应.users()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.