首页 > 解决方案 > 如何在结束命令之前使命令循环一定次数?

问题描述

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

我想用这个代码来做 -

提前致谢!

标签: pythondiscorddiscord.py

解决方案


更新

# 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


推荐阅读