如何使命令运行,但随后在命令中使其等待用户输入?

时间:2020-08-27 12:01:36

标签: python discord discord.py

@client.command()
@commands.has_permissions(administrator=True)
async def nuke(ctx):
    await ctx.send('''Are you sure you want to nuke this channel? This will completely erase all messages from it!
type proceed to continue, and return to return. ''')

我希望它能够使用户从此命令中输入并锁定在该命令中,直到他们输入有效的选择为止。 (例如dank Memer的搜索命令)。可以帮忙吗?

预先感谢

2 个答案:

答案 0 :(得分:1)

这应该可以帮助您...它会一直等到您输入“继续”或60秒的时间用完。

@commands.has_permissions(administrator=True)
async def nuke(ctx):

    def check(message):
        return message.author == ctx.author and message.channel == ctx.channel and message.content.lower() == "proceed"

    try:

        await ctx.send(
            f'Are you sure you want to nuke this channel? \n This will completely erase messages from it! \n'
            'type __proceed__ to continue, and return to return.')
        await client.wait_for('message', check=check, timeout=60)
        # You can now fill this with your action

    except asyncio.TimeoutError:
        #this will react if you no react in time
        await ctx.send('You took to long')
        await asyncio.sleep(10)
        await ctx.channel.purge(limit=3)

答案 1 :(得分:1)

您可以使用discord.Client.wait_for来获取用户输入,这是一个示例:

@client.command()
@commands.has_permissions(administrator=True)
async def nuke(ctx):
    await ctx.send('''Are you sure you want to nuke this channel? This will completely erase all messages from it!
type proceed to continue, and return to return. ''')
    answer = await client.wait_for('message', check=lambda message: message.author == ctx.author and message != "") # Gets user input and checks if message is not empty and was sent by the same user
    answer = answer.content # Gets content of message

    while answer.lower() != "continue" and answer.lower() != "return": # Loop until user enters a correct answer
        await ctx.send("Only enter 'continue' or 'return'!")
        await ctx.send('''Are you sure you want to nuke this channel? This will completely erase all messages from it!
type proceed to continue, and return to return. ''')
        answer = await client.wait_for('message', check=lambda message: message.author == ctx.author and message != "") # Gets user input and checks if message is not empty and was sent by the same user
        answer = answer.content # Gets content of message
    
    if answer.lower() == "continue":
        # Do something if user chooses 'continue'
    elif answer.lower() == "return":
        # Do something if user chooses 'return'

您也可以在不使用while循环的情况下执行此操作,而是可以调整检查以查看消息是否包含“继续”或“返回”,例如:

@client.command()
async def nuke(ctx):
    await ctx.send('''Are you sure you want to nuke this channel? This will completely erase all messages from it!
type proceed to continue, and return to return. ''')

    answer = await client.wait_for('message', check=lambda message: message.author == ctx.author and message != "" and (message.content.lower() == "continue" or message.content.lower() == "return"))
    answer = answer.content
    
    if answer.lower() == "continue":
        # Do something if user chooses 'continue'
    elif answer.lower() == "return":
        # Do something if user chooses 'return'