diff --git a/leveler/leveler.py b/leveler/leveler.py index d5f064d..778e3f1 100644 --- a/leveler/leveler.py +++ b/leveler/leveler.py @@ -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") @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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)