diff --git a/src/main/cogs/owner.py b/src/main/cogs/owner.py index a78d3fc..ac3b2c6 100644 --- a/src/main/cogs/owner.py +++ b/src/main/cogs/owner.py @@ -28,13 +28,12 @@ class Bot: @checks.del_ctx() async def die(self, ctx): if isinstance(self.bot.get_channel(u.config['shutdown_channel']), d.TextChannel): - await self.bot.get_channel(u.config['shutdown_channel']).send('**Shutting down . . .** 🌙') + await self.bot.get_channel(u.config['shutdown_channel']).send('**Shutting down 🌙 . . .**') # loop = self.bot.loop.all_tasks() # for task in loop: # task.cancel() - u.close() await self.bot.logout() - await self.bot.close() + u.close(self.bot.loop) print('\n/ / / / / / / / / / / /\nD I S C O N N E C T E D\n\\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\\n') # u.notify('D I S C O N N E C T E D') @@ -44,14 +43,13 @@ class Bot: async def restart(self, ctx): print('\n| | | | | | | | | |\nR E S T A R T I N G\n| | | | | | | | | |\n') if isinstance(self.bot.get_channel(u.config['shutdown_channel']), d.TextChannel): - await self.bot.get_channel(u.config['shutdown_channel']).send('**Restarting . . .** 💤') + await self.bot.get_channel(u.config['shutdown_channel']).send('**Restarting 💤 . . .**') # u.notify('R E S T A R T I N G') # loop = self.bot.loop.all_tasks() # for task in loop: # task.cancel() - u.close() await self.bot.logout() - await self.bot.close() + u.close(self.bot.loop) os.execl(sys.executable, 'python3', 'run.py') # Invite bot to bot owner's server diff --git a/src/main/run.py b/src/main/run.py index 8ee88a8..453debe 100644 --- a/src/main/run.py +++ b/src/main/run.py @@ -35,7 +35,7 @@ async def on_ready(): # bot.loop.create_task(u.clear(booru.temp_urls, 30*60)) if isinstance(bot.get_channel(u.config['startup_channel']), d.TextChannel): - await bot.get_channel(u.config['startup_channel']).send('**Started.** ☀️') + await bot.get_channel(u.config['startup_channel']).send('**Started ☀️ .**') print('\n\\ \\ \\ \\ \\ \\ \\ \\ \\\nC O N N E C T E D : {}\n/ / / / / / / / /\n'.format(bot.user.name)) # u.notify('C O N N E C T E D') @@ -44,9 +44,8 @@ async def on_ready(): async def on_error(error, *args, **kwargs): if isinstance(bot.get_channel(u.config['shutdown_channel']), d.TextChannel): await bot.get_channel(u.config['shutdown_channel']).send('**ERROR** ⚠️ {}'.format(error)) - u.close() await bot.logout() - await bot.close() + u.close(bot.loop) print('\n! ! ! ! !\nE R R O R : {}\n! ! ! ! !\n'.format(error), file=sys.stderr) tb.print_exc() # u.notify('E R R O R') diff --git a/src/main/utils/utils.py b/src/main/utils/utils.py index 2bd4d6b..bf1d834 100644 --- a/src/main/utils/utils.py +++ b/src/main/utils/utils.py @@ -3,6 +3,7 @@ import json import os import pickle as pkl import subprocess +from contextlib import suppress import aiohttp from pync import Notifier @@ -72,12 +73,22 @@ async def clear(obj, interval=10 * 60, replace=None): session = aiohttp.ClientSession() -def close(): +def close(loop): global session if session: session.close() + loop.stop() + pending = asyncio.Task.all_tasks() + for task in pending: + task.cancel() + # with suppress(asyncio.CancelledError): + # loop.run_until_complete(task) + # loop.close() + + print('Finished cancelling tasks.') + async def fetch(url, *, params={}, json=False): global session