Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 26 additions & 22 deletions leveler/leveler.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class Leveler:

def __init__(self, bot):
self.bot = bot
self.session = aiohttp.ClientSession()
self.backgrounds = fileIO("data/leveler/backgrounds.json", "load")
self.badges = fileIO("data/leveler/badges.json", "load")
self.settings = fileIO("data/leveler/settings.json", "load")
Expand All @@ -75,6 +76,9 @@ def create_global(self):
userinfo['user_id'] = userid
db.users.insert_one(userinfo)

def __unload(self):
self.session.close()


@commands.cooldown(1, 10, commands.BucketType.user)
@commands.command(name = "profile", pass_context=True, no_pm=True)
Expand Down Expand Up @@ -708,7 +712,7 @@ async def _auto_color(self, url:str, ranks):
await self.bot.say("**{}**".format(random.choice(phrases)))
clusters = 10

async with aiohttp.get(url) as r:
async with self.session.get(url) as r:
image = await r.content.read()
with open('data/leveler/temp_auto.png','wb') as f:
f.write(image)
Expand Down Expand Up @@ -1087,7 +1091,7 @@ async def _valid_image_url(self, url):
max_byte = 1000

try:
async with aiohttp.get(url) as r:
async with self.session.get(url) as r:
image = await r.content.read()
with open('data/leveler/test.png','wb') as f:
f.write(image)
Expand Down Expand Up @@ -2004,15 +2008,15 @@ def _write_unicode(text, init_x, y, font, unicode_font, fill):
bg_image = Image
profile_image = Image

async with aiohttp.get(bg_url) as r:
async with self.session.get(bg_url) as r:
image = await r.content.read()
with open('data/leveler/temp/{}_temp_profile_bg.png'.format(user.id),'wb') as f:
f.write(image)
try:
async with aiohttp.get(profile_url) as r:
async with self.session.get(profile_url) as r:
image = await r.content.read()
except:
async with aiohttp.get(default_avatar_url) as r:
async with self.session.get(default_avatar_url) as r:
image = await r.content.read()
with open('data/leveler/temp/{}_temp_profile_profile.png'.format(user.id),'wb') as f:
f.write(image)
Expand Down Expand Up @@ -2194,7 +2198,7 @@ def _write_unicode(text, init_x, y, font, unicode_font, fill):
# determine image or color for badge bg
if await self._valid_image_url(bg_color):
# get image
async with aiohttp.get(bg_color) as r:
async with self.session.get(bg_color) as r:
image = await r.content.read()
with open('data/leveler/temp/{}_temp_badge.png'.format(user.id),'wb') as f:
f.write(image)
Expand Down Expand Up @@ -2351,23 +2355,23 @@ def _write_unicode(text, init_x, y, font, unicode_font, fill):
bg_image = Image
profile_image = Image

async with aiohttp.get(bg_url) as r:
async with self.session.get(bg_url) as r:
image = await r.content.read()
with open('data/leveler/temp/{}_temp_rank_bg.png'.format(user.id),'wb') as f:
f.write(image)
try:
async with aiohttp.get(profile_url) as r:
async with self.session.get(profile_url) as r:
image = await r.content.read()
except:
async with aiohttp.get(default_avatar_url) as r:
async with self.session.get(default_avatar_url) as r:
image = await r.content.read()
with open('data/leveler/temp/{}_temp_rank_profile.png'.format(user.id),'wb') as f:
f.write(image)
try:
async with aiohttp.get(server_icon_url) as r:
async with self.session.get(server_icon_url) as r:
image = await r.content.read()
except:
async with aiohttp.get(default_avatar_url) as r:
async with self.session.get(default_avatar_url) as r:
image = await r.content.read()
with open('data/leveler/temp/{}_temp_server_icon.png'.format(user.id),'wb') as f:
f.write(image)
Expand Down Expand Up @@ -2559,23 +2563,23 @@ def _write_unicode(text, init_x, y, font, unicode_font, fill):
bg_image = Image
profile_image = Image

async with aiohttp.get(bg_url) as r:
async with self.session.get(bg_url) as r:
image = await r.content.read()
with open('data/leveler/temp/test_temp_rank_bg.png'.format(user.id),'wb') as f:
f.write(image)
try:
async with aiohttp.get(profile_url) as r:
async with self.session.get(profile_url) as r:
image = await r.content.read()
except:
async with aiohttp.get(default_avatar_url) as r:
async with self.session.get(default_avatar_url) as r:
image = await r.content.read()
with open('data/leveler/temp/test_temp_rank_profile.png'.format(user.id),'wb') as f:
f.write(image)
try:
async with aiohttp.get(server_icon_url) as r:
async with self.session.get(server_icon_url) as r:
image = await r.content.read()
except:
async with aiohttp.get(default_avatar_url) as r:
async with self.session.get(default_avatar_url) as r:
image = await r.content.read()
with open('data/leveler/temp/test_temp_server_icon.png'.format(user.id),'wb') as f:
f.write(image)
Expand Down Expand Up @@ -2738,15 +2742,15 @@ async def draw_levelup(self, user, server):
bg_image = Image
profile_image = Image

async with aiohttp.get(bg_url) as r:
async with self.session.get(bg_url) as r:
image = await r.content.read()
with open('data/leveler/temp/{}_temp_level_bg.png'.format(user.id),'wb') as f:
f.write(image)
try:
async with aiohttp.get(profile_url) as r:
async with self.session.get(profile_url) as r:
image = await r.content.read()
except:
async with aiohttp.get(default_avatar_url) as r:
async with self.session.get(default_avatar_url) as r:
image = await r.content.read()
with open('data/leveler/temp/{}_temp_level_profile.png'.format(user.id),'wb') as f:
f.write(image)
Expand Down Expand Up @@ -2844,15 +2848,15 @@ async def draw_levelup(self, user, server):
bg_image = Image
profile_image = Image

async with aiohttp.get(bg_url) as r:
async with self.session.get(bg_url) as r:
image = await r.content.read()
with open('data/leveler/temp/{}_temp_level_bg.png'.format(user.id),'wb') as f:
f.write(image)
try:
async with aiohttp.get(profile_url) as r:
async with self.session.get(profile_url) as r:
image = await r.content.read()
except:
async with aiohttp.get(default_avatar_url) as r:
async with self.session.get(default_avatar_url) as r:
image = await r.content.read()
with open('data/leveler/temp/{}_temp_level_profile.png'.format(user.id),'wb') as f:
f.write(image)
Expand Down