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