简体   繁体   English

Discord.py:在用户使用命令后,我如何从用户那里获取输入?

[英]Discord.py: How would I get the input from the user after they use a command?

I am making a higher lower game in my bot but I can't figure out how to get the user's input after they enter in the command to start the game.我在我的机器人中制作了一个更高更低的游戏,但我不知道在用户输入命令开始游戏后如何获取用户的输入。 How would I do this?我该怎么做?

@client.command(aliases=['highlow'])
async def higherlower(ctx):
    random_number = randint(1, 25)
    guess = 0
    tries = 5

    # code here that detects user input

    while guess != random_number:
        if tries == 0:
            await ctx.send('You're out of tries! Try again.)
            break
        if guess != random_number:
            tries = tries -1
            await ctx.send('Incorrect!' + tries + ' tries left.')
        if guess == random_number:
            await ctx.send('You got it! The number was ' + random_number)
            break
    

Okay, so you need to use the while True and client.wait_for parts of discord.py:好的,所以你需要使用 discord.py 的while Trueclient.wait_for部分:

@client.command(aliases=['highlow'])
async def higherlower(ctx):
    random_number = randint(1, 25)
    tries = 5

    while True:
        guess_message = await client.wait_for('message')

        if guess_message.author == ctx.message.author:
            guess = int(guess_message.content)
            if tries == 0:
                await ctx.send('You're out of tries! Try again.)
                break
            if guess != random_number:
                tries = tries -1
                await ctx.send('Incorrect!' + tries + ' tries left.')
            if guess == random_number:
                await ctx.send('You got it! The number was ' + random_number)
                break

I would do it like this:我会这样做:

(Make a new function that initializes the global variables, while the "higherlower" function is reserved for guessing) (制作一个新的 function 初始化全局变量,而“higherlower” function 保留用于猜测)

random_number = 0
guess = 0
tries = 0

@client.command()
async def start_higherlower(ctx):
    global random_number, guess, tries

    random_number = randint(1, 25)
    guess = 0
    tries = 5

@client.command(aliases=['highlow'])
async def higherlower(ctx):
    global random_number, guess, tries

    if tries == 0:
        await ctx.send('You're out of tries! Try again.)
        #End_higherlower()
    elif guess == random_number:
        await ctx.send('You got it! The number was ' + random_number)
        #End_higherlower()
    else:
        tries = tries -1
        await ctx.send('Incorrect!' + tries + ' tries left.')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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