-
-
Notifications
You must be signed in to change notification settings - Fork 347
/
sticklet.py
77 lines (60 loc) · 2.27 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
73
74
75
76
77
"""
Command - .sticklet <text>
Make sticker of text with random colour and font.
"""
import io
import os
import random
import textwrap
from PIL import Image, ImageDraw, ImageFont
from telethon.tl.types import InputMessagesFilterDocument
from ULTRA.utils import admin_cmd
@borg.on(admin_cmd(pattern="text (.*)"))
async def sticklet(event):
R = random.randint(0,256)
G = random.randint(0,256)
B = random.randint(0,256)
# get the input text
# the text on which we would like to do the magic on
sticktext = event.pattern_match.group(1)
# delete the userbot command,
await event.delete()
# https://docs.python.org/3/library/textwrap.html#textwrap.wrap
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
FONT_FILE = await get_font_file(event.client, "@FontHub")
font = ImageFont.truetype(FONT_FILE, size=fontsize)
while draw.multiline_textsize(sticktext, font=font) > (512, 512):
fontsize -= 3
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 = "leobrownlee.webp"
image.save(image_stream, "WebP")
image_stream.seek(0)
# finally, reply the sticker
await event.client.send_message(event.chat_id, "{}".format(sticktext), file=image_stream, reply_to=event.message.reply_to_msg_id)
# cleanup
try:
os.remove(FONT_FILE)
except:
pass
async def get_font_file(client, channel_id):
# first get the font messages
font_file_message_s = await client.get_messages(
entity=channel_id,
filter=InputMessagesFilterDocument,
# this might cause FLOOD WAIT,
# if used too many times
limit=None
)
# get a random font from the list of fonts
# https://docs.python.org/3/library/random.html#random.choice
font_file_message = random.choice(font_file_message_s)
# download and return the file path
return await client.download_media(font_file_message)