Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reaction Event Not Dispatching? #68

Open
ASingleToaster opened this issue Dec 22, 2021 · 0 comments
Open

Reaction Event Not Dispatching? #68

ASingleToaster opened this issue Dec 22, 2021 · 0 comments

Comments

@ASingleToaster
Copy link

Hello!

I'm trying to test that my bot properly listens for reactions and I can't seem to get it to receive the reaction_add event. I'm not sure if this is an issue with my testing setup or is related to the open support for reactions issue - if this is related and will be addressed in the future, please feel free to close this.

What I'm attempting to test is that following a command, my bot will send back some confirmation messages, and then use the wait_for method to get confirmation for the user that the command was interpreted properly via a reaction (the command has people pass strings that need to be encased by "s so I wanted to err on the side of caution to make sure the string parsing was correct). I'm relatively new to asyncio and the discord api at large, so it's possible that there's something wrong with my test setup. I did some other asyncio testing for the bot using dpytest and didn't have any problems - the fixture I'm using to set up my client works fine for my other tests & is able to completely mock discord's behavior (it can send messages back and forth) and I have all intents set to true. The working tests, however, don't use the wait_for command or have any reactions. Additionally, I live tested the bot and it was able to pick up a reaction I added; I can mock the wait_for temporarily for testing purposes, but if possible I'd like to have it mock the behavior instead of stubbing out the call.

I've tried several different methods using dpytest - ranging from trying to use the runner add reaction method to manually dispatching the event using the backend and callback methods. My test looks something like:

import discord.ext.test as dis_test
... other imports ...

@pytest.mark.asyncio
async def test_good_command(<fixture_which_does_the_setup_of_the_client>): 

	client, guild, channel, member_list = fixture_which_does_the_setup_of_the_client
	
	# Check to make sure the user that we're sending the message as isn't the bot itself.
	if  not member_list[0].bot:
		message_user = member_list[0]
	else:
		message_user = member_list[1]

	# Here what happens is I send a command in a fake channel, read in and check the messages that the bot 
	#	has sent, and make some assertions that the correct messages were recieved. 

	# Here's where I try to add the reaction, only one method is used in the actual test. 

	# Using runner add reaction: 
	await dis_test.add_reaction(user=message_user, message=bot_message, emoji="👍")

	# Trying to manually add the reaction and dispatch the event:
	dis_test.backend.add_reaction(user=message_user, message=bot_message, emoji="👍")
	await dis_test.callbacks.dispatch_event("reaction_add", user=message_user, reaction="👍")
	await dis_test.run_all_events()

	# Afterwards I make more assertions

For more context, here's what the wait_for looks like from the bot's end:

... this is all wrapped in a command ...

# Wait for an answer back from the user
def  check_reaction(reaction, user):
	# Checks that the person who is reacting is who sent the original command, the reaction was to the last 	
	# 	message the bot sent, and the emoji is acceptable
	return command_invoker == user and reaction.message == last_message and reaction.emoji in ["👍", "👎"]

try:
	reaction, _ =  await  self.bot_client.wait_for("reaction_add", timeout=MAX_WAIT_TIME, check=check_reaction)
except asyncio.TimeoutError:
	await message_channel.send("Request Timeout")
	return

... do some other stuff based on the recieved reaction ...

Within the test it fails to recognize the reaction was added and I get back the request timeout message from the bot. If something is failing within the bot or dpytest itself it's doing so silently and not producing a stacktrace that I can use for debug. I have the timeout for testing set to 30 seconds, which I think should be long enough for the test to add a reaction. As far as I can tell it never performs the check function - I've added some print statements locally that confirm this. When I live tested it the print statement within the check_reaction function showed up in the terminal whereas during the test run it never does - which led me to think that the event just isn't being picked up.

Any guidance on how to fix this issue or on what to look for would be appreciated. :)

Also thanks for making this library - it's awesome and I've found it largely very helpful and useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant