|
7 | 7 | # |
8 | 8 | # Based code + improve from AdekMaulana and aidilaryanto |
9 | 9 |
|
| 10 | +import textwrap |
10 | 11 | import asyncio |
11 | 12 | import io |
12 | 13 | import os |
13 | 14 | import random |
14 | 15 | import re |
15 | 16 | from random import randint, uniform |
16 | 17 |
|
17 | | -from PIL import Image, ImageEnhance, ImageOps |
| 18 | +from PIL import Image, ImageEnhance, ImageOps, ImageFont, ImageDraw |
18 | 19 | from telethon import events |
19 | 20 | from telethon.errors.rpcerrorlist import YouBlockedUserError |
20 | | -from telethon.tl.types import DocumentAttributeFilename, MessageMediaPhoto |
| 21 | +from telethon.tl.types import DocumentAttributeFilename |
21 | 22 |
|
22 | 23 | from userbot import CMD_HELP, TEMP_DOWNLOAD_DIRECTORY, bot |
23 | 24 | from userbot.events import register |
@@ -64,98 +65,88 @@ async def mim(event): |
64 | 65 | "```Transfiguration Time! Mwahaha Memifying this image! (」゚ロ゚)」 ```" |
65 | 66 | ) |
66 | 67 | await asyncio.sleep(5) |
67 | | - |
68 | | - async with bot.conversation("@MemeAutobot") as bot_conv: |
69 | | - chat = "@MemeAutobot" |
70 | | - try: |
71 | | - memeVar = event.pattern_match.group(1) |
72 | | - await silently_send_message(bot_conv, "/start") |
73 | | - await asyncio.sleep(1) |
74 | | - await silently_send_message(bot_conv, memeVar) |
75 | | - await bot.send_file(chat, reply_message.media) |
76 | | - response = await bot_conv.get_response() |
77 | | - except YouBlockedUserError: |
78 | | - await event.reply("```Please unblock @MemeAutobot and try again```") |
79 | | - return |
80 | | - if response.text.startswith("Forward"): |
81 | | - await event.edit( |
82 | | - "```can you kindly disable your forward privacy settings for good, Nibba?```" |
83 | | - ) |
84 | | - if "Okay..." in response.text: |
85 | | - await event.edit( |
86 | | - "```🛑 🤨 NANI?! This is not an image! This will take sum tym to convert to image... UwU 🧐 🛑```" |
87 | | - ) |
88 | | - thumb = None |
89 | | - if os.path.exists(THUMB_IMAGE_PATH): |
90 | | - thumb = THUMB_IMAGE_PATH |
91 | | - input_str = event.pattern_match.group(1) |
92 | | - if not os.path.isdir(TEMP_DOWNLOAD_DIRECTORY): |
93 | | - os.makedirs(TEMP_DOWNLOAD_DIRECTORY) |
94 | | - if event.reply_to_msg_id: |
95 | | - file_name = "meme.png" |
96 | | - reply_message = await event.get_reply_message() |
97 | | - to_download_directory = TEMP_DOWNLOAD_DIRECTORY |
98 | | - downloaded_file_name = os.path.join( |
99 | | - to_download_directory, file_name) |
100 | | - downloaded_file_name = await bot.download_media( |
101 | | - reply_message, downloaded_file_name, |
102 | | - ) |
103 | | - if os.path.exists(downloaded_file_name): |
104 | | - await bot.send_file( |
105 | | - chat, |
106 | | - downloaded_file_name, |
107 | | - force_document=False, |
108 | | - supports_streaming=False, |
109 | | - allow_cache=False, |
110 | | - thumb=thumb, |
111 | | - ) |
112 | | - os.remove(downloaded_file_name) |
113 | | - else: |
114 | | - await event.edit("File Not Found {}".format(input_str)) |
115 | | - response = await bot_conv.get_response() |
116 | | - the_download_directory = TEMP_DOWNLOAD_DIRECTORY |
117 | | - files_name = "memes.webp" |
118 | | - download_file_name = os.path.join( |
119 | | - the_download_directory, files_name) |
120 | | - await bot.download_media( |
121 | | - response.media, download_file_name, |
122 | | - ) |
123 | | - requires_file_name = TEMP_DOWNLOAD_DIRECTORY + "memes.webp" |
124 | | - await bot.send_file( # pylint:disable=E0602 |
125 | | - event.chat_id, |
126 | | - requires_file_name, |
127 | | - supports_streaming=False, |
128 | | - caption="Memifyed", |
129 | | - ) |
130 | | - await event.delete() |
131 | | - # await bot.send_message(event.chat_id, "`☠️☠️Ah Shit... Here we go |
132 | | - # Again!🔥🔥`") |
133 | | - elif not is_message_image(reply_message): |
134 | | - await event.edit( |
135 | | - "Invalid message type. Plz choose right message type u NIBBA." |
| 68 | + text = event.pattern_match.group(1) |
| 69 | + if event.reply_to_msg_id: |
| 70 | + file_name = "meme.jpg" |
| 71 | + reply_message = await event.get_reply_message() |
| 72 | + to_download_directory = TEMP_DOWNLOAD_DIRECTORY |
| 73 | + downloaded_file_name = os.path.join( |
| 74 | + to_download_directory, file_name) |
| 75 | + downloaded_file_name = await bot.download_media( |
| 76 | + reply_message, downloaded_file_name, |
136 | 77 | ) |
137 | | - return |
138 | | - else: |
139 | | - await bot.send_file(event.chat_id, response.media) |
140 | | - |
141 | | - |
142 | | -def is_message_image(message): |
143 | | - if message.media: |
144 | | - if isinstance(message.media, MessageMediaPhoto): |
145 | | - return True |
146 | | - return bool( |
147 | | - message.media.document |
148 | | - and message.media.document.mime_type.split("/")[0] == "image" |
149 | | - ) |
150 | | - |
151 | | - return False |
152 | | - |
153 | | - |
154 | | -async def silently_send_message(conv, text): |
155 | | - await conv.send_message(text) |
156 | | - response = await conv.get_response() |
157 | | - await conv.mark_read(message=response) |
158 | | - return response |
| 78 | + dls_loc = downloaded_file_name |
| 79 | + webp_file = await draw_meme_text(dls_loc, text) |
| 80 | + await event.client.send_file(event.chat_id, webp_file, reply_to=event.reply_to_msg_id) |
| 81 | + await event.delete() |
| 82 | + os.remove(webp_file) |
| 83 | + |
| 84 | + |
| 85 | +async def draw_meme_text(image_path, text): |
| 86 | + img = Image.open(image_path) |
| 87 | + os.remove(image_path) |
| 88 | + i_width, i_height = img.size |
| 89 | + m_font = ImageFont.truetype( |
| 90 | + "resources/MutantAcademyStyle.ttf", int((70 / 640) * i_width)) |
| 91 | + if ";" in text: |
| 92 | + upper_text, lower_text = text.split(";") |
| 93 | + else: |
| 94 | + upper_text = text |
| 95 | + lower_text = '' |
| 96 | + draw = ImageDraw.Draw(img) |
| 97 | + current_h, pad = 10, 5 |
| 98 | + if upper_text: |
| 99 | + for u_text in textwrap.wrap(upper_text, width=15): |
| 100 | + u_width, u_height = draw.textsize(u_text, font=m_font) |
| 101 | + |
| 102 | + draw.text(xy=(((i_width - u_width) / 2) - 1, int((current_h / 640) |
| 103 | + * i_width)), text=u_text, font=m_font, fill=(0, 0, 0)) |
| 104 | + draw.text(xy=(((i_width - u_width) / 2) + 1, int((current_h / 640) |
| 105 | + * i_width)), text=u_text, font=m_font, fill=(0, 0, 0)) |
| 106 | + draw.text(xy=((i_width - u_width) / 2, |
| 107 | + int(((current_h / 640) * i_width)) - 1), |
| 108 | + text=u_text, |
| 109 | + font=m_font, |
| 110 | + fill=(0, |
| 111 | + 0, |
| 112 | + 0)) |
| 113 | + draw.text(xy=(((i_width - u_width) / 2), |
| 114 | + int(((current_h / 640) * i_width)) + 1), |
| 115 | + text=u_text, |
| 116 | + font=m_font, |
| 117 | + fill=(0, |
| 118 | + 0, |
| 119 | + 0)) |
| 120 | + |
| 121 | + draw.text(xy=((i_width - u_width) / 2, int((current_h / 640) |
| 122 | + * i_width)), text=u_text, font=m_font, fill=(255, 255, 255)) |
| 123 | + current_h += u_height + pad |
| 124 | + if lower_text: |
| 125 | + for l_text in textwrap.wrap(lower_text, width=15): |
| 126 | + u_width, u_height = draw.textsize(l_text, font=m_font) |
| 127 | + |
| 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) + 1, i_height - u_height - int((20 / 640) * i_width)), |
| 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 | + draw.text( |
| 138 | + xy=((i_width - u_width) / 2, (i_height - u_height - int((20 / 640) * i_width)) + 1), |
| 139 | + text=l_text, font=m_font, fill=(0, 0, 0)) |
| 140 | + |
| 141 | + draw.text( |
| 142 | + xy=((i_width - u_width) / 2, i_height - u_height - int((20 / 640) * i_width)), |
| 143 | + text=l_text, font=m_font, fill=(255, 255, 255)) |
| 144 | + current_h += u_height + pad |
| 145 | + |
| 146 | + image_name = "memify.webp" |
| 147 | + webp_file = os.path.join(TEMP_DOWNLOAD_DIRECTORY, image_name) |
| 148 | + img.save(webp_file, "WebP") |
| 149 | + return webp_file |
159 | 150 |
|
160 | 151 |
|
161 | 152 | @register(outgoing=True, pattern=r"^\.q(?: |$)(.*)") |
|
0 commit comments