我有一个控制一些 RGB 灯的不和谐机器人。我想要一个重复的模式。但是,我需要在输入任何新命令后立即中断循环。
@client.command()
async def rainbow(ctx):
await ctx.send("It is rainbow")
while True:
rainbow_cycle(0.001)
我知道虽然真正的循环无法被打破,但我不知道循环此函数的另一种方法。如果需要完整代码,请访问 Github 链接 https://github.com/MichaelMediaGroup/Discord_controlled_lights/blob/main/discord/main.py
感谢您的帮助
最佳答案
我认为这不是最好的选择,但它应该有效:
您可以为循环创建一个新的全局值,如下所示:
loop = False;
@client.command()
async def rainbow(ctx):
await ctx.send("It is rainbow")
global loop
loop = True
while loop:
rainbow_cycle(0.001)
@client.command()
async def anothercommand(ctx):
global loop
loop = False
#Some other stuff here
关于python-3.x - 新命令中断循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70260979/