如何在 while 循环中使用 if 语句

How to use a if statement inside of a while loop

我目前正在使用一个不和谐的机器人,我试图让它连续说 E 但当有人说 *E 停止时停止。但是,就我目前的情况而言,它不起作用。有人知道为什么吗?

E = False

if message.content.startswith('*E start'):
    E = True
    await message.channel.send('E is beginning!!!')
    while E == True:
       time.sleep(1)
       await message.channel.send('E')
       if message.content.startswith('*E stop'):
            E = False

在使用 discord 库时,不可能在一段时间内有一个 message.content.startswith。您要找的是multiprocessing。您需要启动新进程,然后在有人说 *E stop 时将其终止。像这样(未测试):

from multiprocessing import Process, Pipe

def sendE(E):
    while E == True:
        time.sleep(1)
        await message.channel.send('E')

E = False
p = type(Process)

if message.content.startswith('*E start'):
    E = True
    await message.channel.send('E is beginning!!!')
    parent_conn, child_conn = Pipe()
    p = Process(target=SendE,args=E)
    p.start()
elif message.content.startswith('*E stop'):
    E = False
    p.kill()