close

How can I get user input in a python discord bot?

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How can I get user input in a python discord bot in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How can I get user input in a python discord bot?

  1. How can I get user input in a python discord bot?

    You'll be wanting to use Client.wait_for():
    @client.command(name="command") async def _command(ctx): global times_used await ctx.send(f"y or n")

  2. can I get user input in a python discord bot

    You'll be wanting to use Client.wait_for():
    @client.command(name="command") async def _command(ctx): global times_used await ctx.send(f"y or n")

Method 1

You’ll be wanting to use Client.wait_for():

@client.command(name="command")
async def _command(ctx):
    global times_used
    await ctx.send(f"y or n")

    # This will make sure that the response will only be registered if the following
    # conditions are met:
    def check(msg):
        return msg.author == ctx.author and msg.channel == ctx.channel and \
        msg.content.lower() in ["y", "n"]

    msg = await client.wait_for("message", check=check)
    if msg.content.lower() == "y":
        await ctx.send("You said yes!")
    else:
        await ctx.send("You said no!")

    times_used = times_used + 1

And with a timeout:

import asyncio # To get the exception

@client.command(...)
async def _command(ctx):
    # code
    try:
        msg = await client.wait_for("message", check=check, timeout=30) # 30 seconds to reply
    except asyncio.TimeoutError:
        await ctx.send("Sorry, you didn't reply in time!")

Method 2

With this you can make something like this

@client.command()
async def command(ctx):
    computer = random.randint(1, 10)
    await ctx.send('Guess my number')

    def check(msg):
        return msg.author == ctx.author and msg.channel == ctx.channel and int(msg.content) in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

    msg = await client.wait_for("message", check=check)

    if int(msg.content) == computer:
        await ctx.send("Correct")
    else:
        await ctx.send(f"Nope it was {computer}")

Summery

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read