QWERTY
QWERTY

Reputation: 33

Discord.py Make the bot send something when wait_for answer is wrong

I want to make my bot send a message when the wait_for answer is wrong

@client.event
async def on_message(message):
    if message.content.startswith('$greet'):
        channel = message.channel
        await channel.send('Say hello!')

        def check(m):
            return m.content == 'hello' and m.channel == channel

        msg = await client.wait_for('message', check=check)
        await channel.send('Hello {.author}!'.format(msg))

So when the user answer is not hello then the bot should send a message too

How to make this?

Upvotes: 2

Views: 615

Answers (1)

Bagle
Bagle

Reputation: 2346

Instead of putting your m.content in your check function itself, you can instead call it outside of this using msg.content. This is because the variable msg is still a message object. Do view the revised code below.

def check(m):
    # we won't check the content here...
    return m.author == message.author and m.channel == message.channel 
    
msg = await client.wait_for('message', check=check)

# ...instead we will check the content using an if-else statement
if msg.content == 'hello':
    await message.channel.send("Hello {.author}!".format(msg))
else:
    await message.channel.send("You did not say hello...")

Some helpful documentation:

Upvotes: 2

Related Questions