-
Notifications
You must be signed in to change notification settings - Fork 0
/
welcome.py
161 lines (136 loc) · 6.01 KB
/
welcome.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# This file was created by github.com/ruin332
# He is 100% boss
# he knows de way
from discord.ext import commands
import discord
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
from PIL import ImageOps
from PIL import ImageFilter
import os
import glob
import random
from io import StringIO, BytesIO
import aiohttp
from unidecode import unidecode
import asyncio
class Welcome:
def __init__(self, bot):
self.bot = bot
# im is the background to use (pass in from PIL's Image.open)
# offset_x and offset_y refer to avatar img
# returns BytesIO file-like object for easy use with bot.send_file
async def welcome_member(self, im, font, member, offset_x=0, offset_y=-70,
new_width=1000, new_height=500, ava_sqdim=260,
text_offset_x=0, text_offset_y=140, text=None,
is_square=False, text_color=None, blur_radius=15,
blur_offset_y=0, outline=True):
im = im.copy()
width, height = im.size
name = unidecode(member.name)
if text is None:
welcome = 'Welcome {0},\n to {1.server.name}!'.format(name, member)
else:
welcome = text
left = (width - new_width) // 2
top = (height - new_height) // 2
right = (width + new_width) // 2
bottom = (height + new_height) // 2
im = im.crop((left, top, right, bottom)).convert("RGB")
# how to set up a gradient from the bottom:
# fade_from = new_height/4
# fade_to = new_height-fade_from
#
# fade_from = int(fade_from)
# fade_to = int(fade_to)
#
# for i in range(fade_from, new_height+1):
# fade = int((i-fade_from)/(fade_to)*255)
# draw.rectangle(((0, i), (new_width, i)), fill=(0, 0, 0, fade))
ov_left = 0
ov_top = (im.height // 2) + (blur_offset_y)
ov_right = im.width
ov_bottom = im.height
ov_box = (ov_left, ov_top, ov_right, ov_bottom)
ov_ic = im.crop(ov_box)
ov_ic = ov_ic.filter(ImageFilter.GaussianBlur(blur_radius))
im.paste(ov_ic, ov_box)
draw = ImageDraw.Draw(im, mode='RGBA')
draw.rectangle(((ov_left, ov_top), (ov_right, ov_bottom)), fill=(0, 0, 0, 120))
avatar_im = None
url = member.avatar_url
if not url:
url = member.default_avatar_url
retries = 1
while True:
async with aiohttp.ClientSession(loop=self.bot.loop) as aiosession:
with aiohttp.Timeout(10):
async with aiosession.get(url) as resp:
avatar_im = BytesIO(await resp.read())
if avatar_im.getbuffer().nbytes > 0 or retries == 0:
await aiosession.close()
break
retries -= 1
print('0 nbytes image found. Retries left: {}'.format(retries+1))
resize = (ava_sqdim, ava_sqdim)
avatar_im = Image.open(avatar_im).convert("RGBA")
avatar_im = avatar_im.resize(resize, Image.ANTIALIAS)
avatar_im.putalpha(avatar_im.split()[3])
if not is_square:
mask = Image.new('L', resize, 0)
maskDraw = ImageDraw.Draw(mask)
maskDraw.ellipse((0, 0) + resize, fill=255)
mask = mask.resize(avatar_im.size, Image.ANTIALIAS)
avatar_im.putalpha(mask)
img_center_x = (im.width // 2)
img_center_y = (im.height // 2)
img_offset_x = img_center_x + offset_x
img_offset_y = img_center_y + offset_y
ava_right = img_offset_x + avatar_im.width//2
ava_bottom = img_offset_y + avatar_im.height//2
ava_left = img_offset_x - avatar_im.width//2
ava_top = img_offset_y - avatar_im.height//2
im.paste(avatar_im, box=(ava_left, ava_top, ava_right, ava_bottom), mask=avatar_im)
text_width, text_height = draw.textsize(welcome, font=font)
x = ((img_center_x - text_width / 2) + text_offset_x)
y = ((img_center_y - text_height / 2) + text_offset_y)
if outline:
border_coords = ((x-1, y), (x+1, y), (x, y-1), (x, y+1), (x-1, y-1),
(x+1, y-1), (x-1, y+1), (x+1, y+1))
for coord in border_coords:
draw.text(coord, welcome, font=font, align='center', fill='black')
draw.text((x, y), welcome, fill=text_color, font=font, align='center')
temp = BytesIO()
im.save(temp, format='png')
temp.seek(0)
return temp
async def setup_welcome(self, member, ch=False):
if member.bot:
return
cwd = os.getcwd()
send_to = self.bot.get_server('300155035558346752').get_channel(ch) if ch else None
kwargs = dict.fromkeys(['offset_x', 'offset_y', 'text_offset_x',
'text_offset_y', 'new_width', 'new_height',
'ava_sqdim', 'is_square', 'text', 'text_color',
'blur_radius', 'blur_offset_y', 'outline'])
if member.server.id == '300155035558346752':
files = glob.glob('{}/images2/*.png'.format(cwd))
files.extend(glob.glob('{}/images2/*.jpg'.format(cwd)))
font = ImageFont.truetype('Stranger back in the Night.ttf', 100)
# kwargs['ava_sqdim'] = 200
# kwargs['blur_offset_y'] = 100
rand_img = random.choice(files)
im = Image.open(rand_img)
else:
return
keys_to_remove = [key for key, value in kwargs.items() if value is None]
for k in keys_to_remove:
kwargs.pop(k, None)
im = await self.welcome_member(im, font, member, **kwargs)
if send_to is None:
send_to = member.server.default_channel
content = 'Welcome {0.mention} to {0.server.name}!'.format(member)
await self.bot.send_file(send_to, im, content=content, filename='welcome.jpg')
def setup(bot):
bot.add_cog(Welcome(bot))