Home > Software design >  discord.py wait_for() bot responds to itself
discord.py wait_for() bot responds to itself

Time:11-22

When I'm using wait_for() in my discord bot, sometimes it will immediately take in its own message as a response

def check_instant_return(m):
    return m.content

@client.event
async def on_message(message):
  if message.author == client.user:
    return  

  if message.content.startswith('!example'):
    await message.channel.send("This is an example. Say 'y'")

    msg = await client.wait_for('message', check=check_instant_return)

    # the issue is that sometimes msg.content = "This is an example. Say 'y" instead of the real user input

    if msg.content == "y":
      await message.channel.send("Alright")

CodePudding user response:

@client.event
async def on_message(message):
  if message.author == client.user:
    return  

  if message.content.startswith('!example'):
    await message.channel.send("This is an example. Say 'y'")

    msg = await client.wait_for('message', check=lambda mess: mess.author == message.author and mess.channel == message.channel)

    # the issue is that sometimes msg.content = "This is an example. Say 'y" instead of the real user input

    if msg.content == "y":
      await message.channel.send("Alright")

Your check was useless, do it like this to check the author of your message.

CodePudding user response:

Not sure why this is happening exactly, maybe a mistiming in the execution of commands on discord py's side. May I suggest, as a crude, temporary fix, you try adding a time.sleep before waiting for a message.

import time # top of the file

@client.event
async def on_message(message):
  ...
  if message.content.startswith('!example'):
    await message.channel.send("This is an example. Say 'y'")

    time.sleep(0.5)

    msg = await client.wait_for('message', check=check_instant_return)

    ...

CodePudding user response:

Tell the bot not to respond to itself(respond to anyone but itself)

  • Related