Skip to content
Advertisement

How to add timeout, python discord bot

I made a guess the character game in discord bot, (see the code below). I want to add 30 second timeout for a player to response, but I totally dont know what to do, any help?

@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.')```

Advertisement

Answer

You should add timeout = seconds criteria in wait_for

@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!")
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement