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

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

Merged
merged 39 commits into from Nov 23, 2022
Merged
Show file tree
Hide file tree
Changes from 36 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
d705496
refactor!: use `attrs.asdict` as a way to get json data of the object
Damego Oct 17, 2022
ce8907c
refactor: remove `self._json` mentions
Damego Oct 18, 2022
802e42b
ci: correct from checks.
pre-commit-ci[bot] Oct 18, 2022
0e42d88
refactor: wsclient is hard. part I
Damego Oct 18, 2022
f5a736e
ci: correct from checks.
pre-commit-ci[bot] Oct 18, 2022
ed2b161
Update attrs_utils.pyi
Damego Oct 18, 2022
657d729
refactor: remove json editing
Damego Oct 18, 2022
7a0135a
refactor: why
Damego Oct 18, 2022
b1cc157
refactor: add wat
Damego Oct 18, 2022
d7c018a
Merge branch 'why-didn't-we-do-this-before' of https://github.com/Dam…
Damego Oct 18, 2022
415ebe8
refactor: limit `._json` usage & remove json modifing
Damego Oct 18, 2022
68489e7
ci: correct from checks.
pre-commit-ci[bot] Oct 18, 2022
2d1221d
refactor: run pre-commit
Damego Oct 18, 2022
a7552b3
Merge branch 'why-didn't-we-do-this-before' of https://github.com/Dam…
Damego Oct 18, 2022
15f7048
fix: oop
Damego Oct 18, 2022
fd30a94
refactor: wsclient is hard. part 2
Damego Oct 18, 2022
171e1ea
chore: remove comment
Damego Oct 18, 2022
8ded3ed
ci: correct from checks.
pre-commit-ci[bot] Oct 18, 2022
b9f0e6e
refactor: remove comments and debug print
Damego Oct 19, 2022
a0c2328
refactor(channel): remove _json modifing
Damego Oct 19, 2022
d6a2a9c
chore: remove comment
Damego Oct 19, 2022
138e56b
refactor: wsclient is hard. Part 3. refactor `__sub_command_context`
Damego Oct 19, 2022
447b1a0
ci: correct from checks.
pre-commit-ci[bot] Oct 19, 2022
7b48faf
chore: remove comments
Damego Oct 19, 2022
c2bf51f
Update interactions/utils/attrs_utils.py
Damego Nov 6, 2022
c4d5bbf
Merge branch 'unstable' into why-didn't-we-do-this-before
Damego Nov 6, 2022
619d073
Update interactions/client/models/component.py
Damego Nov 7, 2022
805483c
Update interactions/client/models/component.py
Damego Nov 7, 2022
0245a7b
Update interactions/client/models/component.py
Damego Nov 7, 2022
ec13a1f
Update interactions/client/models/component.py
Damego Nov 7, 2022
12b27fc
Update interactions/client/models/component.py
Damego Nov 7, 2022
8cce2d2
Update attrs_utils.py
Damego Nov 7, 2022
2cea0dc
fix syntax error
Damego Nov 7, 2022
e130137
ci: correct from checks.
pre-commit-ci[bot] Nov 7, 2022
87a25bc
fix: missed action rows in the modal components
Damego Nov 7, 2022
a187eef
ci: correct from checks.
pre-commit-ci[bot] Nov 7, 2022
a3af8fa
remove comment
Damego Nov 7, 2022
75b6bfa
if snowflakes are technically str, why i have to do this?
Damego Nov 8, 2022
5ff44b4
Merge branch 'unstable' into why-didn't-we-do-this-before
Damego Nov 10, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
111 changes: 41 additions & 70 deletions interactions/api/gateway/client.py
Expand Up @@ -364,46 +364,41 @@ 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:
EdVraz marked this conversation as resolved.
Show resolved Hide resolved
_type[option.value]._client = self._http
option._json.update({"value": _type[option.value]})
_type[option.value]._client = self._http
# I'm not sure about line below because its doesn't affect anything
option.value = _type[option.value]
EdVraz marked this conversation as resolved.
Show resolved Hide resolved
_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 @@ -417,8 +412,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 @@ -430,11 +423,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 @@ -537,7 +526,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 @@ -752,91 +741,72 @@ 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 data: The data structure of the option.
:type data: Union[dict, Option]
:param option: The option object.
:type option: Option
:param context: The context to refer subcommands from.
:type context: object
: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 @@ -1148,9 +1118,10 @@ async def _update_presence(self, presence: ClientPresence) -> None:
:param presence: The presence to change the bot to on identify.
:type presence: ClientPresence
"""
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 @@ -1748,9 +1748,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 @@ -1764,9 +1763,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 @@ -1162,7 +1162,7 @@ async def modify_channel(
)
_type = ch.type

payload = Channel(
payload = dict(
name=_name,
type=_type,
topic=_topic,
Expand All @@ -1175,8 +1175,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