Conditions | 9 |
Total Lines | 51 |
Code Lines | 26 |
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 | """Deals with long message sending""" |
||
10 | async def list_message(ctx, message: list, title: str, **kwargs): |
||
11 | """list_message |
||
12 | --- |
||
13 | Asynchronous Function |
||
14 | |||
15 | Breaks up messages that contain a list and sends the parts of them. Shared function between |
||
16 | multiple commands. |
||
17 | |||
18 | |||
19 | I'm sorry for everyone dealing with this function. It is not clean and I have commented to |
||
20 | the best that I can. |
||
21 | |||
22 | Arguments: |
||
23 | --- |
||
24 | ctx {discord.ext.commands.Context} -- Context of the command. |
||
25 | message {list} -- list of items to send. |
||
26 | title {str} -- First line of the message to send. |
||
27 | """ |
||
28 | joined_message = len("".join(message)) |
||
29 | list_of_embeds = [] |
||
30 | part = 1 |
||
31 | item = 0 |
||
32 | amount_of_embeds = len(range(0, joined_message, 1500)) |
||
33 | for _ in range(amount_of_embeds): |
||
34 | # Each embed can only be 6000 characters so if the length is over that more are created |
||
35 | embed = await make_embed(ctx, title=title, send=False, **kwargs) |
||
36 | for _ in range(2): |
||
37 | temp_msg = "" |
||
38 | while len(temp_msg) < 1024: |
||
39 | # Each field can only be 1024 characters |
||
40 | try: |
||
41 | if len(temp_msg + "- {}\n".format(message[item])) > 1024: |
||
42 | # If the new item is going to make it over the 1024 limit then skip it. |
||
43 | break |
||
44 | temp_msg += "- {}\n".format(message[item]) |
||
45 | item += 1 |
||
46 | except IndexError: |
||
47 | # Error happens when there the length of temp_msg is still under 1000 but |
||
48 | # no items left. |
||
49 | break |
||
50 | if len(temp_msg) > 0: |
||
51 | # Blank messages can occur and this filters them out |
||
52 | embed.add_field(name="Part: {}".format(part), value=temp_msg, inline=True) |
||
53 | part += 1 |
||
54 | list_of_embeds.append(embed) |
||
55 | |||
56 | for item in list_of_embeds: |
||
57 | if len(item.fields) > 0: |
||
58 | await ctx.send(embed=item) |
||
59 | else: |
||
60 | log.warning("Empty embed") |
||
61 | |||
138 |