【发布时间】:2021-07-21 21:40:59
【问题描述】:
@client.command(name="start")
async def start(maid_cafe):
x = len(images)
if x == 0:
await maid_cafe.send("0 images cached,\nTry !cache first.")
return
loop_run = 1;
while loop_run < 60:
if not halt:
image = random.choice(images)
await asyncio.sleep(10)
maid_cafe = client.get_channel(773213667801169930)
await maid_cafe.send(file=discord.File(f"{image}"))
else:
break
在这里需要一些帮助,有点在 python 上的循环中苦苦挣扎。
这里的问题描述如下。我已在代码顶部将停止定义为全局
# Misc.
global halt
global images
images = []
halt = True
我有另一个函数,如果我触发整个循环中断,它就在我在这里首先列出的函数的正下方
@client.command(name="halt")
async def halt(ctx):
halt = True
await ctx.send("Send loop coming to a stop.")
现在,当我为要执行的函数执行某个短语时,它不会在我的不和谐通道上显示任何内容,也不会在运行窗口上打印出任何错误。但是当我删除
if not halt:
...
...
...
...
else:
break
机器人完美地在频道上显示消息,没有任何问题,但代价是我无法停止循环并且它会继续持续......
基本上我在这里想要实现的是是否可以创建一个单独的函数来打破循环。
任何帮助将不胜感激!
【问题讨论】:
标签: python discord discord.py