Skip to content

Conversation

@Damego
Copy link
Member

@Damego Damego commented Dec 15, 2022

About

There are cases when unknown enum value brokes whole bot.

For example:

Websocket have raised an exception, closing.
Traceback (most recent call last):
  File "D:\python_gh\discord_bots\ipy_bot\venv\Lib\site-packages\interactions\client\bot.py", line 479, in _login
    await self._websocket.run()
  File "D:\python_gh\discord_bots\ipy_bot\venv\Lib\site-packages\interactions\api\gateway\client.py", line 304, in run
    await self._handle_stream(msg)
  File "D:\python_gh\discord_bots\ipy_bot\venv\Lib\site-packages\interactions\api\gateway\client.py", line 356, in _handle_stream
    self._dispatch_event(event, data)
  File "D:\python_gh\discord_bots\ipy_bot\venv\Lib\site-packages\interactions\api\gateway\client.py", line 374, in _dispatch_event
    self._dispatch_discord_event(event, data)
  File "D:\python_gh\discord_bots\ipy_bot\venv\Lib\site-packages\interactions\api\gateway\client.py", line 493, in _dispatch_discord_event
    obj = model(**data)
          ^^^^^^^^^^^^^
  File "D:\python_gh\discord_bots\ipy_bot\venv\Lib\site-packages\interactions\utils\attrs_utils.py", line 144, in __init__
    super().__init__(**kwargs)
  File "D:\python_gh\discord_bots\ipy_bot\venv\Lib\site-packages\interactions\utils\attrs_utils.py", line 80, in __init__
    self.__attrs_init__(**passed_kwargs)  # type: ignore
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<attrs generated init interactions.api.models.message.Message>", line 21, in __attrs_init__
    self.type = __attr_converter_type(type)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\python_gh\discord_bots\ipy_bot\venv\Lib\site-packages\attr\converters.py", line 39, in optional_converter
    return converter(val)
           ^^^^^^^^^^^^^^
  File "D:\python_gh\discord_bots\ipy_bot\venv\Lib\site-packages\interactions\utils\attrs_utils.py", line 205, in inner_convert_object
    return value if isinstance(value, type_) else type_(value)
                                                  ^^^^^^^^^^^^
  File "C:\Users\User\AppData\Local\Programs\Python\Python311\Lib\enum.py", line 695, in __call__
    return cls.__new__(cls, value)
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\User\AppData\Local\Programs\Python\Python311\Lib\enum.py", line 1111, in __new__
    raise ve_exc
ValueError: 0 is not a valid MessageType

Process finished with exit code 0

This pr fixes this error by implementing own enum for int and str types and adding _missing_ method for catching unknown values.

Code:

@client.event
async def on_message_create(message: interactions.Message):
    print(message.type, type(message.type))

Result:

MessageType.UNKNOWN: 0 <enum 'MessageType'>

Checklist

  • The pre-commit code linter has been run over all edited files to ensure the code is linted.
  • I've ensured the change(s) work on 3.8.6 and higher.

I've made this pull request: (check all that apply)

  • For the documentation
  • To add a new feature
  • As a general enhancement
  • As a refactor of the library/the library's code
  • To fix an existing bug
  • To resolve #ISSUENUMBER

This is:

  • A breaking change

@Damego Damego added bug Something isn't working enhancement New feature or request labels Dec 15, 2022
@Damego Damego merged commit f052b44 into interactions-py:unstable Jan 21, 2023
@Damego Damego deleted the enuuuuums branch January 21, 2023 16:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants