Skip to content

Commit

Permalink
refactor!: change the _json attribute to a property using `attrs.as…
Browse files Browse the repository at this point in the history
…dict` (#1126)

* refactor!: use `attrs.asdict` as a way to get json data of the object

* refactor: remove `self._json` mentions

* ci: correct from checks.

* refactor: wsclient is hard. part I

* ci: correct from checks.

* Update attrs_utils.pyi

* refactor: remove json editing

* refactor: why

* refactor: add wat

* refactor: limit `._json` usage & remove json modifing

* ci: correct from checks.

* refactor: run pre-commit

* fix: oop

* refactor: wsclient is hard. part 2

* chore: remove comment

* ci: correct from checks.

* refactor: remove comments and debug print

* refactor(channel): remove _json modifing

* chore: remove comment

* refactor: wsclient is hard. Part 3. refactor `__sub_command_context`

* ci: correct from checks.

* chore: remove comments

* Update interactions/utils/attrs_utils.py

* Update interactions/client/models/component.py

Co-authored-by: EdVraz <88881326+EdVraz@users.noreply.github.com>

* Update interactions/client/models/component.py

Co-authored-by: EdVraz <88881326+EdVraz@users.noreply.github.com>

* Update interactions/client/models/component.py

Co-authored-by: EdVraz <88881326+EdVraz@users.noreply.github.com>

* Update interactions/client/models/component.py

Co-authored-by: EdVraz <88881326+EdVraz@users.noreply.github.com>

* Update interactions/client/models/component.py

Co-authored-by: EdVraz <88881326+EdVraz@users.noreply.github.com>

* Update attrs_utils.py

* fix syntax error

* ci: correct from checks.

* fix: missed action rows in the modal components

* ci: correct from checks.

* remove comment

* if snowflakes are technically str, why i have to do this?

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: EdVraz <88881326+EdVraz@users.noreply.github.com>
  • Loading branch information
3 people committed Nov 23, 2022
1 parent e07eecc commit a23d25e
Show file tree
Hide file tree
Showing 11 changed files with 122 additions and 368 deletions.
108 changes: 39 additions & 69 deletions interactions/api/gateway/client.py
Expand Up @@ -379,46 +379,40 @@ def _dispatch_event(self, event: str, data: dict) -> None:
if data["type"] == InteractionType.APPLICATION_COMMAND:
_name = f"command_{_context.data.name}"

if _context.data._json.get("options"):
for option in _context.data.options:
if options := _context.data.options:
for option in options:
_type = self.__option_type_context(
_context,
(option["type"] if isinstance(option, dict) else option.type.value),
option.type.value,
)
if _type:
if isinstance(option, dict):
_type[option["value"]]._client = self._http
option.update({"value": _type[option["value"]]})
else:
_type[option.value]._client = self._http
option._json.update({"value": _type[option.value]})
_type[option.value]._client = self._http
option.value = _type[option.value]
_option = self.__sub_command_context(option, _context)
__kwargs.update(_option)

self._dispatch.dispatch("on_command", _context)
elif data["type"] == InteractionType.MESSAGE_COMPONENT:
_name = f"component_{_context.data.custom_id}"

if _context.data._json.get("values"):
if values := _context.data.values:
if _context.data.component_type.value not in {5, 6, 7, 8}:
__args.append(_context.data.values)
__args.append(values)
else:
_list = [] # temp storage for items
_data = self.__select_option_type_context(
_context, _context.data.component_type.value
) # resolved.
for value in _context.data._json.get("values"):
for value in values:
_list.append(_data[value])
__args.append(_list)

self._dispatch.dispatch("on_component", _context)
elif data["type"] == InteractionType.APPLICATION_COMMAND_AUTOCOMPLETE:
_name = f"autocomplete_{_context.data.name}"

if _context.data._json.get("options"):
if _context.data.options:
for option in _context.data.options:
if isinstance(option, dict):
option = Option(**option)
if option.type not in (
OptionType.SUB_COMMAND,
OptionType.SUB_COMMAND_GROUP,
Expand All @@ -432,8 +426,6 @@ def _dispatch_event(self, event: str, data: dict) -> None:

elif option.type == OptionType.SUB_COMMAND:
for _option in option.options:
if isinstance(_option, dict):
_option = Option(**_option)
if _option.focused:
__name, _value = self.__sub_command_context(
_option, _context
Expand All @@ -445,11 +437,7 @@ def _dispatch_event(self, event: str, data: dict) -> None:

elif option.type == OptionType.SUB_COMMAND_GROUP:
for _option in option.options:
if isinstance(_option, dict):
_option = Option(**_option)
for __option in _option.options:
if isinstance(__option, dict):
__option = Option(**__option)
if __option.focused:
__name, _value = self.__sub_command_context(
__option, _context
Expand Down Expand Up @@ -552,7 +540,7 @@ def _dispatch_event(self, event: str, data: dict) -> None:
old_obj = _cache.get(id)
if old_obj:
before = model(**old_obj._json)
old_obj.update(**obj._json)
old_obj.update(**data)
else:
before = None
old_obj = obj
Expand Down Expand Up @@ -757,89 +745,70 @@ def __contextualize(self, data: dict) -> "_Context":

return context(**data)

def __sub_command_context(
self, data: Union[dict, Option], context: "_Context"
) -> Union[Tuple[str], dict]:
def __sub_command_context(self, option: Option, context: "_Context") -> Union[Tuple[str], dict]:
"""
Checks if an application command schema has sub commands
needed for argument collection.
:param Union[dict, Option] data: The data structure of the option.
:param Option option: The option object
:param _Context context: The context to refer subcommands from.
:return: A dictionary of the collected options, if any.
:rtype: Union[Tuple[str], dict]
"""
__kwargs: dict = {}
_data: dict = data._json if isinstance(data, Option) else data

def _check_auto(option: dict) -> Optional[Tuple[str]]:
return (option["name"], option["value"]) if option.get("focused") else None
def _check_auto(_option: Option) -> Optional[Tuple[str]]:
return (_option.name, _option.value) if _option.focused else None

check = _check_auto(_data)
check = _check_auto(option)

if check:
return check
if _data.get("options"):
if _data["type"] == OptionType.SUB_COMMAND:
__kwargs["sub_command"] = _data["name"]
if options := option.options:
if option.type == OptionType.SUB_COMMAND:
__kwargs["sub_command"] = option.name

for sub_option in _data["options"]:
for sub_option in options:
_check = _check_auto(sub_option)
_type = self.__option_type_context(
context,
(
sub_option["type"]
if isinstance(sub_option, dict)
else sub_option.type.value
),
sub_option.type,
)

if _type:
if isinstance(sub_option, dict):
_type[sub_option["value"]]._client = self._http
sub_option.update({"value": _type[sub_option["value"]]})
else:
_type[sub_option.value]._client = self._http
sub_option._json.update({"value": _type[sub_option.value]})
_type[sub_option.value]._client = self._http
sub_option.value = _type[sub_option.value]
if _check:
return _check

__kwargs[sub_option["name"]] = sub_option["value"]
elif _data["type"] == OptionType.SUB_COMMAND_GROUP:
__kwargs["sub_command_group"] = _data["name"]
for _group_option in _data["options"]:
__kwargs[sub_option.name] = sub_option.value
elif option.type == OptionType.SUB_COMMAND_GROUP:
__kwargs["sub_command_group"] = option.name
for _group_option in option.options:
_check_auto(_group_option)
__kwargs["sub_command"] = _group_option["name"]
__kwargs["sub_command"] = _group_option.name

for sub_option in _group_option["options"]:
for sub_option in _group_option.options:
_check = _check_auto(sub_option)
_type = self.__option_type_context(
context,
(
sub_option["type"]
if isinstance(sub_option, dict)
else sub_option.type.value
),
sub_option.type,
)

if _type:
if isinstance(sub_option, dict):
_type[sub_option["value"]]._client = self._http
sub_option.update({"value": _type[sub_option["value"]]})
else:
_type[sub_option.value]._client = self._http
sub_option._json.update({"value": _type[sub_option.value]})
_type[sub_option.value]._client = self._http
sub_option.value = _type[sub_option.value]
if _check:
return _check

__kwargs[sub_option["name"]] = sub_option["value"]
__kwargs[sub_option.name] = sub_option.value

elif _data.get("type") and _data["type"] == OptionType.SUB_COMMAND:
elif option.type == OptionType.SUB_COMMAND:
# sub_command_groups must have options so there is no extra check needed for those
__kwargs["sub_command"] = _data["name"]
__kwargs["sub_command"] = option.name

elif _data.get("value") is not None and _data.get("name") is not None:
__kwargs[_data["name"]] = _data["value"]
elif (name := option.name) is not None and (value := option.value) is not None:
__kwargs[name] = value

return __kwargs

Expand Down Expand Up @@ -1162,9 +1131,10 @@ async def _update_presence(self, presence: ClientPresence) -> None:
:param ClientPresence presence: The presence to change the bot to on identify.
"""
payload: dict = {"op": OpCodeType.PRESENCE.value, "d": presence._json}
_presence = presence._json
payload: dict = {"op": OpCodeType.PRESENCE.value, "d": _presence}
await self._send_packet(payload)
log.debug(f"UPDATE_PRESENCE: {presence._json}")
log.debug(f"UPDATE_PRESENCE: {_presence}")
self.__presence = presence

async def request_guild_members(
Expand Down
7 changes: 2 additions & 5 deletions interactions/api/models/channel.py
Expand Up @@ -1737,9 +1737,8 @@ async def create_forum_post(
_content = content

elif isinstance(content, Message):
_content = content._json
if content.attachments and any(attach.id is None for attach in content.attachments):
del content._json["attachments"]

for attach in content.attachments:
_data = await attach.download()

Expand All @@ -1753,9 +1752,7 @@ async def create_forum_post(
_files = [__files._json_payload(0)]
__files = [__files]

content._json["attachments"] = _files

_content = content._json
_content["attachments"] = _files

elif isinstance(content, Attachment):
if content.id:
Expand Down
4 changes: 1 addition & 3 deletions interactions/api/models/guild.py
Expand Up @@ -1085,7 +1085,7 @@ async def modify_channel(
)
_type = ch.type

payload = Channel(
payload = dict(
name=_name,
type=_type,
topic=_topic,
Expand All @@ -1098,8 +1098,6 @@ async def modify_channel(
nsfw=_nsfw,
)

payload = payload._json

if (
archived is not MISSING or auto_archive_duration is not MISSING or locked is not MISSING
) and not ch.thread_metadata:
Expand Down

0 comments on commit a23d25e

Please sign in to comment.