Conditions | 3 |
Total Lines | 72 |
Code Lines | 45 |
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 | import re |
||
33 | @command(description="to create fake tweets") |
||
34 | async def twitter( |
||
35 | self, |
||
36 | ctx: MessageContext, |
||
37 | content: CommandArg[str, Description["The content of the message"]] |
||
38 | ): |
||
39 | await ctx.interaction.ack() |
||
40 | |||
41 | for text_match, user_id in re.findall( |
||
42 | re.compile(r"(<@!(\d+)>)"), content |
||
43 | ): |
||
44 | content = content.replace( |
||
45 | text_match, f"@{await self.get_user(user_id)}" |
||
46 | ) |
||
47 | |||
48 | if len(content) > 280: |
||
49 | return "A tweet can be at maximum 280 characters long" |
||
50 | |||
51 | # download the profile picture and convert it into Image object |
||
52 | avatar = (await ctx.author.user.get_avatar()).resize((128, 128)) |
||
53 | avatar = circular_avatar(avatar) |
||
54 | |||
55 | # create the tweet by pasting the profile picture into a white image |
||
56 | tweet = trans_paste( |
||
57 | avatar, |
||
58 | Image.new( |
||
59 | "RGBA", |
||
60 | (800, 250 + 50 * len(textwrap.wrap(content, 38))), |
||
61 | (255, 255, 255) |
||
62 | ), |
||
63 | box=(15, 15), |
||
64 | ) |
||
65 | |||
66 | # add the fonts |
||
67 | font_normal = ImageFont.truetype("NotoSans-Regular.ttf", 40) |
||
68 | font_small = ImageFont.truetype("NotoSans-Regular.ttf", 30) |
||
69 | font_bold = ImageFont.truetype("NotoSans-Bold.ttf", 40) |
||
70 | |||
71 | # write the name and username on the Image |
||
72 | draw = ImageDraw.Draw(tweet) |
||
73 | draw.text( |
||
74 | (180, 20), str(ctx.author.user), |
||
75 | fill=(0, 0, 0), font=font_bold |
||
76 | ) |
||
77 | draw.text( |
||
78 | (180, 70), |
||
79 | f"@{ctx.author.user.username}", |
||
80 | fill=(120, 120, 120), font=font_normal, |
||
81 | ) |
||
82 | |||
83 | content = add_color_to_mentions(content) |
||
84 | |||
85 | # write the text |
||
86 | tweet = draw_multicolored_text(tweet, content, font_normal) |
||
87 | |||
88 | # write the footer |
||
89 | draw.text( |
||
90 | (30, tweet.size[1] - 60), |
||
91 | datetime.now().strftime( |
||
92 | "%I:%M %p · %d %b. %Y · Twitter for Discord" |
||
93 | ), |
||
94 | fill=(120, 120, 120), |
||
95 | font=font_small, |
||
96 | ) |
||
97 | |||
98 | return Message( |
||
99 | embeds=[ |
||
100 | Embed(title="Twitter for Discord").set_image( |
||
101 | url="attachment://image0.png" |
||
102 | ) |
||
103 | ], |
||
104 | attachments=[tweet], |
||
105 | ) |
||
193 |