Home > Enterprise >  Yes or no question with python on discord
Yes or no question with python on discord

Time:07-25

I'm trying to make a Discord bot which can ask a yes-or-no question and respond to the user's answer. I tried this code:

@bot.event
async def on_message(message):
    if message.content == 'ask me a question':
        await message.channel.send('Yes or No')
        if message.content == 'yes':
            await message.channel.send('correct!')
        elif message.content == 'no':
            await message.channel.send('wrong')

The bot responds to 'ask me a question', but when I type in 'yes' or 'no' I don't get a response. Why doesn't it work, and how can I fix it?

CodePudding user response:

The problem is you don't ask it to wait for another message. Which means your variable message is still the "ask me a question" message. Intead, you should use bot.wait_for("message"), that will return the next message sent.

@bot.event
async def on_message(message):
    if message.content == 'ask me a question':
        await message.channel.send('Yes or No')

        response = await bot.wait_for("message")
        if response.content == 'yes':
            await message.channel.send('correct!')
        elif response.content == 'no':
            await message.channel.send('wrong')
  • Related