diff --git a/bottery/bottery.py b/bottery/bottery.py index 97d0f76..5173f6c 100644 --- a/bottery/bottery.py +++ b/bottery/bottery.py @@ -101,8 +101,16 @@ def run(self, server_port): # sys.exit(1) click.echo('Quit the bot with CONTROL-C') - self.loop.run_forever() + + try: + self.loop.run_forever() + except KeyboardInterrupt: + self.stop() + finally: + self.cleanup() def stop(self): - asyncio.gather(self.session.close()) self.loop.stop() + + def cleanup(self): + self.loop.run_until_complete(self.session.close()) diff --git a/bottery/cli.py b/bottery/cli.py index 1669cc4..cfca4fe 100644 --- a/bottery/cli.py +++ b/bottery/cli.py @@ -80,8 +80,4 @@ def import_string(import_name): @click.option('--port', default=7000, type=int) def run(bot_module, port): bot = Bottery() - - try: - bot.run(server_port=port) - except KeyboardInterrupt: - bot.stop() + bot.run(server_port=port)