python-3.x - 新命令中断循环

标签 python-3.x discord discord.py

我有一个控制一些 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/

相关文章:

python - 如何为特定用户打开一个窗口 discord.py

python-3.x - 在不下载文件的情况下使用 Youtube 的机器人播放音乐

python - Pythonanywhere 上 flask 中 send_file() 的替代方案?

python - 如何安装 discord.py 重写?

java - 我如何在 JDA 中的不同游戏状态消息之间循环?

python - 有没有办法在嵌入之前将用户 ID 转换为用户名?

python - Python 中的 Tkinter - 它应该很慢还是程序中其他地方的瓶颈?

python - 使用 Python 刷新 Tableau 仪表板

python-3.x - python : only two decimal places after user entered number

discord - 如何获得在线成员(member)数量?