user8328934
user8328934

Reputation: 11

Python Discord Bot: Command not properly functioning

So pretty much whats going on is I made this line of code for my bot:

@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

And it works BUT when I run the bot and type the command it comes out like this:

example command 1
example command 2

And what I am trying to do is enter a command that starts spamming 'example command 1' and trying to end the spam with a command and it sending a message saying 'example command 2'. But instead it does that. If anyone could help that'd be dope.

Upvotes: 0

Views: 150

Answers (1)

Wright
Wright

Reputation: 3424

You have to await the client.wait_for_message. It returns a message object. A better approach would be to create a global variable and set it to true when it loops, then false when the command patched is used. Hence stopping the loop.

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

However of course, the bot would only send 5 messages and then stops and then continues again. You can put a 1.2 second interval between the intervals of the spam.

@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)

Upvotes: 1

Related Questions