CraftSpider / dpytest

A package that assists in writing tests for discord.py
MIT License
103 stars 25 forks source link

Support reaction type #129

Open alkshmir opened 3 months ago

alkshmir commented 3 months ago

Is your feature request related to a problem? Please describe.

discord.py 2.4.0 adds the support for reaction type (https://github.com/Rapptz/discord.py/pull/9836) and this breaks the existing tests including the add_reaction(). Specifically, it raises KeyError: 'type' when running test:

ERROR    discord.client:client.py:523 Ignoring exception in on_message
Traceback (most recent call last):
  File "/Users/user/work/mybot/.venv/lib/python3.12/site-packages/discord/client.py", line 449, in _run_event
    await coro(*args, **kwargs)
  File "/Users/user/work/mybot/src/cogs/songlink.py", line 54, in convert_apple_music_link
    await message.add_reaction(
  File "/Users/user/work/mybot/.venv/lib/python3.12/site-packages/discord/message.py", line 1258, in add_reaction
    await self._state.http.add_reaction(self.channel.id, self.id, emoji)
  File "/Users/user/work/mybot/.venv/lib/python3.12/site-packages/discord/ext/test/backend.py", line 243, in add_reaction
    add_reaction(message, user, emoji)
  File "/Users/user/work/mybot/.venv/lib/python3.12/site-packages/discord/ext/test/backend.py", line 983, in add_reaction
    state.parse_message_reaction_add(data)
  File "/Users/user/work/mybot/.venv/lib/python3.12/site-packages/discord/state.py", line 714, in parse_message_reaction_add
    raw = RawReactionActionEvent(data, emoji, 'REACTION_ADD')
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/user/work/mybot/.venv/lib/python3.12/site-packages/discord/raw_models.py", line 254, in __init__
    self.type: ReactionType = try_enum(ReactionType, data['type'])
                                                     ~~~~^^^^^^^^
KeyError: 'type'

Describe the solution you'd like

When I add type field in backend.py::add_reaction() the tests are successful.

    data = {
        "message_id": message.id,
        "channel_id": message.channel.id,
        "user_id": user.id,
        "emoji": emoji,
        "type": discord.enums.ReactionType.normal,  # add
    }

https://github.com/CraftSpider/dpytest/blob/61cd0fb73359c727a458aa615276cdec4b66cd9e/discord/ext/test/backend.py#L969-L974

Describe alternatives you've considered

I'm ready to open PR but I don't fully understand how can I add test for this issue yet.

Additional context

I'm using discord.py 2.4.0 and dpytest 0.7.0.

It seems reaction type has been added in this commit: https://github.com/Rapptz/discord.py/commit/f77ba711ba21bc2f70b252134880ebac3856acf8

Sergeileduc commented 3 months ago

OK ! thanks !!

I'll try to implement this ASAP !