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?
JavaScript
x
25
25
1
@client.command()
2
async def game(ctx):
3
4
chosen_image = random.choice(embedlinks.bnhaLinks)
5
channel = ctx.channel
6
7
index = embedlinks.bnhaLinks.index(chosen_image)
8
embed = discord.Embed(color=0x999999)
9
embed.set_image(url=chosen_image)
10
embed.set_footer(text=f"You have 30 seconds to guess this MHA character")
11
12
await ctx.send(embed=embed)
13
14
def check(msg):
15
return msg.author == ctx.author and msg.channel == ctx.channel
16
17
18
user_guess = (await client.wait_for('message', check=check)).content
19
20
if user_guess == embedlinks.bnhaChars[index]:
21
await ctx.send('Correct. Good job.')
22
elif user_guess is not embedlinks.bnhaChars[index]:
23
await ctx.send('Wrong. Start a new game.')```
24
25
Advertisement
Answer
You should add timeout = seconds
criteria in wait_for
JavaScript
1
29
29
1
@client.command()
2
async def game(ctx):
3
4
chosen_image = random.choice(embedlinks.bnhaLinks)
5
channel = ctx.channel
6
7
index = embedlinks.bnhaLinks.index(chosen_image)
8
embed = discord.Embed(color=0x999999)
9
embed.set_image(url=chosen_image)
10
embed.set_footer(text=f"You have 30 seconds to guess this MHA character")
11
12
await ctx.send(embed=embed)
13
14
def check(msg):
15
return msg.author == ctx.author and msg.channel == ctx.channel
16
17
18
user_guess = (await client.wait_for(timeout = 30, 'message', check=check)).content
19
if user_guess == embedlinks.bnhaChars[index]:
20
await ctx.send('Correct. Good job.')
21
elif user_guess is not embedlinks.bnhaChars[index]: #put this if you want user to guess only once and not multiple times
22
await ctx.send('Wrong. Start a new game.')```
23
24
25
@game.error
26
async def on_error(self, ctx, error):
27
if isinstance(error, commands.CommandInvokeError):
28
await ctx.send("Time is up!")
29