Conditions | 8 |
Total Lines | 68 |
Code Lines | 40 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | # -*- coding: utf-8 -*- |
||
39 | async def interaction_create_middleware(self, payload: GatewayDispatch): |
||
40 | """ |
||
41 | Middleware for ``on_interaction``, which handles command |
||
42 | execution. |
||
43 | |||
44 | :param self: |
||
45 | The current client. |
||
46 | |||
47 | :param payload: |
||
48 | The data received from the interaction event. |
||
49 | """ |
||
50 | interaction: Interaction = Interaction.from_dict(payload.data) |
||
51 | command = ChatCommandHandler.register.get(interaction.data.name) |
||
52 | |||
53 | if command: |
||
54 | defaults = {param: None for param in get_params(command.call)} |
||
55 | params = {} |
||
56 | |||
57 | if interaction.data.options is not MISSING: |
||
58 | params = { |
||
59 | opt.name: opt.value for opt in interaction.data.options |
||
60 | } |
||
61 | |||
62 | kwargs = {**defaults, **params} |
||
63 | |||
64 | if should_pass_cls(command.call): |
||
65 | kwargs["self"] = self |
||
66 | |||
67 | if isasyncgenfunction(command.call): |
||
68 | message = command.call(**kwargs) |
||
69 | started = False |
||
70 | |||
71 | async for msg in message: |
||
72 | |||
73 | msg = convert_message(self, msg) |
||
74 | if started: |
||
75 | try: |
||
76 | await self.http.post( |
||
77 | f"webhooks/{interaction.application_id}/{interaction.token}", |
||
78 | msg.to_dict().get("data") |
||
79 | ) |
||
80 | except RateLimitError as e: |
||
81 | _log.exception( |
||
82 | f"RateLimitError: {e}. Retrying in {e.json.get('retry_after', 40)} seconds") |
||
83 | await sleep(e.json.get("retry_after", 40)) |
||
84 | await self.http.post( |
||
85 | f"webhooks/{interaction.application_id}/{interaction.token}", |
||
86 | msg.to_dict().get("data") |
||
87 | ) |
||
88 | |||
89 | else: |
||
90 | started = True |
||
91 | |||
92 | await self.http.post( |
||
93 | f"interactions/{interaction.id}/{interaction.token}/callback", |
||
94 | msg.to_dict() |
||
95 | ) |
||
96 | await sleep(0.3) |
||
97 | else: |
||
98 | message = await command.call(**kwargs) |
||
99 | message = convert_message(self, message) |
||
100 | |||
101 | await self.http.post( |
||
102 | f"interactions/{interaction.id}/{interaction.token}/callback", |
||
103 | message.to_dict() |
||
104 | ) |
||
105 | |||
106 | return "on_interaction_create", [interaction] |
||
107 | |||
123 |