如何添加超时,python discord bot



我猜了一个角色游戏在不和谐bot,(见下面的代码)。我想添加30秒的超时为球员的反应,但我完全不知道该怎么做,任何帮助?

@client.command()
async def game(ctx):
chosen_image = random.choice(embedlinks.bnhaLinks)
channel = ctx.channel
index = embedlinks.bnhaLinks.index(chosen_image)
embed = discord.Embed(color=0x999999)
embed.set_image(url=chosen_image)
embed.set_footer(text=f"You have 30 seconds to guess this MHA character")
await ctx.send(embed=embed)
def check(msg):
return msg.author == ctx.author and msg.channel == ctx.channel


user_guess = (await client.wait_for('message', check=check)).content

if user_guess == embedlinks.bnhaChars[index]:
await ctx.send('Correct. Good job.')
elif user_guess is not embedlinks.bnhaChars[index]:
await ctx.send('Wrong. Start a new game.')```

您应该在wait_for中添加timeout = seconds标准

@client.command()
async def game(ctx):
chosen_image = random.choice(embedlinks.bnhaLinks)
channel = ctx.channel
index = embedlinks.bnhaLinks.index(chosen_image)
embed = discord.Embed(color=0x999999)
embed.set_image(url=chosen_image)
embed.set_footer(text=f"You have 30 seconds to guess this MHA character")
await ctx.send(embed=embed)
def check(msg):
return msg.author == ctx.author and msg.channel == ctx.channel

user_guess = (await client.wait_for(timeout = 30, 'message', check=check)).content
if user_guess == embedlinks.bnhaChars[index]:
await ctx.send('Correct. Good job.')
elif user_guess is not embedlinks.bnhaChars[index]:  #put this if you want user to guess only once and not multiple times
await ctx.send('Wrong. Start a new game.')```

@game.error
async def on_error(self, ctx, error):
if isinstance(error, commands.CommandInvokeError):
await ctx.send("Time is up!")

如果你指的是冷却时间,那么这已经是一个很好的问题和答案了。

基本上,您所要做的就是稍微修改命令的开头部分。把@commands.cooldown(1, 30, commands.BucketType.user)加在@client.command()后面一行。更多信息,请点击上面的链接。

最新更新