如何添加超时、PYTHON不和机器人 [英] How to add timeout, python discord bot

查看:0
本文介绍了如何添加超时、PYTHON不和机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我猜了一下不和谐机器人中的角色游戏(见下面的代码)。 我想给球员增加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!")

这篇关于如何添加超时、PYTHON不和机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆