From 5c5a59c18e41538bee44ed85b7cf1806e830623d Mon Sep 17 00:00:00 2001 From: Rapptz Date: Sat, 16 Dec 2017 21:36:39 -0500 Subject: [PATCH] Add Client.clear to clear the bot's internal state to a clean slate. In case you want to have some external restart loop. --- discord/client.py | 12 ++++++++++++ discord/http.py | 4 ++++ 2 files changed, 16 insertions(+) diff --git a/discord/client.py b/discord/client.py index deede9cce..1bc06d90b 100644 --- a/discord/client.py +++ b/discord/client.py @@ -462,6 +462,18 @@ class Client: yield from self.http.close() self._ready.clear() + def clear(self): + """Clears the internal state of the bot. + + After this, the bot can be considered "re-opened", i.e. :meth:`.is_closed` + and :meth:`.is_ready` both return ``False`` along with the bot's internal + cache cleared. + """ + self._closed.clear() + self._ready.clear() + self._connection.clear() + self._http.recreate() + @asyncio.coroutine def start(self, *args, **kwargs): """|coro| diff --git a/discord/http.py b/discord/http.py index 70d47424f..7ec64d82c 100644 --- a/discord/http.py +++ b/discord/http.py @@ -102,6 +102,10 @@ class HTTPClient: user_agent = 'DiscordBot (https://github.com/Rapptz/discord.py {0}) Python/{1[0]}.{1[1]} aiohttp/{2}' self.user_agent = user_agent.format(__version__, sys.version_info, aiohttp.__version__) + def recreate(self): + if self._session.closed: + self._session = aiohttp.ClientSession(connector=self.connector, loop=self.loop) + @asyncio.coroutine def request(self, route, *, header_bypass_delay=None, **kwargs): bucket = route.bucket