From 027666f95ccaf07dfc73d2bfb7487e5a61bcd2d2 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Sat, 7 Nov 2020 09:45:19 +0200 Subject: Remove both cogs and extensions on closing --- bot/bot.py | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/bot/bot.py b/bot/bot.py index 9a60474b3..fbd97dc18 100644 --- a/bot/bot.py +++ b/bot/bot.py @@ -3,6 +3,7 @@ import logging import socket import warnings from collections import defaultdict +from contextlib import suppress from typing import Dict, List, Optional import aiohttp @@ -134,20 +135,12 @@ class Bot(commands.Bot): self._recreate() super().clear() - def _remove_extensions(self) -> None: - """Remove all extensions to trigger cog unloads.""" - extensions = list(self.extensions.keys()) - - for ext in extensions: - try: - self.unload_extension(ext) - except Exception: - pass - async def close(self) -> None: """Close the Discord connection and the aiohttp session, connector, statsd client, and resolver.""" # Done before super().close() to allow tasks finish before the HTTP session closes. - self._remove_extensions() + with suppress(Exception): + [self.unload_extension(ext) for ext in tuple(self.extensions)] + [self.remove_cog(cog) for cog in tuple(self.cogs)] # Wait until all tasks that have to be completed before bot is closing is done log.trace("Waiting for tasks before closing.") -- cgit v1.2.3