[英]How to add timeout, python discord bot
我猜了 discord 機器人中的角色游戲,(見下面的代碼)。 我想為玩家添加 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!")
如果您的意思是冷卻時間,那么已經有一個很好的問答了。
基本上,您所要做的就是稍微修改命令的開頭部分。 Append @commands.cooldown(1, 30, commands.BucketType.user)
@client.command()
之后的行。 有關更多信息,請查看上面的鏈接。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.