|
| 1 | +# Py-cord Models |
| 2 | + |
| 3 | +This directory contains the pydantic models and types used by py-cord. |
| 4 | + |
| 5 | +## Structure |
| 6 | + |
| 7 | +The models are structured in a way that they mirror the structure of the Discord API. |
| 8 | +They are subdivided into the following submodules: |
| 9 | + |
| 10 | +> [!IMPORTANT] Each of the submodules is defined below in order. Submodules may only |
| 11 | +> reference in their code classes from the same or lower level submodules. For example, |
| 12 | +> `api` may reference classes from `api`, `base` and `types`, but `base` may not |
| 13 | +> reference classes from `api`. This is to prevent circular imports and to keep the |
| 14 | +> codebase clean and maintainable. |
| 15 | +
|
| 16 | +### `types` |
| 17 | + |
| 18 | +Contains python types and dataclasses that are used in the following submodules. These |
| 19 | +are used to represent the data in a more pythonic way, and are used to define the |
| 20 | +pydantic models. |
| 21 | + |
| 22 | +### `base` |
| 23 | + |
| 24 | +Contains the base models defined in the Discord docs. These are the models you will |
| 25 | +often find with a heading like "... Template", and hyperlinks linking to it referring to |
| 26 | +it as an "object". |
| 27 | + |
| 28 | +For example, the |
| 29 | +[User Template](https://discord.com/developers/docs/resources/user#user-object) is |
| 30 | +defined in `base/user.py`. |
| 31 | + |
| 32 | +### `api` |
| 33 | + |
| 34 | +Contains the models that are used to represent the data received and set trough discord |
| 35 | +API requests. They represent payloads that are sent and received from the Discord API. |
| 36 | + |
| 37 | +When representing a route, it is preferred to create a single python file for each base |
| 38 | +route. If the file may become too large, it is preferred to split it into multiple |
| 39 | +files, one for each sub-route. In that case, a submodule with the name of the base route |
| 40 | +should be created to hold the sub-routes. |
| 41 | + |
| 42 | +For example, the |
| 43 | +[Modify Guild Template](https://discord.com/developers/docs/resources/guild-template#modify-guild-template) |
| 44 | +is defined in `api/guild_template.py`. |
| 45 | + |
| 46 | +### `gateway` |
| 47 | + |
| 48 | +Contains the models that are used to represent the data received and sent trough the |
| 49 | +Discord Gateway. They represent payloads that are sent and received from the Discord |
| 50 | +Gateway. |
| 51 | + |
| 52 | +For example, the [Ready Event](https://discord.com/developers/docs/topics/gateway#hello) |
| 53 | +is defined in `gateway/ready.py`. |
| 54 | + |
| 55 | +## Naming |
| 56 | + |
| 57 | +The naming of the models is based on the Discord API documentation. The models are named |
| 58 | +after the object they represent in the Discord API documentation. It is generally |
| 59 | +preferred to create a new model for each object in the Discord API documentation, even |
| 60 | +if the file may only contain a single class, so that the structure keeps a 1:1 mapping |
| 61 | +with the Discord API documentation. |
| 62 | + |
| 63 | +## Exporting strategies |
| 64 | + |
| 65 | +The models are exported in the following way: |
| 66 | + |
| 67 | +- The models are exported in the `__init__.py` of their respective submodules. |
| 68 | +- Models from the `base` submodule are re-exported in the `__init__.py` of the `modules` |
| 69 | + module. |
| 70 | +- The other submodules are re-exported in the `__init__.py` of the `models` module as a |
| 71 | + single import. |
| 72 | +- The `models` module is re-exported in the `discord` module. |
0 commit comments