66from PIL import Image
77from io import BytesIO
88
9- from utils import handle_errors , ImageText , edit_image , closest_match , no_ping
9+ from utils import handle_errors , ImageText , edit_image , no_ping
1010from cogs .general .gif import GifizeView
1111
12- positions = [app_commands .Choice (name = "Верх" , value = "up" ), app_commands .Choice (name = "Низ" , value = "down" )]
13- positions_aliases = {
14- "up" : ["верх" , "в" ],
15- "down" : ["низ" , "н" ]
16- }
17-
1812
1913class ImpactCommand (commands .Cog ):
2014 @commands .hybrid_command (
@@ -24,41 +18,39 @@ class ImpactCommand(commands.Cog):
2418 help = "### Пример:\n `/impact image.png пей горн Низ`"
2519 )
2620 @app_commands .describe (
27- text = "Текст (до 500 символов)"
21+ top_text = "Верхний текст (до 500 символов)" ,
22+ bottom_text = "Нижний текст (до 500 символов)"
2823 )
24+ @app_commands .default_permissions (discord .Permissions .administrator )
2925
3026 async def impact (
3127 self ,
3228 ctx : commands .Context ,
3329 image : discord .Attachment ,
34- text : str ,
35- position : str = "down "
30+ top_text : str = "" ,
31+ bottom_text : str = ""
3632 ):
37- text = text [:500 ]
33+ top_text = top_text [:500 ]
34+ bottom_text = bottom_text [:500 ]
3835
3936 if not image .content_type or "image" not in image .content_type :
4037 raise Exception ("Not image" )
4138 await ctx .defer ()
4239 extension = image .filename .split ("." )[- 1 ]
4340
44- position = closest_match (position , positions_aliases )
4541 impacted = edit_image (
4642 Image .open (BytesIO (await image .read ())),
4743 extension ,
4844 impact ,
49- text = text ,
50- position = position
45+ top_text = top_text ,
46+ bottom_text = bottom_text
5147 )
5248 impacted_discorded = discord .File (impacted , filename = image .filename )
5349 await ctx .reply (
5450 file = impacted_discorded ,
5551 view = GifizeView () if extension != "gif" else MISSING ,
5652 allowed_mentions = no_ping
5753 )
58-
59- @impact .autocomplete (name = "position" )
60- async def position_default_choices (self , ctx , curr ):
61- return positions
6254
6355 @impact .error
6456 async def impact_error (self , ctx , error ):
@@ -70,33 +62,42 @@ async def impact_error(self, ctx, error):
7062 ])
7163
7264
73- def impact (image : Image .Image , text : str , position : str ):
65+ def impact (image : Image .Image , top_text : str , bottom_text : str ):
7466 font = "assets/memes/impact.ttf"
75- size = int ((((image .width + image .height )/ 2 )/ 10 ) / max (1 , (len (text )/ 25 )* 0.5 ))
67+ get_size = lambda text : int ((((image .width + image .height )/ 2 )/ 10 ) / max (1 , (len (text )/ 25 )* 0.5 ))
68+ top_size = get_size (top_text )
69+ bottom_size = get_size (bottom_text )
7670 color = (255 , 255 , 255 )
7771 width = image .size [0 ]
7872 height = image .size [1 ]
79- padding = int (height / 12 )
80- text_width = width - padding * 2
73+ padding = int (height / 16 )
74+ text_width = width - padding
8175
8276 image = image .convert (mode = "RGBA" )
8377
8478 # Calculate required heights first
8579 impact_text = ImageText (image , anchor = "ma" )
8680
87- if position == "down " :
81+ if bottom_text != " " :
8882 text_height = impact_text .get_height (
89- (int (width / 2 ), 0 ), text ,
90- text_width , font , size ,
91- stroke_width = int (size / 16 ),
83+ (int (width / 2 ), 0 ), bottom_text ,
84+ text_width , font , bottom_size ,
85+ stroke_width = int (bottom_size / 16 ),
86+ stroke_fill = (0 , 0 , 0 , 255 )
87+ )
88+ y = height - padding - text_height
89+ impact_text .write_text_box (
90+ (int (width / 2 ), y ), bottom_text ,
91+ text_width , font , bottom_size , color ,
92+ stroke_width = int (bottom_size / 16 ),
9293 stroke_fill = (0 , 0 , 0 , 255 )
9394 )
94- padding = height - padding - text_height
9595
96- impact_text .write_text_box (
97- (int (width / 2 ), padding ), text ,
98- text_width , font , size , color ,
99- stroke_width = int (size / 16 ),
96+ if top_text != "" :
97+ impact_text .write_text_box (
98+ (int (width / 2 ), padding ), top_text ,
99+ text_width , font , top_size , color ,
100+ stroke_width = int (top_size / 16 ),
100101 stroke_fill = (0 , 0 , 0 , 255 )
101102 )
102103
0 commit comments