Wait_for() function doesn't work in pycord

Question:

I need a bot to wait for the user’s input (with a reaction), but even if I react to the message, it displays the error below.

import discord
import os
import random
import asyncio

testing_servers = [912361242985918464]
intents = discord.Intents.all()
bot = discord.Bot(intents=intents)

@bot.event
async def on_ready():
    print('Online!')

@bot.slash_command(guild_ids=testing_servers, name="announce", description="Make server announcements!")
async def announce(ctx,channel_id : discord.Option(str, description = "Copy the text channel in developer mode."),title:str,text : str):
    #response embed
  try:
    channel = bot.get_channel(int(channel_id))
  except ValueError:
    channel = channel_id
    #announcement embed
  embed_check = discord.Embed(
    colour = discord.Colour.blue(),
    title = "Is this embed shown correct?",
    description = title + "n" * 2 + text
    
  )
  response = await ctx.respond(embed = embed_check)
  global message_react
  message_react = await response.original_message()
  await message_react.add_reaction("")
  
  def check(r,u):
    return True
  
  try:
    user_react = await bot.wait_for("on_reaction_add", check = check, timeout = 10.0)
    #the error ^
  except asyncio.TimeoutError:
    await ctx.send("Did not respond, and timed out")
    return
  embed_announce = discord.Embed(
      colour = discord.Colour.blue(),
      title=str(title),
      description = text
  )
  await channel.send(embed = embed_announce)

  embed = discord.Embed(
      colour=discord.Colour.blue(),
      title = "Sent!",
      description= "Check the channel!"
  )

  await ctx.send(embed = embed)

I’ve tried:

  1. Reinviting the bot
  2. Giving the bot all intents
  3. Making the check function return True
  4. Changing intents in the discord dev portal

The error happens in the line before the comment #the error ^.
I want to make the bot run the stuff under the try-catch system in that comment.

If I didn’t use a try-catch system it would make this error:

Ignoring exception in command announce:
Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/commands/core.py", line 113, in wrapped
    ret = await coro(arg)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/commands/core.py", line 766, in _invoke
    await self.callback(ctx, **kwargs)
File "main.py", line 37, in announce
    user_react = await bot.wait_for("on_reaction_add", check = check, timeout = 10.0)
File "/usr/lib/python3.8/asyncio/tasks.py", line 501, in wait_for
    raise exceptions.TimeoutError() asyncio.exceptions.TimeoutError

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/bot.py", line 755, in process_application_commands
    await ctx.command.invoke(ctx)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/commands/core.py", line 312, in invoke
    await injected(ctx)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/commands/core.py", line 119, in wrapped
    raise ApplicationCommandInvokeError(exc) from exc discord.commands.errors.ApplicationCommandInvokeError: Application Command raised an exception: TimeoutError:
Asked By: Blue Robin

||

Answers:

When you put events inside wait_for you don’t put ‘on’ in front of the event, so try like this:

user_react = await bot.wait_for("reaction_add", check = check, timeout = 10.0)
Answered By: 3nws
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.