forked from TeamUltroid/UltroidAddons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sticklet.py
72 lines (65 loc) · 2.25 KB
/
sticklet.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# Ultroid - UserBot
# Copyright (C) 2020 TeamUltroid
#
# This file is a part of < https://github.com/TeamUltroid/Ultroid/ >
# PLease read the GNU Affero General Public License in
# <https://www.github.com/TeamUltroid/Ultroid/blob/main/LICENSE/>.
"""
✘ Commands Available -
• `{i}sticklet <text>`
`create random sticker with text.`
"""
import io
import os
import random
import textwrap
from glob import glob
from PIL import Image, ImageDraw, ImageFont
from telethon.errors.rpcerrorlist import BotMethodInvalidError
from telethon.tl.types import InputMessagesFilterDocument
from . import *
@ultroid_cmd(pattern="sticklet (.*)")
async def sticklet(event):
a = await event.eor("`Processing...`")
R = random.randint(0, 256)
G = random.randint(0, 256)
B = random.randint(0, 256)
sticktext = event.pattern_match.group(1)
if not sticktext:
return await event.eor("`Give me some Text`")
sticktext = textwrap.wrap(sticktext, width=10)
# converts back the list to a string
sticktext = "\n".join(sticktext)
image = Image.new("RGBA", (512, 512), (255, 255, 255, 0))
draw = ImageDraw.Draw(image)
fontsize = 230
try:
font_file_ = await event.client.get_messages(
entity="@fonthub", filter=InputMessagesFilterDocument, limit=None
)
FONT_FILE = await random.choice(font_file_).download_media()
except BotMethodInvalidError:
font_file_ = glob("resources/fonts/*ttf")
FONT_FILE = random.choice(font_file_)
font = ImageFont.truetype(FONT_FILE, size=fontsize)
for i in range(10):
if not draw.multiline_textsize(sticktext, font=font) > (512, 512):
break
fontsize = 100
font = ImageFont.truetype(FONT_FILE, size=fontsize)
width, height = draw.multiline_textsize(sticktext, font=font)
draw.multiline_text(
((512 - width) / 2, (512 - height) / 2), sticktext, font=font, fill=(R, G, B)
)
image_stream = io.BytesIO()
image_stream.name = "ult.webp"
image.save(image_stream, "WebP")
image_stream.seek(0)
await a.delete()
await event.client.send_message(
event.chat_id,
"{}".format(sticktext),
file=image_stream,
reply_to=event.message.reply_to_msg_id,
)
os.remove(FONT_FILE)