简体   繁体   English

如何在结束命令之前使命令循环一定次数?

[英]How do I make a command loop a certain number of times before making it end the command?

With the following code, I would like to make it loop 2 times before ending, to prevent spam in chat.使用以下代码,我想在结束前循环 2 次,以防止聊天中出现垃圾邮件。

while answer.lower() != "proceed" and answer.lower() != "return":
    await ctx.send("Only enter 'proceed' 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

I would like to do it with this code-我想用这个代码来做 -

Thanks in advance!提前致谢!

Update更新

# use a counter to track how many times you run the while loop
loop_counter = 0
while answer.lower() != "proceed" and answer.lower() != "return": 
    loop_counter += 1
    if loop_counter >=2:
    # break out of the loop if reach a threshold.
    # send out the message before break out of the loop.
        await ctx.send("The loop has ended.") 
        break
    await ctx.send("Only enter 'proceed' 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

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

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