77#
88# Based code + improve from AdekMaulana and aidilaryanto
99
10+ import textwrap
1011import asyncio
1112import os
1213import random
1314import re
1415from asyncio .exceptions import TimeoutError
1516
17+ from PIL import Image , ImageEnhance , ImageOps , ImageFont , ImageDraw
1618from telethon import events
1719from telethon .errors .rpcerrorlist import YouBlockedUserError
18- from telethon .tl .types import MessageMediaPhoto
20+ from telethon .tl .types import DocumentAttributeFilename
1921
2022from userbot import CMD_HELP , TEMP_DOWNLOAD_DIRECTORY , bot
2123from userbot .events import register
3941
4042@register (outgoing = True , pattern = "^.mmf(?: |$)(.*)" )
4143async def mim (event ):
42- if event .fwd_from :
43- return
4444 if not event .reply_to_msg_id :
4545 await event .edit (
4646 "`Syntax: reply to an image with .mmf` 'text on top' ; 'text on bottom' "
@@ -51,94 +51,99 @@ async def mim(event):
5151 if not reply_message .media :
5252 await event .edit ("```reply to a image/sticker/gif```" )
5353 return
54- if reply_message .sender .bot :
55- await event .edit ("```Reply to actual users message.```" )
56- return
57- else :
54+ await bot .download_file (reply_message .media )
55+ if event .is_reply :
56+ data = await check_media (reply_message )
57+ if isinstance (data , bool ):
58+ await event .edit ("`Unsupported Files...`" )
59+ return
60+
5861 await event .edit (
5962 "```Transfiguration Time! Mwahaha Memifying this image! (」゚ロ゚)」 ```"
6063 )
61-
62- chat = "@MemeAutobot"
63- async with bot .conversation (chat ) as bot_conv :
64- try :
65- memeVar = event .pattern_match .group (1 )
66- await silently_send_message (bot_conv , "/start" )
67- await asyncio .sleep (1 )
68- await silently_send_message (bot_conv , memeVar )
69- await bot .send_file (chat , reply_message .media )
70- response = await bot_conv .get_response ()
71- except YouBlockedUserError :
72- await event .reply ("```Please unblock @MemeAutobot and try again```" )
73- return
74- if response .text .startswith ("Forward" ):
75- await event .edit (
76- "```can you kindly disable your forward privacy settings for good, Nibba?```"
77- )
78- if "Okay..." in response .text :
79- await event .edit (
80- "```🛑 🤨 NANI?! This is not an image! This will take sum tym to convert to image... UwU 🧐 🛑```"
81- )
82- input_str = event .pattern_match .group (1 )
83- if event .reply_to_msg_id :
84- file_name = "meme.png"
85- reply_message = await event .get_reply_message ()
86- downloaded_file_name = os .path .join (TEMP_DOWNLOAD_DIRECTORY , file_name )
87- downloaded_file_name = await bot .download_media (
88- reply_message ,
89- downloaded_file_name ,
90- )
91- if os .path .exists (downloaded_file_name ):
92- await bot .send_file (
93- chat ,
94- downloaded_file_name ,
95- force_document = False ,
96- supports_streaming = False ,
97- allow_cache = False ,
98- )
99- os .remove (downloaded_file_name )
100- else :
101- await event .edit ("File Not Found {}" .format (input_str ))
102- response = await bot_conv .get_response ()
103- files_name = "memes.webp"
104- download_file_name = os .path .join (TEMP_DOWNLOAD_DIRECTORY , files_name )
105- await bot .download_media (
106- response .media ,
107- download_file_name ,
108- )
109- await bot .send_file ( # pylint:disable=E0602
110- event .chat_id ,
111- download_file_name ,
112- supports_streaming = False ,
113- caption = "Memifyed" ,
64+ await asyncio .sleep (5 )
65+ text = event .pattern_match .group (1 )
66+ if event .reply_to_msg_id :
67+ file_name = "meme.jpg"
68+ to_download_directory = TEMP_DOWNLOAD_DIRECTORY
69+ downloaded_file_name = os .path .join (
70+ to_download_directory , file_name )
71+ downloaded_file_name = await bot .download_media (
72+ reply_message , downloaded_file_name ,
11473 )
115- await event .delete ()
116- elif not is_message_image (reply_message ):
117- await event .edit (
118- "Invalid message type. Plz choose right message type u NIBBA."
119- )
120- return
121- else :
122- await bot .send_file (event .chat_id , response .media )
123- await event .delete ()
124-
125-
126- def is_message_image (message ):
127- if message .media :
128- if isinstance (message .media , MessageMediaPhoto ):
129- return True
130- if message .media .document :
131- if message .media .document .mime_type .split ("/" )[0 ] == "image" :
132- return True
133- return False
134- return False
135-
136-
137- async def silently_send_message (conv , text ):
138- await conv .send_message (text )
139- response = await conv .get_response ()
140- await conv .mark_read (message = response )
141- return response
74+ dls_loc = downloaded_file_name
75+ webp_file = await draw_meme_text (dls_loc , text )
76+ await event .client .send_file (event .chat_id , webp_file , reply_to = event .reply_to_msg_id )
77+ await event .delete ()
78+ os .remove (webp_file )
79+ os .remove (dls_loc )
80+
81+
82+ async def draw_meme_text (image_path , text ):
83+ img = Image .open (image_path )
84+ os .remove (image_path )
85+ i_width , i_height = img .size
86+ m_font = ImageFont .truetype (
87+ "resources/MutantAcademyStyle.ttf" , int ((70 / 640 ) * i_width ))
88+ if ";" in text :
89+ upper_text , lower_text = text .split (";" )
90+ else :
91+ upper_text = text
92+ lower_text = ''
93+ draw = ImageDraw .Draw (img )
94+ current_h , pad = 10 , 5
95+ if upper_text :
96+ for u_text in textwrap .wrap (upper_text , width = 15 ):
97+ u_width , u_height = draw .textsize (u_text , font = m_font )
98+
99+ draw .text (xy = (((i_width - u_width ) / 2 ) - 1 , int ((current_h / 640 )
100+ * i_width )), text = u_text , font = m_font , fill = (0 , 0 , 0 ))
101+ draw .text (xy = (((i_width - u_width ) / 2 ) + 1 , int ((current_h / 640 )
102+ * i_width )), text = u_text , font = m_font , fill = (0 , 0 , 0 ))
103+ draw .text (xy = ((i_width - u_width ) / 2 ,
104+ int (((current_h / 640 ) * i_width )) - 1 ),
105+ text = u_text ,
106+ font = m_font ,
107+ fill = (0 ,
108+ 0 ,
109+ 0 ))
110+ draw .text (xy = (((i_width - u_width ) / 2 ),
111+ int (((current_h / 640 ) * i_width )) + 1 ),
112+ text = u_text ,
113+ font = m_font ,
114+ fill = (0 ,
115+ 0 ,
116+ 0 ))
117+
118+ draw .text (xy = ((i_width - u_width ) / 2 , int ((current_h / 640 )
119+ * i_width )), text = u_text , font = m_font , fill = (255 , 255 , 255 ))
120+ current_h += u_height + pad
121+ if lower_text :
122+ for l_text in textwrap .wrap (lower_text , width = 15 ):
123+ u_width , u_height = draw .textsize (l_text , font = m_font )
124+
125+ draw .text (
126+ xy = (((i_width - u_width ) / 2 ) - 1 , i_height - u_height - int ((20 / 640 ) * i_width )),
127+ text = l_text , font = m_font , fill = (0 , 0 , 0 ))
128+ draw .text (
129+ xy = (((i_width - u_width ) / 2 ) + 1 , i_height - u_height - int ((20 / 640 ) * i_width )),
130+ text = l_text , font = m_font , fill = (0 , 0 , 0 ))
131+ draw .text (
132+ xy = ((i_width - u_width ) / 2 , (i_height - u_height - int ((20 / 640 ) * i_width )) - 1 ),
133+ text = l_text , font = m_font , fill = (0 , 0 , 0 ))
134+ draw .text (
135+ xy = ((i_width - u_width ) / 2 , (i_height - u_height - int ((20 / 640 ) * i_width )) + 1 ),
136+ text = l_text , font = m_font , fill = (0 , 0 , 0 ))
137+
138+ draw .text (
139+ xy = ((i_width - u_width ) / 2 , i_height - u_height - int ((20 / 640 ) * i_width )),
140+ text = l_text , font = m_font , fill = (255 , 255 , 255 ))
141+ current_h += u_height + pad
142+
143+ image_name = "memify.webp"
144+ webp_file = os .path .join (TEMP_DOWNLOAD_DIRECTORY , image_name )
145+ img .save (webp_file , "WebP" )
146+ return webp_file
142147
143148
144149@register (outgoing = True , pattern = r"^\.q" )
0 commit comments