Python Discord Bot:命令运行不正常

Python Discord Bot:命令运行不正常,discord,Discord,我为我的机器人做了一行代码: @client.command(pass_context=True) async def weewoo(ctx): for _ in range(number_of_times): await client.say('example command 1') if client.wait_for_message(content='~patched'): await clie

我为我的机器人做了一行代码:

@client.command(pass_context=True)
async def weewoo(ctx):
        for _ in range(number_of_times):
            await client.say('example command 1')
            if client.wait_for_message(content='~patched'):
                await client.say('example command 2')
                break
它可以工作,但当我运行bot并键入命令时,结果如下:

example command 1
example command 2

我要做的是输入一个命令,开始发送垃圾邮件“示例命令1”,并尝试用一个命令结束垃圾邮件,然后发送一条消息,说“示例命令2”。但它却做到了这一点。如果有人能帮忙,那就是毒品。

你必须等待
客户端。等待消息。它返回一个消息对象。更好的方法是创建一个全局变量,并在循环时将其设置为true,然后在使用命令
patched
时将其设置为false。因此停止循环

checker = False

@client.command(pass_context=True)
async def weewoo(ctx):
    global checker
    checker = True

    for _ in range(number_of_times):
        await client.say('example command 1')
        if not checker:
            return await client.say('example command 2')


@client.command()
async def patched():
    global checker
    checker = False
当然,bot只会发送5条消息,然后停止,然后再继续。您可以在垃圾邮件间隔之间设置1.2秒的间隔

@client.command(pass_context=True)
async def weewoo(ctx):
    global checker
    checker = True

    for _ in range(number_of_times):
        await client.say('example command 1')
        if not checker:
            return await client.say('example command 2')

        await asyncio.sleep(1.2)

非常感谢你!我已经研究这个问题有一段时间了:)如果它对您有用,您可以单击复选标记将答案标记为“已接受”@用户8328934