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



使用以下代码,我想让它在结束前循环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

我想用这个代码做它-

提前感谢!

更新

# 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

最新更新