Conditions | 9 |
Total Lines | 83 |
Code Lines | 44 |
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 | # Copyright Pincer 2021-Present |
||
19 | async def interaction_create_middleware(self, payload: GatewayDispatch): |
||
20 | """ |
||
21 | Middleware for ``on_interaction``, which handles command |
||
22 | execution. |
||
23 | |||
24 | :param self: |
||
25 | The current client. |
||
26 | |||
27 | :param payload: |
||
28 | The data received from the interaction event. |
||
29 | """ |
||
30 | |||
31 | interaction: Interaction = Interaction.from_dict( |
||
32 | payload.data | {"_client": self, "_http": self.http} |
||
33 | ) |
||
34 | |||
35 | command = ChatCommandHandler.register.get(interaction.data.name) |
||
36 | |||
37 | if command: |
||
38 | defaults = {param: None for param in get_params(command.call)} |
||
39 | params = {} |
||
40 | |||
41 | if interaction.data.options is not MISSING: |
||
42 | params = { |
||
43 | opt.name: opt.value for opt in interaction.data.options |
||
44 | } |
||
45 | |||
46 | kwargs = {**defaults, **params} |
||
47 | |||
48 | if should_pass_cls(command.call): |
||
49 | kwargs["self"] = self |
||
50 | |||
51 | sig, params = get_signature_and_params(command.call) |
||
52 | if should_pass_ctx(sig, params): |
||
53 | kwargs[params[0]] = interaction.to_context() |
||
54 | |||
55 | if isasyncgenfunction(command.call): |
||
56 | message = command.call(**kwargs) |
||
57 | started = False |
||
58 | |||
59 | async for msg in message: |
||
60 | |||
61 | msg = convert_message(self, msg) |
||
62 | if started: |
||
63 | try: |
||
64 | await self.http.post( |
||
65 | f"webhooks/{interaction.application_id}" |
||
66 | f"/{interaction.token}", |
||
67 | msg.to_dict().get("data") |
||
68 | ) |
||
69 | except RateLimitError as e: |
||
70 | _log.exception( |
||
71 | f"RateLimitError: {e}." |
||
72 | f" Retrying in {e.json.get('retry_after', 40)}" |
||
73 | f" seconds" |
||
74 | ) |
||
75 | |||
76 | await sleep(e.json.get("retry_after", 40)) |
||
77 | await self.http.post( |
||
78 | f"webhooks/{interaction.application_id}" |
||
79 | f"/{interaction.token}", |
||
80 | msg.to_dict().get("data") |
||
81 | ) |
||
82 | |||
83 | else: |
||
84 | started = True |
||
85 | |||
86 | await self.http.post( |
||
87 | f"interactions/{interaction.id}" |
||
88 | f"/{interaction.token}/callback", |
||
89 | msg.to_dict() |
||
90 | ) |
||
91 | await sleep(0.3) |
||
92 | else: |
||
93 | message = await command.call(**kwargs) |
||
94 | message = convert_message(self, message) |
||
95 | |||
96 | await self.http.post( |
||
97 | f"interactions/{interaction.id}/{interaction.token}/callback", |
||
98 | message.to_dict() |
||
99 | ) |
||
100 | |||
101 | return "on_interaction_create", [interaction] |
||
102 | |||
118 |