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