简体   繁体   中英

How to get user input in discord.py

@client.command()
async def blackjack(ctx):
  y= random.choice(deck)
  x=random.choice(deck)
  z=random.choice(deck)
  a=random.choice(deck)
  await ctx.send(f'{x, y}')
  await ctx.send(f'{a, z}')
  m = await client.wait_for("message", check=None)
  
  if m == "Stand":
    B = x+y
    S = a+z
    if S < B:
      while S < B or S < 21: 
       await ctx.send(S= a+z+random.choice(deck))
      if S > 21:
        ctx.send("You win")
      else:
        ctx.send("You Lose")
    else:
      ctx.send("You Lose")

This is a snippet of my code where I'm trying to get user input on whether he wants to stand or draw another card. I have not yet implemented a draw feature as the stand feature does not seem to work. My deck is properly defined as numbers and.

I think the problem is in the user input as I'm not very familiar with input in discord.py

if the program would work it would wait for the user's response if they want to stand and draw cards until it has over 21 or has more than the player and print out whether the player has lost or won.

Any help would be appreciated

I think you didn't add the check you kept it NONE.

so you should do it like this:

@client.command()
async def blackjack(ctx):
  y= random.choice(deck)
  x=random.choice(deck)
  z=random.choice(deck)
  a=random.choice(deck)
  await ctx.send(f'{x, y}')
  await ctx.send(f'{a, z}')

  def check(msg):
    return msg.author == ctx.author and msg.channel == ctx.channel and msg.content.lower().startswith()

  msg = await client.wait_for("message", check=check)
  
  if msg.content.lower().startswith("Stand"):
    B = x+y
    S = a+z
    if S < B:
      while S < B or S < 21: 
       await ctx.send(S= a+z+random.choice(deck))
      if S > 21:
        ctx.send("You win")
      else:
        ctx.send("You Lose")
    else:
      ctx.send("You Lose")

The startswith() is purely optional but I recommend you to add it!

You can add event time out like this:

msg = await bot.wait_for("message", check=check, timeout=<anything you want in {int}>)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM