-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcowsay.py
45 lines (43 loc) · 1.37 KB
/
cowsay.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
from subprocess import Popen, PIPE
from PIL import Image, ImageFont, ImageDraw
from io import BytesIO
import core
PLUGINVERSION = 2
# Always name this variable as `plugin`
# If you dont, module loader will fail to load the plugin!
plugin = core.Plugin()
@plugin.command(command="/cowsay",
description="Have you mooed today?",
inline_supported=True,
required_args=1,
hidden=False)
def cowsay(_, update, user, args):
"""
User:/cowsay test
Bot:[Picture of cow saying moo]
"""
args = " ".join(args)
proc = Popen(['cowsay'], stdout=PIPE, stdin=PIPE)
stdout = str(
proc.communicate(input=bytes(args, 'utf-8'))[0], 'utf-8').split('\n')
font = ImageFont.truetype("arial.ttf", 12)
width = []
height_t = []
height = 0
for item in stdout:
size = font.getsize(item)
width.append(size[0])
height_t.append(size[1])
width = max(width) + 10
for item in height_t:
height = height + item
height = height + 20
size = width, height
img = Image.new("RGB", size, (255, 255, 255))
draw = ImageDraw.Draw(img)
draw.text((5, 5), "\n".join(stdout), (0, 0, 0), font=font)
draw = ImageDraw.Draw(img)
photo = BytesIO()
img.save(photo, 'PNG')
photo.seek(0)
return core.message(photo=photo)