Skip to content

Commit 0961b12

Browse files
authored
refactor: sourcery-AI-refactor (#621)
1 parent fa6d9e4 commit 0961b12

File tree

2 files changed

+88
-97
lines changed

2 files changed

+88
-97
lines changed

interactions/api/gateway.py

Lines changed: 80 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ async def wait_until_ready(self):
280280
"""Waits for the client to become ready according to the Gateway."""
281281
await self.ready.wait()
282282

283-
def _dispatch_event(self, event: str, data: dict) -> None:
283+
def _dispatch_event(self, event: str, data: dict) -> None: # sourcery no-metrics
284284
"""
285285
Dispatches an event from the Gateway.
286286
@@ -292,96 +292,87 @@ def _dispatch_event(self, event: str, data: dict) -> None:
292292
path: str = "interactions"
293293
path += ".models" if event == "INTERACTION_CREATE" else ".api.models"
294294

295-
if event != "TYPING_START":
296-
if event != "INTERACTION_CREATE":
297-
name: str = event.lower()
298-
try:
299-
_event_path: list = [section.capitalize() for section in name.split("_")]
300-
_name: str = (
301-
_event_path[0] if len(_event_path) < 3 else "".join(_event_path[:-1])
302-
)
303-
__obj: object = getattr(__import__(path), _name)
304-
305-
if name in {"_create", "_add"}:
306-
data["_client"] = self._http
307-
308-
self._dispatch.dispatch(f"on_{name}", __obj(**data)) # noqa
309-
except AttributeError as error:
310-
log.fatal(f"An error occured dispatching {name}: {error}")
295+
if event == "INTERACTION_CREATE":
296+
if not data.get("type"):
297+
log.warning(
298+
"Context is being created for the interaction, but no type is specified. Skipping..."
299+
)
311300
else:
312-
if not data.get("type"):
313-
log.warning(
314-
"Context is being created for the interaction, but no type is specified. Skipping..."
315-
)
316-
else:
317-
_context = self.__contextualize(data)
318-
_name: str = ""
319-
__args: list = [_context]
320-
__kwargs: dict = {}
321-
322-
if data["type"] == InteractionType.APPLICATION_COMMAND:
323-
_name = f"command_{_context.data.name}"
324-
325-
if _context.data._json.get("options"):
326-
for option in _context.data.options:
327-
_type = self.__option_type_context(
328-
_context,
329-
(
330-
option["type"]
331-
if isinstance(option, dict)
332-
else option.type.value
333-
),
334-
)
335-
if _type:
336-
if isinstance(option, dict):
337-
_type[option["value"]]._client = self._http
338-
option.update({"value": _type[option["value"]]})
339-
else:
340-
_type[option.value]._client = self._http
341-
option._json.update({"value": _type[option.value]})
342-
_option = self.__sub_command_context(option, _context)
343-
__kwargs.update(_option)
344-
345-
self._dispatch.dispatch("on_command", _context)
346-
elif data["type"] == InteractionType.MESSAGE_COMPONENT:
347-
_name = f"component_{_context.data.custom_id}"
348-
349-
if _context.data._json.get("values"):
350-
__args.append(_context.data.values)
351-
352-
self._dispatch.dispatch("on_component", _context)
353-
elif data["type"] == InteractionType.APPLICATION_COMMAND_AUTOCOMPLETE:
354-
_name = f"autocomplete_{_context.data.id}"
355-
356-
if _context.data._json.get("options"):
357-
for option in _context.data.options:
358-
__name, _value = self.__sub_command_context(option, _context)
359-
_name += f"_{__name}" if __name else ""
360-
361-
if _value:
362-
__args.append(_value)
363-
364-
self._dispatch.dispatch("on_autocomplete", _context)
365-
elif data["type"] == InteractionType.MODAL_SUBMIT:
366-
_name = f"modal_{_context.data.custom_id}"
367-
368-
if _context.data._json.get("components"):
369-
for component in _context.data.components:
370-
if component.get("components"):
371-
__args.append(
372-
[_value["value"] for _value in component["components"]][0]
373-
)
301+
# sourcery skip: extract-method
302+
_context = self.__contextualize(data)
303+
_name: str = ""
304+
__args: list = [_context]
305+
__kwargs: dict = {}
306+
307+
if data["type"] == InteractionType.APPLICATION_COMMAND:
308+
_name = f"command_{_context.data.name}"
309+
310+
if _context.data._json.get("options"):
311+
for option in _context.data.options:
312+
_type = self.__option_type_context(
313+
_context,
314+
(option["type"] if isinstance(option, dict) else option.type.value),
315+
)
316+
if _type:
317+
if isinstance(option, dict):
318+
_type[option["value"]]._client = self._http
319+
option.update({"value": _type[option["value"]]})
374320
else:
375-
__args.append(
376-
[_value.value for _value in component.components][0]
377-
)
378-
379-
self._dispatch.dispatch("on_modal", _context)
380-
381-
self._dispatch.dispatch(_name, *__args, **__kwargs)
382-
self._dispatch.dispatch("on_interaction", _context)
383-
self._dispatch.dispatch("on_interaction_create", _context)
384-
321+
_type[option.value]._client = self._http
322+
option._json.update({"value": _type[option.value]})
323+
_option = self.__sub_command_context(option, _context)
324+
__kwargs.update(_option)
325+
326+
self._dispatch.dispatch("on_command", _context)
327+
elif data["type"] == InteractionType.MESSAGE_COMPONENT:
328+
_name = f"component_{_context.data.custom_id}"
329+
330+
if _context.data._json.get("values"):
331+
__args.append(_context.data.values)
332+
333+
self._dispatch.dispatch("on_component", _context)
334+
elif data["type"] == InteractionType.APPLICATION_COMMAND_AUTOCOMPLETE:
335+
_name = f"autocomplete_{_context.data.id}"
336+
337+
if _context.data._json.get("options"):
338+
for option in _context.data.options:
339+
__name, _value = self.__sub_command_context(option, _context)
340+
_name += f"_{__name}" if __name else ""
341+
342+
if _value:
343+
__args.append(_value)
344+
345+
self._dispatch.dispatch("on_autocomplete", _context)
346+
elif data["type"] == InteractionType.MODAL_SUBMIT:
347+
_name = f"modal_{_context.data.custom_id}"
348+
349+
if _context.data._json.get("components"):
350+
for component in _context.data.components:
351+
if component.get("components"):
352+
__args.append(
353+
[_value["value"] for _value in component["components"]][0]
354+
)
355+
else:
356+
__args.append([_value.value for _value in component.components][0])
357+
358+
self._dispatch.dispatch("on_modal", _context)
359+
360+
self._dispatch.dispatch(_name, *__args, **__kwargs)
361+
self._dispatch.dispatch("on_interaction", _context)
362+
self._dispatch.dispatch("on_interaction_create", _context)
363+
elif event != "TYPING_START":
364+
name: str = event.lower()
365+
try:
366+
_event_path: list = [section.capitalize() for section in name.split("_")]
367+
_name: str = _event_path[0] if len(_event_path) < 3 else "".join(_event_path[:-1])
368+
__obj: object = getattr(__import__(path), _name)
369+
370+
if name in {"_create", "_add"}:
371+
data["_client"] = self._http
372+
373+
self._dispatch.dispatch(f"on_{name}", __obj(**data)) # noqa
374+
except AttributeError as error:
375+
log.fatal(f"An error occured dispatching {name}: {error}")
385376
self._dispatch.dispatch("raw_socket_create", data)
386377

387378
def __contextualize(self, data: dict) -> object:

interactions/api/models/message.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,11 +1132,11 @@ def set_field_at(
11321132
self.fields[index] = EmbedField(name=name, value=value, inline=inline)
11331133
self._json.update({"fields": [field._json for field in self.fields]})
11341134

1135-
except AttributeError:
1136-
raise AttributeError("No fields found in Embed")
1135+
except AttributeError as e:
1136+
raise AttributeError("No fields found in Embed") from e
11371137

1138-
except IndexError:
1139-
raise IndexError("No fields at this index")
1138+
except IndexError as e:
1139+
raise IndexError("No fields at this index") from e
11401140

11411141
def remove_field(self, index: int) -> None:
11421142
"""
@@ -1150,11 +1150,11 @@ def remove_field(self, index: int) -> None:
11501150
self.fields.pop(index)
11511151
self._json.update({"fields": [field._json for field in self.fields]})
11521152

1153-
except AttributeError:
1154-
raise AttributeError("No fields found in Embed")
1153+
except AttributeError as e:
1154+
raise AttributeError("No fields found in Embed") from e
11551155

1156-
except IndexError:
1157-
raise IndexError("Field not Found at index")
1156+
except IndexError as e:
1157+
raise IndexError("Field not Found at index") from e
11581158

11591159
def remove_author(self) -> None:
11601160
"""

0 commit comments

Comments
 (0)