How do you collect input from discord

Viewed 38
chars = "qwertyuiopasdfghjklzxcvbnm!@#$%^&*()1234567890QWERTYUIOPASDFGHJKLZXCVBNM?/"
 while 1:

How do I collect input in discord.py. Like I want the user to type in the len of password and the number of password and save it in passCount and passLength.

passLength = int(input("What length do you want your password to be: "))
      passCount = int(input("How many passwords do you want to generate: "))
      for x in range(0,passCount):
        password = ""
        for x in range(0,passLength):
           password_char = random.choice(chars) 
           password = password + password_char
        print("Here is your password,",password)
2 Answers
@client.command()
async def gen_password(ctx, length):
    [your code here...]
    await ctx.send(f'Your generated password is {password')

you can use wait_for

Waits for a WebSocket event to be dispatched.

This could be used to wait for a user to reply to a message, or to react to a message, or to edit a message in a self-contained way.

The timeout parameter is passed onto asyncio.wait_for(). By default, it does not timeout. Note that this does propagate the asyncio.TimeoutError for you in case of timeout and is provided for ease of use.

In case the event returns multiple arguments, a tuple containing those arguments is returned instead. Please check the documentation for a list of events and their parameters.

This function returns the first event that meets the requirements.

Examples

wait_for(event, *, check=None, timeout=None)

it waits until check returns True. and you can set a timeout

Waiting for a user reply:

@client.event
async def on_message(message):
    if message.content.startswith('$greet'):
        channel = message.channel
        await channel.send('Say hello!')

        def check(m): # m = [Message Object]
            return m.content == 'hello' and m.channel == channel

        msg = await client.wait_for('message', check=check) # wait for reply
        await channel.send('Hello {.author}!'.format(msg))

Example 2

Waiting for a thumbs up reaction from the message author:

@client.event
async def on_message(message):
    if message.content.startswith('$thumb'):
        channel = message.channel
        await channel.send('Send me that  reaction, mate')

        def check(reaction, user):
            return user == message.author and str(reaction.emoji) == ''

        try:
            reaction, user = await client.wait_for('reaction_add', timeout=60.0, check=check)
        except asyncio.TimeoutError:
            await channel.send('')
        else:
            await channel.send('')

Note this code hadn't been tested


def isInt(string):
    try:
        int(string)
        return True
    except ValueError:
        return False
chars = [...]
@bot.command()
async def password(ctx):
    def check(m): # m = [Message Object]
        if isInt(m.content) and m.channel == channel and m.author.id == ctx.author:
            await ctx.send("please send a number")
            return True
        else:
            return False
    try:
        await ctx.send("How many passwords do you want to generate")
        message = await client.wait_for('message', timeout=60.0, check=check)
    except asyncio.TimeoutError:
        await channel.send('time out returning')
        return
    for x in range(0,int(message)):
        password = ""
        for x in range(0,10):
            password_char = random.choice(chars) 
            password += password_char
        print("Here is your password,",password)
Related