Conditions | 9 |
Total Lines | 86 |
Code Lines | 46 |
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 | def convert_message(self, message: Union[Embed, Message, str]) -> Message: |
||
20 | """Converts a message to a Message object""" |
||
21 | if isinstance(message, Embed): |
||
22 | message = Message(embeds=[message]) |
||
23 | elif not isinstance(message, Message): |
||
24 | message = Message(message) if message else Message( |
||
25 | self.received_message, |
||
26 | flags=InteractionFlags.EPHEMERAL |
||
27 | ) |
||
28 | return message |
||
29 | |||
30 | |||
31 | async def reply(self, interaction: Interaction, message): |
||
32 | """ |
||
33 | Sends a reply to an interaction. |
||
34 | |||
35 | :param self: |
||
36 | The current client. |
||
37 | |||
38 | :param interaction: |
||
39 | The interaction from whom the reply is. |
||
40 | |||
41 | :param message: |
||
42 | The message to reply with. |
||
43 | """ |
||
44 | await self.http.post( |
||
45 | f"interactions/{interaction.id}/{interaction.token}/callback", |
||
46 | message.to_dict() |
||
47 | ) |
||
48 | |||
49 | |||
50 | async def interaction_response_handler( |
||
51 | self, |
||
52 | command: Coro, |
||
53 | context: MessageContext, |
||
54 | interaction: Interaction, |
||
55 | kwargs: Dict[str, Any] |
||
56 | ): |
||
57 | """ |
||
58 | Handle any coroutine as a command. |
||
59 | |||
60 | :param self: |
||
61 | The current client. |
||
62 | |||
63 | :param command: |
||
64 | The coroutine which will be seen as a command. |
||
65 | |||
66 | :param context: |
||
67 | The context of the command. |
||
68 | |||
69 | :param interaction: |
||
70 | The interaction which is linked to the command. |
||
71 | |||
72 | :param kwargs: |
||
73 | The arguments to be passed to the command. |
||
74 | """ |
||
75 | # TODO: Make response thread based (eg a new thread per response handler) |
||
76 | if should_pass_cls(command): |
||
77 | kwargs["self"] = self |
||
78 | |||
79 | sig, params = get_signature_and_params(command) |
||
80 | if should_pass_ctx(sig, params): |
||
81 | kwargs[params[0]] = context |
||
82 | |||
83 | if isasyncgenfunction(command): |
||
84 | message = command(**kwargs) |
||
85 | started = False |
||
86 | |||
87 | async for msg in message: |
||
88 | msg = convert_message(self, msg) |
||
89 | |||
90 | if started: |
||
91 | await self.http.post( |
||
92 | f"webhooks/{interaction.application_id}" |
||
93 | f"/{interaction.token}", |
||
94 | msg.to_dict().get("data") |
||
95 | ) |
||
96 | else: |
||
97 | started = True |
||
98 | await reply(self, interaction, msg) |
||
99 | else: |
||
100 | message = await command(**kwargs) |
||
101 | await reply(self, interaction, convert_message(self, message)) |
||
102 | |||
103 | |||
104 | async def interaction_handler( |
||
105 | self, |
||
188 |