Python Discord Bot:命令无法正常运行

Python Discord Bot: Command not properly functioning

我为我的机器人编写了这行代码:

@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

它可以工作,但是当我 运行 机器人并输入命令时,它会像这样出现:

example command 1
example command 2

我想做的是输入一个开始发送垃圾邮件的命令 'example command 1' 并尝试用一个命令结束垃圾邮件并发送一条消息说 'example command 2'。但它却这样做了。如果有人可以提供帮助,那就太棒了。

你必须 await client.wait_for_message。它 returns 一个消息对象。更好的方法是创建一个全局变量并在循环时将其设置为 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

然而,当然,机器人只会发送 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)